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

constructor

Posted on 2000-04-03
7
391 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
getting a fire fox page (view page source ) in c++ 6 42
Path to  STL Map header file 1 69
c++, dynamic object by json 1 53
Need some help with mailto 16 20
  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 …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

856 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