Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private constructor [duplicate]

Possible Duplicate:
What is the use of making constructor private in a class?

Where do we need private constructor? How can we instantiate a class having private constructor?

like image 421
Atul Avatar asked Jan 10 '11 15:01

Atul


People also ask

What happens when you make copy constructor private?

According to the C + + copy constructor it is possible to make a copy function Object() { [native code] }. When a copy function Object() { [native code] } in a class is made private, objects in that class become non-copyable.

Can copy constructor be made private?

Can a copy constructor be made private? Explanation: The copy constructor can be defined as private. If we make it private then the objects of the class can't be copied. It can be used when a class used dynamic memory allocation.

Can you have multiple copy constructors?

A class can have multiple copy constructors, e.g. both T::T(const T&) and T::T(T&).

Can private constructor instantiate?

Yes, we can access the private constructor or instantiate a class with private constructor. The java reflection API and the singleton design pattern has heavily utilized concept to access to private constructor.


2 Answers

Private constructor means a user cannot directly instantiate a class. Instead, you can create objects using something like the Named Constructor Idiom, where you have static class functions that can create and return instances of a class.

The Named Constructor Idiom is for more intuitive usage of a class. The example provided at the C++ FAQ is for a class that can be used to represent multiple coordinate systems.

This is pulled directly from the link. It is a class representing points in different coordinate systems, but it can used to represent both Rectangular and Polar coordinate points, so to make it more intuitive for the user, different functions are used to represent what coordinate system the returned Point represents.

 #include <cmath>               // To get std::sin() and std::cos()   class Point {  public:    static Point rectangular(float x, float y);      // Rectangular coord's    static Point polar(float radius, float angle);   // Polar coordinates    // These static methods are the so-called "named constructors"    ...  private:    Point(float x, float y);     // Rectangular coordinates    float x_, y_;  };   inline Point::Point(float x, float y)    : x_(x), y_(y) { }   inline Point Point::rectangular(float x, float y)  { return Point(x, y); }   inline Point Point::polar(float radius, float angle)  { return Point(radius*std::cos(angle), radius*std::sin(angle)); } 

There have been a lot of other responses that also fit the spirit of why private constructors are ever used in C++ (Singleton pattern among them).

Another thing you can do with it is to prevent inheritance of your class, since derived classes won't be able to access your class' constructor. Of course, in this situation, you still need a function that creates instances of the class.

like image 198
逆さま Avatar answered Sep 21 '22 18:09

逆さま


One common use is in the singleton pattern where you want only one instance of the class to exist. In that case, you can provide a static method which does the instantiation of the object. This way the number of objects instantiated of a particular class can be controlled.

like image 29
Naveen Avatar answered Sep 24 '22 18:09

Naveen