c++ constructors

Posted on 2003-12-05
Medium Priority
Last Modified: 2010-04-01
hello experts,
i have a question regarding Constructures in c++.
please tell me what is fun of declaring a constructor as a private member in a class ? what purpose will it serve ? In which situations it may be required ?
Question by:soodsandeep
LVL 10

Expert Comment

ID: 9887241
It may be required in case you do not want any one to create an object of your class

This is useful to create a singleton class - a class which can have only one object instantiated. This is done by declaring the constructor private/protected [both default and copy constructors] and also having a static data member of the same type in the class itself. By this, you would have only one object of your class as the static variable of the class type would get instantiated differently thry static member initialization method

Let me know if u got this OR need some code for this




Expert Comment

ID: 9887251
FAQ. Here are some similar ones:

Any many more... (but I guess those should be sufficient). You can post in CS and request a refund. =)

Accepted Solution

dennis_george earned 210 total points
ID: 9887381
Hi Sandeep,

   See, constructors are provided to initialize an object and is called automatically. So you can relate this as......

  Constructor ---- Creation of object...

  Now suppose you want to put some restriction to some of your class members you do this by access specifiers (like private public)......

  Now if you want to restrict or want to control your object creation you make your constructor private... So that nobody can create an object directly.

 Now if you want to create an object of such class you have to request to the class itself to creat its instance.... request to class (or calling a function of class without its object) is implemented through static functions.
Here you can do whatever you want to do....
     you may restrict that only one object of that particlar class should be created (Singleton class).
 or you may want to create some limited number of objects (like allocating network resources(limited)  )
 or you may validate the request of object creation....

So constructors are made private to control or restrict the object creation.....

Hope you got a clear picture...

Expert Comment

ID: 9889107
Look In the class constructor are called automatically but if u want that no object of ur class can be made than u can keep ur constructor private e.g
class test
cout<<"Private constructor called";

static void message()
cout<<"Hello world";
Now no body can call this constructor through the object if u want to call the function message() than u have to call like this
test.message(); directly through the class
Its output would be like this
Private constructor called
Hello world
Sorry for poor english

Expert Comment

ID: 9894547
Hi minamkhan,
First of all syntax error...
you cannot call message() by test.message() because test is not a  object..... you have to use
test::message() ;
And private constructors are never called in the above example you gave.... because you are not creating an object of that class. so the output will be
Hello World


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

588 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