How to solve the problem of inherits multi super classes
-
//DEFINED THE INTERFACE OF THE CLASS class ICallback{ private: ICallback * m_callback; public: void setCallback(ICallback * ptr){ m_callback = ptr; } ICallback * getCallback(){ return m_callback; } virtual void callback(void * ptr){ ... } QString dosomething(){ ... //return the marked string of the ICallback ... } } // class ClassA :public QPushButton,public ICallback{ Q_OBJECT public: virtual void callback(void * ptr) override{ ... //do some thing marked the ClassA for dosomething() could get a value of the ClassA ... ICallback * t_callback = getCallback(); //here is the problem,t_callback->callback called but the pointer of this seemed cant find the real object t_callback->callback(this); } } class ClassB :public QWidget,public ICallback{ Q_OBJECT public: virtual void callback(void * ptr) override{ ... QObject * t_obj = (QObject*)ptr; // this code could run normally t_obj->objectName(); // ClassA * t_classa = (ClassA*)ptr; //this is the problem,the t_classa t_classa->dosomething(); } } //usage : ClassA * ptrA = new ClassA(); ClassB * ptrB = new ClassB(); ptrA->setCallback(ptrB); //here is the problem, the ClassB callback(){} could not run the code t_classa->dosomething(),caused the pointer ptrA->callback(); -
//DEFINED THE INTERFACE OF THE CLASS class ICallback{ private: ICallback * m_callback; public: void setCallback(ICallback * ptr){ m_callback = ptr; } ICallback * getCallback(){ return m_callback; } virtual void callback(void * ptr){ ... } QString dosomething(){ ... //return the marked string of the ICallback ... } } // class ClassA :public QPushButton,public ICallback{ Q_OBJECT public: virtual void callback(void * ptr) override{ ... //do some thing marked the ClassA for dosomething() could get a value of the ClassA ... ICallback * t_callback = getCallback(); //here is the problem,t_callback->callback called but the pointer of this seemed cant find the real object t_callback->callback(this); } } class ClassB :public QWidget,public ICallback{ Q_OBJECT public: virtual void callback(void * ptr) override{ ... QObject * t_obj = (QObject*)ptr; // this code could run normally t_obj->objectName(); // ClassA * t_classa = (ClassA*)ptr; //this is the problem,the t_classa t_classa->dosomething(); } } //usage : ClassA * ptrA = new ClassA(); ClassB * ptrB = new ClassB(); ptrA->setCallback(ptrB); //here is the problem, the ClassB callback(){} could not run the code t_classa->dosomething(),caused the pointer ptrA->callback();Can you please describe your problem and what you're trying to achieve. Your code above does not tell me anything.
-
Can you please describe your problem and what you're trying to achieve. Your code above does not tell me anything.
class ClassB virtual void callback(void * ptr) override{ //this is the problem,the t_classa dosomething() dosnt work. I trans the pointer of the void to the ClassA,But it failed t_classa->dosomething(); } -
class ClassB virtual void callback(void * ptr) override{ //this is the problem,the t_classa dosomething() dosnt work. I trans the pointer of the void to the ClassA,But it failed t_classa->dosomething(); }@nicker-player "failed" how? You aren't actually explaining what problem you are having, or what you are trying to accomplish.
-
class ClassB virtual void callback(void * ptr) override{ //this is the problem,the t_classa dosomething() dosnt work. I trans the pointer of the void to the ClassA,But it failed t_classa->dosomething(); }@nicker-player first of all, for the love of the c++ gods, don't use c-style casts, use dynamic cast here, because I'm betting my last pair of underwear that you did not disable RTTI. So it is available to you.
post what you actually do in "do something"
also I fail to see your initialisation of
ICallbackwhere do you do that ? -
Your
callbackinsideClassBcasts the pointerptrit receives toClassA. For a callback I would expect it to be called with itself as parameter. So, ifptris of typeClassByou are not allowed to cast it toClassAbecause those are not related at all. Isptrof typeClassAin your case? -
How to solve the problem of inherits multi super classes
In computer science texts this is called the "diamond problem". There are countless online discussions about it. It's not specifically a Qt problem, but a design paradigm challenge.