[SOLVED] [Class Inheritance] Need tips on creating a hierarchic class structure
-
wrote on 22 Jun 2011, 14:12 last edited by
You guessed right, but I don't understand what you mean.
-
wrote on 22 Jun 2011, 14:20 last edited by
@
class testroot : public QWidget
{
Q_OBJECT
public:
testroot(QWidget *parent = 0) : QWidget(parent)
{}
}CQp::CQp(QWidget *parent) :
testroot(parent)
{
}
@ -
wrote on 22 Jun 2011, 14:30 last edited by
Thanks!
Do you have any good reference book that talks about this kind of inheritance? My code now compiles, but I haven't the slightest idea why. I read about inheritance before, but it's always demonstrated in simple cases, and rarely with abstract classes.
Thanks, @loladiro!
-
wrote on 22 Jun 2011, 14:34 last edited by
[quote author="Joey Dumont" date="1308751406"]
and when I try to compile, it complains that 'QWidget' is not a direct base of CQp. True. But I if make it a parent class to CQp explicitly, it complains that class CQp inherits QWidget from two places. Is there a way around that?[/quote]What the compiler is telling you is that from a constructor you can call only direct base constructors.
That is, if you have an inheritance chain like this
@
A --> B --> C
@
(C inherits from B, B from A) then from C::C() you can call B::B(), but not A::A(), because it's not a direct base for C.The offending line is obviously the call to the QWidget ctor here, since "testcase" is a direct base for CQp, and QWidget is a non-direct base:
[quote author="Joey Dumont" date="1308751406"]
@#include "cqp.h"CQp::CQp(QWidget *parent) :
QWidget(parent)
{
}
@
[/quote] -
wrote on 22 Jun 2011, 14:36 last edited by
And by using,
@ CQp::CQp(QWidget *parent):
testroot(parent);{
}
@what am I changing to the inheritance chain?
-
wrote on 22 Jun 2011, 14:44 last edited by
You're not changing the inheritance graph (which is defined by the class definition); that's a call to a base constructor from your constructor. You're now calling a costructor of a direct-base (allowed) instead of a non-direct base (forbidden).
-
wrote on 22 Jun 2011, 14:44 last edited by
Nothing, but you are calling the constructor of your base class (which is the only constructor your are allowed to call in C++).
-
wrote on 22 Jun 2011, 14:46 last edited by
Thanks for both your simultaneous answers!
Now, will I be able to instantiate the tests that are at the far end of the inheritance graph by using QWidget signals like show() and such? My guess is no, but if so my abstract classes organization scheme falls apart.
Any thoughts?
-
wrote on 22 Jun 2011, 14:48 last edited by
Yes you will! Signals/Slots are inherited just like methods (because they are methods).
-
wrote on 22 Jun 2011, 14:49 last edited by
Oh, I just got it. Thanks for your patience!
12/12