Inheritance forms an "is a" relationship.
For example... if Poodle is derived from Dog, it implies that a Poodle
is a Dog:
1 2 3 4 5 6 7 8 9
|
class Dog
{
//...
};
class Poodle : public Dog
{
//...
};
|
Generally the way it works is that base classes are more generic, whereas derived classes get more and more specific the further down the hierarchy you go. Like in this example, we have a generic 'Dog' class... and a more specific 'Poodle' class.
The idea is that actions that are common to ALL dogs can be placed in the generic Dog base class... whereas actions that are specific to Poodles go in the more specific sub class.
C++ facilitates this by allowing a pointer to a derived type to be automatically cast to a pointer to a base type:
1 2 3 4 5 6
|
Poodle fluffy; // fluffy is our poodle.
Poodle* ptr_fluffy = &fluffy; // ptr_fluffy points to our fluffy poodle
Dog* ptr_dog = ptr_fluffy; // <- this is also OK. ptr_fluffy
// points to a Poodle... which means it ALSO points to a Dog... because
// a Poodle "is a" Dog.
|
Coming back to your Qt example:
QLabel *A = new QLabel(this);
This code will work as long as whatever class you're in (ie: the class the 'this' pointer points to) is derived from QObject. Any derived class will work. If a class such as QWindow is derived from QObject, then a pointer to a QWindow is also a pointer to a QObject.... since a QWindow "is a" QObject.