Private constructor

Possible duplicate:
What is the use of creating a constructor in a class?

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

+48
c ++
Jan 10 2018-11-11T00:
source share
7 answers

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

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

This is pulled directly from the link. This is a class that represents points in different coordinate systems, but it can be used to represent both rectangular and polar coordinate points, so to make it more intuitive for the user to represent which coordinate system the returned Point returned, different functions are used.

  #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 were many other answers that also corresponded to the spirit of why private constructors are ever used in C ++ (the Singleton pattern among them).

Another thing you can do with this: prevent the inheritance of your class , since derived classes will not be able to access your class, constructor. Of course, in this situation, you still need a function that instantiates the class.

+64
Jan 10 '11 at 15:52
source share

One common use in a singleton template is where you want only one instance of the class to exist. In this case, you can provide a static method that instantiates an object. This way you can control the number of objects created for a particular class.

+29
Jan 10 '11 at 15:51
source share

A private constructor is useful when you do not want your class to be created by the user. To create such classes, you need to declare a static method that does "new" and returns a pointer.

A class with private ctors cannot be placed in STL containers because they require a copy of ctor.

+6
Jan 10 '11 at 15:53
source share

It makes sense to make the constructor private if there are other methods that can instantiate. The obvious examples are the Singleton patterns (each call returns the same instance) and Factory (each call usually creates a new instance).

+5
Jan 10 '11 at 15:52
source share

This is common when you want to implement a singleton. A class may have a static factory method, which checks whether the class has already been created and calls the constructor if it does not.

+2
Jan 10 '11 at 15:51
source share

For example, you can call a private constructor inside a friend's class or friend's function.

The Singleton template typically uses it to ensure that no one creates more instances of the intended type.

+1
Jan 10 '11 at 15:52
source share

One common use for template-typedef workarounds, such as:

 template <class TObj> class MyLibrariesSmartPointer { MyLibrariesSmartPointer(); public: typedef smart_ptr<TObj> type; }; 

Obviously, an open unrealized constructor will work, but a private constructor generates a compile-time error instead of a link time error if someone tries to set MyLibrariesSmartPointer<SomeType> instead of MyLibrariesSmartPointer<SomeType>::type , which is desirable.

+1
Jan 10 '11 at 15:56
source share



All Articles