SE Does Not Like Dissent
SE Does Not Like Dissent

Reputation: 1825

C++ inheritance questions

When would you use:

If it's too broad, can a single short example for each question of one instance of where you might use it, as I am just looking for a starting idea.

Upvotes: 0

Views: 621

Answers (3)

Janick Bernet
Janick Bernet

Reputation: 21184

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.

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477000

Scott Meyers explains it this way:

  • class D : public B: "D provides the interface B".

  • class D : private B: "D is implemented using B".

  • (Protected inheritance isn't all that useful.)

A protected constructor/destructor are useful for classes that you wish to use only in the second way, i.e. in order to provide implementations for other classes. In that case, only the derived class needs to call the constructor and destructor, which may thus be protected.

A private constructor means that only the class itself can create instances of itself, which is popular for factory or creator patterns, where a static member function returns a pointer to an instance, but direct construction of class instances is not desired.

Upvotes: 2

Seth Carnegie
Seth Carnegie

Reputation: 75130

  • A private constructor/destructor
  • A protected constructor/destructor
    • I don't know about a protected destructor, but a protected constructor when this is an inner class and you only want the class and/or friend classes/functions and subclasses to be able to instantiate itself.
  • A protected inherited main class
  • A private inherited main class

Upvotes: 3

Related Questions