Inheritance – How to use a superclass constructor in Qt Creator?

You may know that the classes in Qt Creator are part of the class.h and class.cpp files. Suppose we have two classes A (ah, a.cp​​p) and B(bh,b.cpp), B inherits A. When using B’s constructor, how to use A’s constructor?
The constructor is automatically called when constructing B.

If you need to pass parameters Give A’s constructor to make it work, please call it explicitly in B’s constructor:

B::B()
:A( Blah)
{
}

This will be common when A is a QObject and you want the ownership content to happen correctly, you will pass in the parent pointer in the constructor of B And pass it to A:

B::B(QObject* parent_)
:A(parent_)
{
}< /pre>

This has nothing to do with Qt, it is a pure C concept.

As you may know, the classes in Qt Creator are class.h and class.cpp files Suppose we have two classes A (ah, a.cp​​p) and B (bh, b.cpp), B inherits A. When using B's constructor, how to use A's constructor?

The constructor is automatically called when constructing B.

If you need to pass parameters to the constructor of A to make it work, please Call it explicitly in the constructor of B:

B::B()
:A(Blah)
{
}

It will be common when A is a QObject and you want the ownership content to happen correctly, you will pass in the parent pointer in the constructor of B and pass it to A:

< p>

B::B(QObject* parent_)
:A(parent_)
{
}

This has nothing to do with Qt, it is Pure C concept.

Leave a Comment

Your email address will not be published.