Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

private constructors

Hi,
What is the use of having constructors in private region ?
Brief what is the adv. or disadv. of having constructors in private region ?

Adv.Thanks,
Sun
0
rsraj
Asked:
rsraj
  • 5
  • 4
  • 4
  • +1
1 Solution
 
jhanceCommented:
0
 
jhanceCommented:
Actually, that is a C# article, see this one also:

http://www.devx.com/free/tips/tipview.asp?content_id=3995
0
 
rsrajAuthor Commented:
Yes this is true. But still why constructors be defined in private region. Does it have anything to do with creating singleton class
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jhanceCommented:
See:

http://www.devx.com/free/tips/tipview.asp?content_id=3995 

It explains both HOW to do this and WHY you might want to....
0
 
jhanceCommented:
BTW, this has NOTHING to do with a singleton class.  A singleton class is one that you want ONE and ONLY ONE instance of.
0
 
AxterCommented:
>>BTW, this has NOTHING to do with a singleton class.  A
>>singleton class is one that you want ONE and ONLY ONE
>>instance of.

Actually, most implementation of Singleton class makes the constructor private so as to prevent other instances from being created.

Example

class MySingleton
{
private:
MySingleton(){}
public:
static MySingleton& GetOneAndOnly_MySingleton();
};

//In *.cpp file
MySingleton& MySingleton::GetOneAndOnly_MySingleton()
{
 static MySingleton OneAndOnly;
 return OneAndOnly;
}
0
 
AxterCommented:
FYI:
For above method, you would also want to make the copy constructor private and the assignment operator private as well.

>>Brief what is the adv. or disadv. of having
>>constructors in private region ?
The advantage is that you can use it to enforce your singleton.

One of the disadvantage is that you can not derive from it, so as to create your own special version of the singleton.
0
 
nke2000Commented:
Put any constructor from which you do not want an instance of the class created private.  If you have multiple constructors, you may want the defuault constructor private to prevent its use. In other words, a user of the class may has to initialize an object of the class to a certain state.  This may be helpful to prevent misuse of the class.

I don't see a disadvantage of it.  Yes, you will not be able to use the default ctor from a derive class but that is not an intrinsic disadvantage.

--NKE
0
 
AxterCommented:
>>Yes, you will not be able to use the default ctor from a
>>derive class but that is not an intrinsic disadvantage.

Actually, you will not be able to use any constructor in derive class.  Moreover, you will not be able to derive from it at all unless you make the decendent a friend.
0
 
nke2000Commented:
Not true, Axter.  You can explicitly initialize the desired public constructor on the member initialization list in the derived class.  If you don't specify a public constructor on Derived's member intialization list, you cannot create a Deverived because the compiler calls Base's default constructor when it attempts to construct Derived.  But if you initialize Base on the member initialization list, you're all set.
See my code sample below.  Try it yourself, it compiles.
 
class Base
{
   private:
     Base();
   public:
     Base(double){++ref;}
     Base(int){++ref;}
     int ref;
};

class Derived:public Base
{
public:
  Derived(int x):Base(x){++ref;}
};


int main()
{
  Derived a(1);
  return 0;
}
0
 
AxterCommented:
nke2000,
In your example, you have public constructors.  Which of course it will work that way.
But most singleton's do not have any public constructors.

The questioner is reffering to having constructors private.  He's not asking about public constructors.
He also mention singleton's, which almost always have all constructors private.

In which case, you can not derive from such a singleton class, without using friend.

See example I posted in previous comment.  No public constructors.
0
 
AxterCommented:
nke2000:
FYI:
In your example class you posted, there really is no need to make default constructor for Base private.
If you removed it, the compiler still would not let you compile using a default constructor for Base, because you have already defined non-default constructors.

So there is no point in making a default constructor private when other constructors have been declared.
0
 
nke2000Commented:
Axter,
rsraj's question does not mention having ALL constructors listed as private; it just asks, "What is the use of having constructors in the private region?"  He asks about the Singleton class, but his original question is broader than that.  You're right, making the default ctor is pointless b/c the compiler won't let you call it, but my point was to demonstrate that it is still possible to call public constructors from a derived class ( but I apparently misunderstood you and my example was no to the point; you were talking about Singleton where all ctor's are private.)

rsraj,
I stand by my original point.  Making constructors private forces a user to initialize an object to a certain state.  There is no intrinsic disadvantage to make a constructor private, but as Axter notes, there is a disadvantage to making ALL constructors private.
0
 
rsrajAuthor Commented:
Thanks nke2000.
In VC++ - default classes are created, have constructors  declared in private region.
I explicitly declare them in public region.

In fact Axter answer is convincing my question and thanks for the same.

Do you have any idea why some default vc++ classes put the constructors in the private region ? Any specific reason behind this ?

If you could give some idea for the above question, then it is fine, otherwise I will close this tomorrow (with full points to Axter)
0
 
nke2000Commented:
rsraj,
what "default vc++ classes" are you referring to?  Ones in standard headers?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now