Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Can you explain the use of static in named constructors?

Posted on 2002-05-28
9
196 Views
Last Modified: 2010-04-02
I took this example of named constructors from Marshall Cline's FAQ-lite: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6

     class Point {
     public:
       static Point rectangular(float x, float y);      // Rectangular coord's
       static Point polar(float radius, float angle);   // Polar coordinates
       // These static methods are the so-called "named constructors"
       // ...
     private:
       Point(float x, float y);     // Rectangular coordinates
       float x_, y_;
     };
     

Bjarne Stroustrup in "The C++ programming language"  second edition p. 166, bottom of section 5.4.4 Static Members says:  "For member functions, static has only the second meaning."  That second meaning from the previous sentence is: " static as in `restricted visibility' as opposed to external linkage."

And yet, there seems to be more that restricted visibility going on here.  If this were just a matter of restricted visibility, it should be possible to do the same without that restricted visibility.  

Can the above example to modified to eliminate the visibility restriction on Point::rectangular and Point::Polar?

I don't claim that there is a good reason to do so.  I am just
trying to understand the concept here.  

Thanks,
  Ken

0
Comment
Question by:klopter
  • 4
  • 3
  • 2
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 7039376
>>Can the above example to modified to eliminate the
>>visibility restriction on Point::rectangular and
>>Point::Polar?

Yes, you could use a function that serves as a 'class factory', e.g.

    class Point {

    friend Point PointFactoryFromRectangular ( float x, float y);
    friend Point PointFactoryFromPolar ( float r, float phi);
    public:
    private:
      Point(float x, float y);     // Rectangular coordinates
      float x_, y_;
    };
   
Point PointFactoryFromRectangular ( float x, float y) {

 return Point ( x, y);
}

Point PointFactoryFromPolar ( float r, float phi) {

// tranform coordinates to rectangular and create object
 return Point ( x, y);
}



0
 
LVL 2

Expert Comment

by:mirtol
ID: 7039478
You've got a whole loada things going on here:

static func()
 - Is a member of the class
 - it cannot access object-specific data, only class-specific data UNLESS a pointer is passed to it, then it may access object-specific data.
 - it can be called globally, via reference to the class and from the object (ie: func(), CLASS::func() or OBJ->func())

friend func()
 - Is NOT a member of the class
 - it cannot access object-specific data, only class-specific data UNLESS a pointer is passed to it, then it may access object-specific data.
 - it may only be called globally (ie: func())

func()
 - Is a member of the class
 - May access object-specific data using 'this' pointer
 - it can only be called from the object (ie: OBJ->func())
0
 
LVL 86

Expert Comment

by:jkr
ID: 7039482
BTW, to address 'the use of static' - in this sample, the members have to be static in order be abler to be called without an instance of the class. As you see, the only ctor is 'private', so you either need 'friends' or members...
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:mirtol
ID: 7039488
So I guess in answer to your question - No, not exactly as the very definition of static denies you that. But by passing a pointer to your object you may restore the visibility.
0
 

Author Comment

by:klopter
ID: 7039655
I think that I pretty much get it now.  I still find Bjarne's description of the meaning of static as "restricted visibility" unhelpful here.  

I see static in this situation as meaning "callable without an object".  Or, making CLASS::func() legal.  Exactly as jkr's second comment says.

I am confused by mirtol's claim that "static func()" allows one to call func() globally.  When I removed the "Point::" from the following it would no lonber compile.
 
Point p1 = Point::rectangular(5.7,1.2);

Thanks,
  Ken
0
 
LVL 86

Expert Comment

by:jkr
ID: 7039672
You cannot use 'rectangular' without specifying to which class it belongs... unless you call it from another method of that class...
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7039680
Me wrong. static can't be called globally. Sorry.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7039691
BTW - to get back to the headline of your Q:

>>Can you explain the use of static in named constructors?

There is no such thing as 'named constructors', these are simply static member functions (IOW: Member functions that can be used without an instance of the class) that create an instance of that class and serve as a class factory. They need to be members as the the only ctor is 'private', and they need to be static to be accesible at all.
0
 

Author Comment

by:klopter
ID: 7039749
mirtol - Thanks for your comments.  They were also helpful.

Ken
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question