Important: Please read the Qt Code of Conduct -

[solved]Parents and inheritance

  • Hi everyone, I need some help.

    I'm a newbie yet, and I need do a class with a base class create by me, I need it get some variables, methods and slots of other class(its parent)...So, how I can do this.

    Sorry if I'm not being clear, I don't know how describe.

  • Moderators

    Well, not terribly clear, I agree, but I'll try to guess.

    There are 2 basic ways to get some stuff from another class in c++: inheritance and friend classes. I'll focus on inheritance.

    Let's say you have a class:
    class Class1 {
    void doSomeStuff();
    int variable1;

    To get access to members of Class1 in another class, all you need to do is:
    Class2 : public Class1 {
    void doSomeOtherStuff();

    That's it. Now, if you want to get another class to participate, it's still possible in c++ (but not in many other languages):
    Class3 : public Class2, QObject {
    // even more stuff!

    Class3 will get members from both Class2 and QObject. Usually it's better not to get too much into multiple inheritance.

    Hope I guessed what you did not understand. If not, feel free to ask more.

  • Thank you, I understand

  • Moderators

    Just one more thing. Since this is Qt Project forums, I should add that "QObject *parent" has nothing to do with this. It's a separate hierarchy (of meta-objects) that allows for some cool additional features.

  • Note that the inheritance with Class3 will not work as described. At least, QObject must be first, however, I would not recommend multiple inheritance as an approach to be taken lightly (with the exception of interfaces, perhaps).

    Furthermore, I really miss a discussion of using encapsulation instead of inheritance. It is just as important a principle, if you ask me. It is about the difference between an is-a and a has-a relationship between classes.

    For gaining access to members of another class, I would first consider using proper access methods instead of resorting to friends. Friends can (and IMHO should) usually be avoided.

  • @Andre can you tell me more about encapsulation?

  • Any good C++ book will tell you more about it.

    The idea is that in order to build something, you can build it out of smaller parts. The bigger class doesn't behave like any of its parts individually, but forms a new entity with its own properties. However, it is composed of (has) the smaller parts.

    In Qt, a nice example is the QComboBox widget. In its editable mode, it is composed of a QLineEdit, a button* and when extended, a QListView. Yet it is none of these. It does not inherit them, it has them. The API even provides direct access to these components in this case. QComboBox still uses inheritance though: QComboBox is a QWidget (it inherits QWidget), just like the objects it is composed of do too.

    So, in order to use properties of a class, there are more ways than inheriting. Only if it makes sense to think of the relation between your base class and your extended class in terms of an is-a relation should you considder inheritance. It is meaningful to think of a QComboBox being a QWidget, and you can handle a QComboBox using only its QWidget interface. QComboBox is not a QListWidget (and a QLineEdit, and a QPushButton) however: it has those. It is composed of these.

    *) QComboBox, from the top of my head, does not actually use a QPushButton for the button, but that is not relevant for the example.

  • I fully agree with Andre that you should carefully consider if you really need inheritance (is-a relationship), simple access methods (encapsulation), or composition (has-a relationship). Deciding if inheritance is really needed is very important and not always very clear and can have a great impact on your design.

    For an excellent explanation on these concepts, see I also encourage you to read all related sections (inheritance, aggregation, encapsulation).

  • I understand, I think I need learn too much

Log in to reply