Solved

private constructors

Posted on 2002-07-01
15
377 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 175
How to convert MFC APP to Win32 APP. 19 133
How to add columns to a RAD Studio FMX grid at design time 1 61
using interface in TLB 3 31
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

752 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