2

I use 3-layer inheritance design below:

class connect_info {
  // these members
};

class vertex : public connect_info {
  // ...
};

// user-defined struct
class algo_vertex: public vertex {
  // ...
};

members of connect_info class(I call it these members in this question) is only used in vertex class. But to keep the semantic of vertex class clear, I must separate these members to another base class(connect_info).

Problems generate here:

  1. how can I hide these members from user-defined class? (protected and private are both useless now. If there is no connect_info base class, private can work well)
  2. Does multi-layer inheritance design make sense in any situation?
  3. Can virtual de-constructor function work well in multi-layer inheritance case?
xunzhang
  • 2,838
  • 6
  • 27
  • 44

3 Answers3

5

You might need to move to has-a relationship, where connect_info can be an internal class(A class inside class) and make it private, if you want to hide connect_info members in user defined class.

class vertex {
  // ...
  private:
     class connect_info{/*these members*/};
};

class algo_vertex : public vertex{ 
  // connect_info members no longer accessible,
  // unless you provide member functions in `vertex` to access it.
};
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • What is the difference between internal class and privately inheritance in this case? – xunzhang Feb 07 '14 at 09:58
  • Keeping things simple is the idea here @xunzhang, you have more control over who gets to use what in `connect_info` class. – Aniket Inge Feb 07 '14 at 09:59
  • 1
    @xunzhang the difference is also a semantic one (which seemed to be your interest if I read your question correctly) : Does a vertex have a connect_info member or is a vertex a specialized version of a connect_info ? Do not use inheritance when "A is a special version of B" doesn't make sense. – Félix Cantournet Feb 07 '14 at 10:09
  • @Aniket with internal class, how can I use these members? – xunzhang Feb 07 '14 at 16:12
1

Inheritance is introducing strong coupling between classes, and is generally avoided. Instead, people are using composition. Read Prefer composition over inheritance question and answers.

In your specific example, what do you do, when you add algo_B_vertex class, where some of fields and methods from Vertex make no sense. Or in worse case scenario connect_info. Then you get into all sorts of problems. Not to mention complexity of several layers of inheritance.

how can I hide these members from user-defined class?

By using composition, and creating objects in private section.

Does multi-layer inheritance design make sense in any situation?

Of course it does. Fortunately that number of such situations is little. General advice is to think twice before jumping into multiple inheritance.

Community
  • 1
  • 1
BЈовић
  • 62,405
  • 41
  • 173
  • 273
0

A good trade-of here would be to make vertexinherit privately from connect_info. The big difference with composition is that within the class vertex you can consider yourself being a connect_info, which means you will be able to access these members by doing this->member instead of connect_info_attribute.member. Also you won't need any friend or encapsulation hacks.

To sum it up, from the point of view of the child class, private inheritance means i consider myself being a "parent" but i don't want anyone else (not even my children) to consider me like one.

Drax
  • 12,682
  • 7
  • 45
  • 85