[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

c++ constructors

Posted on 2003-12-05
5
Medium Priority
?
275 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
[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 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 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...
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

656 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