Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

private constructors

Posted on 2002-07-01
15
Medium Priority
?
380 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
[X]
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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 180 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

715 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