When do we need a private constructor in C++?
There are a few scenarios for having private
constructors:
Restricting object creation for all but
friend
s; in this case all constructors have to beprivate
class A { private: A () {} public: // other accessible methods friend class B; }; class B { public: A* Create_A () { return new A; } // creation rights only with `B` };
Restricting certain type of constructor (i.e. copy constructor, default constructor). e.g.
std::fstream
doesn't allow copying by such inaccessible constructorclass A { public: A(); A(int); private: A(const A&); // C++03: Even `friend`s can't use this A(const A&) = delete; // C++11: making `private` doesn't matter };
To have a common delegate constructor, which is not supposed to be exposed to the outer world:
class A { private: int x_; A (const int x) : x_(x) {} // common delegate; but within limits of `A` public: A (const B& b) : A(b.x_) {} A (const C& c) : A(c.foo()) {} };
For singleton patterns when the singleton
class
is not inheritible (if it's inheritible then use aprotected
constructor)class Singleton { public: static Singleton& getInstance() { Singleton object; // lazy initialization or use `new` & null-check return object; } private: Singleton() {} // make `protected` for further inheritance Singleton(const Singleton&); // inaccessible Singleton& operator=(const Singleton&); // inaccessible };
A private constructor is useful when you want to control the object creation of a class.
Let’s try in code:
#include <iostream>
using namespace std;
class aTestClass
{
aTestClass() ////////// Private constructor of this class
{
cout << "Object created\n";
}
public:
};
int main()
{
aTestClass a;
aTestClass *anObject;
}
The line aTestClass a causes an error because this line is indirectly trying to access the private constructor. Comment out this line and run the program. It runs absolutely fine. Now the question is how to create the object in a such case. Let's write another program.
#include <iostream>
using namespace std;
class aTestClass
{
aTestClass() ////////// Private constructor of this class
{
cout << "Object created\n";
}
public:
aTestClass* getAnObject() ///// A public method create an object of this class and return the address of an object of that class
{
return (new aTestClass);
}
};
int main()
{
//aTestClass a;
aTestClass *anObject = NULL;
anObject = anObject->getAnObject();
}
The output is
Object created
so we have created an object of the class containing a private constructor.
Use this concept to implement a singleton class
Yes, this is commonly used in the Singleton pattern where the object is accessed through a static member function.
A private constructor is commonly used with Builder methods, for example in the Named Constructor idiom.
class Point
{
public:
static Point Polar(double, double);
static Point Cartesian(double, double);
private:
Point(double,double);
};
In this (typical) example, the Named Constructor idiom is used to make it explicitly which coordinate system is used to build the Point
object.