Problem with Cast

A

AriZOnA

Hi!
I have the following scenario:

class QWidget
{}

class GeneratedClass : public QWidget
{}

class AbstractPanel
{
virtual void AbsMethod() = 0;
}

class MyClass : public GeneratedClass , AbstractPanel
{
void AbsMethod() {;} // impl
}


I have the QWidget pointer, how can I cast it so that I can
make a call to the AbsMethod implemented by MyClass ?
thanx a lot ...
bye
 
D

Daniel T.

AriZOnA said:
Hi!
I have the following scenario:

class QWidget
{}

class GeneratedClass : public QWidget
{}

class AbstractPanel
{
virtual void AbsMethod() = 0;
}

class MyClass : public GeneratedClass , AbstractPanel
{
void AbsMethod() {;} // impl
}


I have the QWidget pointer, how can I cast it so that I can
make a call to the AbsMethod implemented by MyClass ?

How do you know that the object in the QWidget pointer you have
implements the AbsMethod?
 
R

Rolf Magnus

AriZOnA said:
Hi!
I have the following scenario:

class QWidget
{}

class GeneratedClass : public QWidget
{}

class AbstractPanel
{
virtual void AbsMethod() = 0;
}

class MyClass : public GeneratedClass , AbstractPanel

Was the private inheritance from AbstractPanel intentional?
{
void AbsMethod() {;} // impl
}


I have the QWidget pointer, how can I cast it so that I can
make a call to the AbsMethod implemented by MyClass ?

I assume QWidget is polymorphic (Well, I know Qt :) ) and the
inheritance from AbstractPanel was supposed to be public.
Use dynamic_cast:

if (MyClass* p = dynamic_cast<MyClass*>(thewidgetpointer))
p->AbsMethod();
else
std::cerr << "your QWidget isn't an instance of MyClass\n";
 
T

tom_usenet

Hi!
I have the following scenario:

class QWidget
{}

class GeneratedClass : public QWidget
{}

class AbstractPanel
{
virtual void AbsMethod() = 0;
}

class MyClass : public GeneratedClass , AbstractPanel
{
void AbsMethod() {;} // impl
}


I have the QWidget pointer, how can I cast it so that I can
make a call to the AbsMethod implemented by MyClass ?

Assuming QWidget has at least 1 virtual function (and surely it has a
virtual destructor), you can do:

if (MyClass* p = dynamic_cast<MyClass*>(qptr))
{
p->AbsMethod();
}

(or dynamic_cast to AbstractPanel instead).

Tom
 
O

Old Wolf

tom_usenet said:
Assuming QWidget has at least 1 virtual function (and surely it has a
virtual destructor), you can do:

if (MyClass* p = dynamic_cast<MyClass*>(qptr))
{
p->AbsMethod();
}

If you expect qptr to always be a MyClass *, you can go:

dynamic_cast<MyClass &>(*qptr).AbsMethod();

which will throw a std::bad_cast exception if qptr was not a
MyClass *.
(or dynamic_cast to AbstractPanel instead).

Is it safe to "sideways" dynamic cast? I've only ever used it
where the cast-to class was a direct descendent of the cast-from class.
 
T

tom_usenet

Is it safe to "sideways" dynamic cast? I've only ever used it
where the cast-to class was a direct descendent of the cast-from class.

Yes, dynamic_cast can be used to cross-cast.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top