Solved

private constructors

Posted on 2002-07-01
15
373 Views
Last Modified: 2010-05-18
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
Comment
Question by:rsraj
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 7121521
0
 
LVL 32

Expert Comment

by:jhance
ID: 7121525
Actually, that is a C# article, see this one also:

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

Author Comment

by:rsraj
ID: 7121576
Yes this is true. But still why constructors be defined in private region. Does it have anything to do with creating singleton class
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 32

Expert Comment

by:jhance
ID: 7121594
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
 
LVL 32

Expert Comment

by:jhance
ID: 7121597
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
 
LVL 30

Accepted Solution

by:
Axter earned 60 total points
ID: 7121613
>>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
 
LVL 30

Expert Comment

by:Axter
ID: 7121634
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
 

Expert Comment

by:nke2000
ID: 7122197
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
 
LVL 30

Expert Comment

by:Axter
ID: 7122340
>>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
 

Expert Comment

by:nke2000
ID: 7122464
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
 
LVL 30

Expert Comment

by:Axter
ID: 7122483
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
 
LVL 30

Expert Comment

by:Axter
ID: 7122509
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
 

Expert Comment

by:nke2000
ID: 7122583
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
 

Author Comment

by:rsraj
ID: 7124147
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
 

Expert Comment

by:nke2000
ID: 7124415
rsraj,
what "default vc++ classes" are you referring to?  Ones in standard headers?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 149
C++ to C# code conversion issue 4 104
Setting nameservers after res_init fails doing res_query 2 94
Create a path if not exists 7 76
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

805 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