Inherit my own class inherited from QObject
-
Hello and Happy New Year to all and to Qt community,
I Have this class :
//class1.h #include <QObject> #include <QDebug> #include <QPainter> class Class1 : public QObject { Q_OBJECT public: explicit Class1(QPainter *painter=Q_NULLPTR); private: QPaintDevice *m_device; QPainter *m_painter; }; //class1.cpp #include "class1.h" Class1::Class1(QPainter *painter) { m_painter = painter; m_device = m_painter->device(); }
This class correctly run.
Now, I want create a second class which inherit from Class1. So, I try this solution ://class2.h #include <QObject> #include <QPainter> #include "class1.h" class Class1; class Class2 : public Class1 { public: explicit Class2(QPainter *painter=Q_NULLPTR); }; //class2.cpp #include "class2.h" Class2::Class2(QPainter *painter) { }
But the programm crash without message (only "The program stopped suddenly").
I have too try without
explicit Class2(QPainter *painter=Q_NULLPTR);
and without cpp file, but the issue is same.Can you help me ?
Thank you very much in advance.
Charlie
-
The problem is that
Class1
does not work with the default constructor...if painter is null then this line:
m_device = m_painter->device();
crashes your program.Class2
just implicitly calls the default constructor for the base classP.S.
- your classes do not provide the usual
QObject* parent
in the constructor which is almost standard for QObjects - in
Class2
you probably want to include theQ_OBJECT
macro
- your classes do not provide the usual
-
@BjornW said in Inherit my own class inherited from QObject:
This is one reason why I strongly dislike default arguments
imho this is not a problem with default arguments but just a wrong implementation of the constructor. This would crash even with
Class1 *val=new Class1(nullptr);
which is not using the default arguments at all -
@VRonin
Well, he would not have been able to do it wrong if it weren't for the default argument. The compiler would have caught it.EDIT: To clarify, he would not have been able to implement the Class2 constructor in this way if it weren't for the default argument in Class1 constructor.
I've had problems with default arugments before, for example, let's say I have this class:
class MyClass { public: MyClass(MyClass* pParent = 0); ... } ... //Somewhere deep in my program... auto p1 = new MyClass(0); //1 has no parent auto p2 = new MyClass(p1); //2 has parent: 1
Now I make changes to my class, like so:
class MyClass { public: MyClass(MyClass* pBuddy, MyClass* pParent = 0); } ... //Somewhere deep in my program... auto p1 = new MyClass(0); //1 has no buddy and no parent auto p2 = new MyClass(p1); //2 has buddy: 1 and no parent
Suddenly, my program does not work because the parent-child relationship broke. The compiler would have caught this and prompted me to add the correct arguments, if it wasn't for the default argument.
Anyway, I digress. Careful with your constructors! :-D
-
@BjornW, I am new to Qt, and am still getting used to the
nullptr
(sorry no0
) default-value ctors. I attempt to do exactly like you - in my classes that derive fromQClasses
have no default args. In fact in my pure C++ classes, I leave no default ctor around. So I definitely have all the 5 ctors declared, of course some with the new= delete
syntax.I thought they (Qt-architects) must have had a good reason for this
nullptr
-default-value-idiom, but nevertheless I follow my own strict rules. Hopefully these different idioms play well together :-)