Solved

c++ constructors

Posted on 2003-12-05
5
235 Views
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 ?
thanks.
0
Comment
Question by:soodsandeep
5 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
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

HTH

Amit

0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9887251
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
 
LVL 5

Accepted Solution

by:
dennis_george earned 70 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...
Dennis
0
 

Expert Comment

by:minamkhan
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
{
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
 
LVL 5

Expert Comment

by:dennis_george
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

Dennis
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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

15 Experts available now in Live!

Get 1:1 Help Now