Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform casting with multiple inheritance

Tags:

c++

casting

qt

My classes structure is like:

class MethodHelper : public QObject, public IMethodHelper {
public:
    // Stuff
};

Now, I get a pointer to the object:

QObject* someObject = getMethodHelper();

Here, I am extremely sure that someObject is a type of MethodHelper. I somehow want to cast it to IMethodHelper. How should I go about it?

My current thoughts are like QObject -> MethodHelper -> IMethodHelper, like:

QObject* someObject = getMethodHelper();
MethodHelper* myHelper = qobject_cast<MethodHelper*>(someObject);
IMethodHelper* myIHelper = dynamic_cast<IMethodHelper*>(myHelper);

is there a potential flaw in my approach?

like image 810
Rohan Prabhu Avatar asked Dec 10 '22 09:12

Rohan Prabhu


1 Answers

You could do it the way you present, but it isn't necessary. The Below should work fine.

IMethodHelper* myIHelper = dynamic_cast<IMethodHelper*>(someObject);
like image 96
Daniel T. Avatar answered Dec 26 '22 00:12

Daniel T.