Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Inheritance is one of the greatest things of OOP, if used correctly.</p> <p>A "subclass", in all good OO designs, has to obey a simple rule: IS the child a KIND OF parent? That is usually called, in OOP literature, a "is a" relationship. And more important: the child has always to do two things: <em>specialize</em> a generic behavior, or <em>extend</em> the functionality of the father. I consider it a code smell when a subclass does neither.</p> <p>That said, your decision has nothing to do with Qt, or with what's programatically better or worse. It should make sense.</p> <p>An example: If you had a QLabel that had to show the score of a game, and only that, it could be a good idea to do something like</p> <pre><code>class MyScoreBoard : public QLabel { private: int scoreP1; int scoreP2; Game *_g; public: MyScoreBoard(QWidget *parent = 0) : QLabel(parent) { scoreP1 = 0; scoreP2 = 0; connect(_g, SIGNAL(scoreChanged(int,int)), this, SLOT(updateScore(int,int))); } public slot: updateScore(int a, int b) { scoreP1 = a; scoreP2 = b; this-&gt;setText(QString::number(scoreP1) + "x" + QString::number(scoreP2)); }; }; </code></pre> <p>On the other hand, if your scoreboard had some lights on top of it, that should blink whenever the score had changed, if it had one label for each player, that had to change its color depending on the score, then it would be better to create a ScoreBoard class that HAD two labels, HAD two lights, and then implement the intended behavior.</p> <p>Bottom line is: inherit if it makes sense on your design</p> <p><a href="http://en.wikipedia.org/wiki/Yo-yo_problem" rel="nofollow">Wikipedia has a good small article about an anti-pattern that appears when inheritance is used without care.</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload