Solved

constructor

Posted on 2000-04-03
7
393 Views
Last Modified: 2010-04-02
if constructor declar in private part
0
Comment
Question by:vipinkumar
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

  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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 how to clear a vector as well as how to detect empty vectors in C++.

691 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