• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

c++ constructors

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 ?
thanks.
0
soodsandeep
Asked:
soodsandeep
1 Solution
 
Sys_ProgCommented:
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

HTH

Amit

0
 
n_fortynineCommented:
FAQ. Here are some similar ones:
http://Q_10321476.html
http://Q_20779542.htm
http://Q_20318193.html

Any many more... (but I guess those should be sufficient). You can post in CS and request a refund. =)
0
 
dennis_georgeCommented:
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...
Dennis
0
 
minamkhanCommented:
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
{
private:
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
minamkhan
0
 
dennis_georgeCommented:
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

Dennis
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now