Link to home
Start Free TrialLog in
Avatar of klopter
klopter

asked on

Can you explain the use of static in named constructors?

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

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mirtol
mirtol

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())
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...
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.
Avatar of klopter

ASKER

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
You cannot use 'rectangular' without specifying to which class it belongs... unless you call it from another method of that class...
Me wrong. static can't be called globally. Sorry.
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.
Avatar of klopter

ASKER

mirtol - Thanks for your comments.  They were also helpful.

Ken