"has-a"-Beziehung



  • Guten Abend

    Also, "is-a" -Beziehung bedeutet ja public-Vererbung.
    Bedeutet private-Vererbung "has-a" -Beziehung? Ist also 1. und 2. praktisch das Gleiche?

    class Bar : private Foo // 1.
    {
    };
    
    class Bar
    {
        private:
            Foo f; // 2.
    }
    


  • http://msdn.microsoft.com/de-de/library/27db6csx(v=vs.80).aspx

    Unter der zweiten Überschrift

    Edit: glaub, es ist doch nicht ganz was du suchst... 🤡



  • Um das mal gleich zu sagen: Das sind keine allgemeingültigen Regeln, sondern eher Orientierungshilfen. Aber eine has-a Beziehung sieht wohl eher nach einer Membervariable aus?



  • Da steht: Die Vererbungshierarchie stellt eine "ist-Beziehung" und keine "hat-Beziehung" dar.



  • Ein Supermarkt hat eine Wurst, aber eine Wurst ist kein Supermarkt. 🤡



  • Private Vererbung kann auch "ist implementiert mit hilfe von" heißen.



  • Ethon schrieb:

    Ein Supermarkt hat eine Wurst, aber eine Wurst ist kein Supermarkt. 🤡

    Und C-Strings sind toll. 🤡



  • Hacker schrieb:

    Ethon schrieb:

    Ein Supermarkt hat eine Wurst, aber eine Wurst ist kein Supermarkt. 🤡

    Und C-Strings sind toll. 🤡

    Andere Baustelle, die machen durchaus in den passenden Situationen Sinn. 😉
    Jürgens Vererbungsbeispiele aber nicht.



  • Ich zitiere mal aus Effective C++:

    Private inheritance means is-implemented-in-terms-of. If you make a class D privately inherit from a class B, you do so because you are interested in taking advantage of some of the features available in class B, not because there is any conceptual relationship between objects of types B and D. As such, private inheritance is purely an implementation technique. (...) Private inheritance means nothing during software design, only during software implementation.

    (...)

    The fact that private inheritance means is-implemented-in-terms-of is a little disturbing, because Item 38 points out that composition can mean the same thing. How are you supposed to choose between them? The answer is simple: use composition whenever you can, and use private inheritance whenever you must. When must you? Primarily when protected members and/or virtual functions enter the picture, (...)



  • Ethon schrieb:

    Hacker schrieb:

    Ethon schrieb:

    Ein Supermarkt hat eine Wurst, aber eine Wurst ist kein Supermarkt. 🤡

    Und C-Strings sind toll. 🤡

    Andere Baustelle, die machen durchaus in den passenden Situationen Sinn. 😉
    Jürgens Vererbungsbeispiele aber nicht.

    Haha, klar! Aber doch garantiert nich in allen! (Wenn ich schlau bin, doch nur wenn es um Geschwindigkeit geht (oder man gezwungen ist)?



  • seldon schrieb:

    Private inheritance means is-implemented-in-terms-of. If you make a class D privately inherit from a class B, you do so because you are interested in taking advantage of some of the features available in class B, not because there is any conceptual relationship between objects of types B and D.

    How are you supposed to choose between them? The answer is simple: use composition whenever you can, and use private inheritance whenever you must. When must you? Primarily when protected members and/or virtual functions enter the picture, (...)

    Danke dir seldon, damit ist meine Frage mit "nein" zu beantworten.


Anmelden zum Antworten