Solved

constructor

Posted on 2000-04-03
7
389 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
ID: 2682114
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
ID: 2682838
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
ID: 2684312
>>if constructor declar in private part
Is that a question?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Expert Comment

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

Author Comment

by:vipinkumar
ID: 2687545
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
ID: 2687751
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
ID: 2688939
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Load and store *.pnm image file 1 86
C++ question 3 65
How to convert MFC::CString to UTF8 wchar_t* 10 210
designing in object programming 12 78
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 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