0

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.

SE Does Not Like Dissent
  • 1,767
  • 3
  • 16
  • 36
  • I suppose protected inheritance is one of those things which exists for completeness but doesn't really have any serious use. I'm sure you could contrive some situation where you build a base class by inheriting protectedly from some component classes, but it's not a common idiom. – Kerrek SB Sep 10 '11 at 12:32
  • I'm currently having inheritance/friend issues, so I'm looking to explore options in order to generate ideas and solutions. – SE Does Not Like Dissent Sep 10 '11 at 12:36
  • This question is too vague, the best answer would be: *when the design dictates it*, or *when needed*... but that is too vague for an answer... It is either that or a long tutorial on inheritance that does not really fit an answer. – David Rodríguez - dribeas Sep 10 '11 at 12:36
  • @SSight3: You should try to explain what you are trying to achieve (I guess this is related to the list/node question), explain what the actual *problem* to solve is, the approach you decided to take, what approaches you discarded and why. With that information you will surely get quite a few good ideas on how to tackle the problem. – David Rodríguez - dribeas Sep 10 '11 at 12:37
  • @David: It is the node/list problem. I am just grasping an understanding on private/protected usage within subclasses to see what way it is possible to protect node from being accessed externally... although I have an idea... – SE Does Not Like Dissent Sep 10 '11 at 12:44
  • 1
    I'm not going to answer this question in detail, because it's a lot of information and very subjective. I suggest you to read [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – orlp Sep 10 '11 at 12:28

3 Answers3

3
  • 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
Community
  • 1
  • 1
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • When inheriting a class, so if you have class Name : public Main (the names are pseudo-names, not actual ones), you have the public typename, which implies you can also have protected and private. I am just wondering in what instance that might apply? – SE Does Not Like Dissent Sep 10 '11 at 12:33
  • @SSight3 oh, I see. http://www.parashift.com/c++-faq-lite/private-inheritance.html – Seth Carnegie Sep 10 '11 at 12:34
2

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.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

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.

Janick Bernet
  • 20,544
  • 2
  • 29
  • 55