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
Solved

c++ constructors

Posted on 2003-12-05
5
247 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ & C# in Vis Studio 2013 7 197
How to print into std::string like sprintf using C++11? 12 78
C++ mouse_event mouse look 7 94
Android development question 2 52
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

808 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