A private constructor/destructor?
Private constructor/destructors makes sense for any kind of class who's object instances should be managed by some other manager class (which for instance has a list of all the instances of the class it manages). An example:
class AManager;
class A {
private:
friend class AManager;
A() {};
~A() {};
}
class AManager {
public:
A* createA() { a = new A(); aList.add(a); return a; }
void destroy(A* a) { aList.remove(a); delete a; }
private:
list<A> aList;
}
A protected constructor/destructor?
If you only want subclasses of your class being created (for instance if your class is just an abstract class, but has no pure virtual methods, so it could theoretically be instantiated had it public constructors):
class A {
protected:
A() {};
~A() {};
}
class A1: public A {
public:
A1() {}
}
class A2: public A {
public:
A2() {}
}
This makes most sense as part of the factory pattern:
class AFactory {
public:
A* createA() {
if(someCondition()) return new A1();
else return new A2();
}
void destroyA(A* a) { delete a; }
private:
bool someCondition() { /*...*/ }
}
The create
/destroy
methods could also be static methods of the A
base class, but then it becomes a bit more complicated due to the need of forward declarations. Also, as an alternative, the A1
and A2
constructors could remain protected and AFactory
be a friend of A1
and A2
.
A protected inherited main class?
A private inherited main class?
What you mean by Main
class? In any case, non-public inheritance is very similar to aggregation, so if there is not a very specific reason not to, aggregation should be preferred to private/protected inheritance.