开发者

C++ inheritance questions

When would you use:

  • A private constructor/destructor?
  • A protected constructor/destructor开发者_开发技巧?

  • A protected inherited main class?

    class Name : Protected Main

  • A private inherited main class?

    class Name : Private Main

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.


  • A private constructor/destructor
    • See here for private destructors, but a private constructor when you don't want anyone constructing instance of your class.
  • 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
    • Read this article at the C++ FAQ: http://www.parashift.com/c++-faq-lite/private-inheritance.html


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜