Solved

constructor

Posted on 2000-04-03
7
387 Views
Last Modified: 2010-04-02
if constructor declar in private part
0
Comment
Question by:vipinkumar
7 Comments
 
LVL 3

Expert Comment

by:kulina
Comment Utility
No it is supposed to be declared in public: part. Constructor is a bridge between private variables and other data in a program.
0
 

Expert Comment

by:kishore_joshi
Comment Utility
You can put the Constroctor in the private domain. As a result only the member functions wil be able to create the objects of that class. But u cannot call a member function without the object.

So, usually if u want to create the object thru a member function, then it should be static function.

If u put the Copy-constructor in the private, then u cannot pass the object of this class by value to any function (member or non-member functions).

Copy constructor can be put in the private domain to impliment Singleton class. i.e. you can place a restriction on the number of objects begin created
of that class.
 
0
 
LVL 4

Expert Comment

by:pagladasu
Comment Utility
>>if constructor declar in private part
Is that a question?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:pagladasu
Comment Utility
>>if constructor declar in private part
Is that a question?
0
 

Author Comment

by:vipinkumar
Comment Utility
if a constructor declare in private or procted part
then how many object is created . if not why or if yes why? if possible give an example.
0
 
LVL 2

Accepted Solution

by:
abesoft earned 100 total points
Comment Utility
Making a c'tor private (or protected) will mean that only member functions can use this particular c'tor.  Usually this means that the world at large will not be able to call that c'tor, unless the class re-publishes it through a static function or something like that.

Usually, c'tors are made private so that others can't use them: a copy constructor for a huge object might be a good candidate for this treatment, since it restricts accidental (or intentional) copying of the object.

class HugeDatabase{
private:
    HugeDatabase( const HugeDatabase &copy);
public:
    // Put in a public c'tor so that people can still use the class....
    HugeDatabase();
.....
};

(There are other, more obscure uses of private c'tors, but this is the most common, IMHO)

Gene
0
 

Expert Comment

by:kishore_joshi
Comment Utility
When I refered to the Singleton class, I meant the same thing as Gene's example .

Here is an example of Singleton class .....

class A
{
private:
           static int count;

           A()
           {            count++;
           }

public:
         
      static A* CreateObj()
      {
      A *a = new A;

      if(count > 1)
      {
            cout<<" Crossed the limit........ ";
            return NULL;
      }
      else
      {
            return a;
      }

      } // End of the function CreateObj
};


int A::count; // defining the static variable

void main()
{

      A *r1,*r2 ,*r3;

      r1 = A::CreateObj();
      r2 = A::CreateObj();
      r3 = A::CreateObj();

} // End of main()

Here the Copy Constructor is in private.So u can restrict the no. of objects getting created and alos the object of class A cannot be passed to any function by Value.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

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

17 Experts available now in Live!

Get 1:1 Help Now