-1

Possible Duplicate:
Prefer composition over inheritance?

A general question which I stumbled upon several times recently: What are the advantages and disadvantages of deriving as opposed to containing?

To be more specific:

class A : public std::vector<int> {};

or

class B {public: std::vector<int> elem;}

?

Of course in a production code I would declare elem as private and implement public getters and setters.

Is there a general recommendation?

Community
  • 1
  • 1
steffen
  • 8,572
  • 11
  • 52
  • 90
  • 5
    Depends on what you need and what's the logic in the concrete case. I don't think someone could answer this "in general". – Kiril Kirov Jul 10 '12 at 07:55
  • 4
    Given any `T` that doesn't have a virtual destructor, `struct D : T {}; T* x = new D; delete x;` is undefined behavior. Most of the classes in the standard library don't have a virtual destructor. Do you mean in general or with standard library classes? – GManNickG Jul 10 '12 at 07:57
  • 1
    Also near-duplicates or strongly related: http://stackoverflow.com/questions/453738/inheritance-or-composition-rely-on-is-a-and-has-a, http://stackoverflow.com/questions/7209019/private-inheritance-vs-composition-when-to-use-which – jogojapan Jul 10 '12 at 08:03
  • my bad: didn't find the duplicates on my search... (yes I did search ;) ) – steffen Jul 10 '12 at 11:08

5 Answers5

3

The general recommendation is to not derive from standard library containers. See also this questions answers.

The first answer to this question gives a note on the Liskov substitution principle which could be your answer.

Substitutability is a principle in object-oriented programming. It states that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may be substituted for objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.).

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
  • From standard library classes - yes, but I think the question is in general, just the example is with `std::vector`. – Kiril Kirov Jul 10 '12 at 07:57
1

Composition means has-a relationship, while inheritance means is-a relationship.

While you should choose the one that better reflects the relationship, a general recommendation is to use composition.

This Wikipedia page provided some explanation.

There's also a similar SO question here.

Community
  • 1
  • 1
WiSaGaN
  • 46,887
  • 10
  • 54
  • 88
0

Always strive to force minimal coupling onto your clients. The more entities you expose, the more complex client code can bind to your library, and the more difficult it becomes to provide future change and break dependencies.

And typically never derive from any standard library container, which are not meant as base classes.


Relevant references:

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
0

You need to make the choice depending on your current task, needs, requirements and so on.

The question as you put it is too generalized. However, usually, deriving from std classes is not a good idea.

Also, check this.

Community
  • 1
  • 1
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
0

I am not sure if this question is related to deriving from stl classes but generally i would go for deriving if the classes are related and meaning full, take an example of classes Car, engine and Hybrid Car

It makes more sense for a Hybrid Car to derive from a car and engine being inside a car (Containment). In case of inheriting you can make use of the powerful virtual function into use.

Inheritance are normally used if you want to follow Open-Close principle

Jeeva
  • 4,585
  • 2
  • 32
  • 56
  • `I am not sure` -> `A general question which I stumbled upon several times recently: What are the advantages and disadvantages of deriving as opposed to containing` – Sebastian Mach Jul 10 '12 at 08:04