Solved

private constructors

Posted on 2002-07-01
15
371 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
Comment Utility
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
Comment Utility
Yes this is true. But still why constructors be defined in private region. Does it have anything to do with creating singleton class
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Expert Comment

by:nke2000
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
rsraj,
what "default vc++ classes" are you referring to?  Ones in standard headers?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now