1
  • I have a class A that I can not edit:
class A{
public:
    int thisCoolFuntion(){
        return 0;
    }
};
  • I want to create a class that uses thisCoolFuntion() (let's call it class B or C)
  • I want the user to be able to create an instance of B or C, but that he doesn't have access to thisCoolFuntion().

  • I thought one solution was to do a private inheritance:

class B : A{
public:
    int member;
    void setBMember(){
    member = thisCoolFuntion();
    }
};
  • I also thought on having a private member of type A:
class C{
    A memberA;
    int member;
public:
    void setCMember(){
        member = memberA.thisCoolFuntion();
    }
};
  • These 2 solutions seem to work:
int main(int argc, const char * argv[]) {
    // insert code here...
    B b;
    b.setBMember();
    //b.thisCoolFuntion(); --> Error!
    C c;
    c.setCMember();
    //c.memberA.thisCoolFuntion(); --> Error!
    return 0;
}

Question: how should I compare theses 2 solutions? how can I choose which one is more appropriated for my project? is one of them faster? or one takes more memory? is one of these more generally recognized by users?

Ivan
  • 1,352
  • 2
  • 13
  • 31
  • 3
    Private inheritance also disables casting, i.e. `A* a = &b` isn't allowed anymore. – Timo May 18 '20 at 08:48
  • 1
    Unsure whether this is really on topic on SO (maybe more on Programmer), hence a comment and not an answer. This is the difference between *composition* and *inheritance*. The best argument is whether on a model point of view class B *is a* A or *has a* A. For example a student *is a* human being (prefere inheritance here), but a car *has a* motor (prefere composition here). The reason should not be technical but what the relation is on a modeling point of view. – Serge Ballesta May 18 '20 at 08:52
  • related: https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance – 463035818_is_not_an_ai May 18 '20 at 08:58
  • [Use private inheritance when you have to](https://isocpp.org/wiki/faq/private-inheritance#priv-inherit-vs-compos) Because private inheritance increases more maintain effort than composition. Also you might have to deal with multiple inheritance in the long run. – Louis Go May 19 '20 at 04:22

1 Answers1

1

These two approaches are called: inheritance and composition.

Since both approaches are solving your problem, the answer to your question should be the design pattern you are targeting for your application.

Inheritance (public) expresses a is-a relationship, while composition expresses a has-a relationship between the two classes.

In case of private inheritance, it's more like derived has a base, which IMHO doesn't sound right, because that's IMHO the work for composition not inheritance of any kind. So, since private inheritance and composition essentially mean same thing logically, which to choose?

You need to decide upon what kind of relationship makes more sense for your application (between class A and class B).

  1. If the answer is B has-a A, then use composition (Your second approach).
  2. If the answer is B is-a A, then use inheritance (Your first approach).

With the example you posted, I'd most certainly go for composition (you can compose more than one thing of the same type) and some style guides (e.g. google c++ style guide) even recommend to never use private inheritance, for reasons similar to what I already written - private inheritance is just a bit confusing.

For example (Composition vs Public-Inheritance),

A best design for a class polygon would use composition because this statement makes sense Polygon has a ordered sequence of Points but not this Polygon is-a ordered sequence of Points. In C++ terms:

class Polygon {
  std::vector<Point> points;
};

While an logic_error is a exception (public inheritance):

struct logic_error : public exception {
};

See this for Difference between Inheritance and Composition.

See this for Private inheritance VS composition.

abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • private inheritance is not "is-a" (that is public inheritance) but rather "has-a". From outside there is not much difference between private inheritance and composition – 463035818_is_not_an_ai May 18 '20 at 08:58