Which cast in mutiple inheritance?

P

Pierre Couderc

In the following example, I want that ABC::f() be called. Is it possible?
Here I have simplified the problem, but the point is that I have to
store the pointer on the ABC object in a pointer on a A object (line [1]).
and when I use it (line [2]), I know that pp points on a AC object but
not on a ABC object.

I have tried many casts (static_cast, dynamic_cast, even
reinterpret_cast) but I do not success.

Is there a pure C++ solution, and else is there a MS VC6++ solution?

Thanks in advance

Pierre Couderc


class A
{
public:
A(){ ;}
virtual ~A(){;}
int dummy;
};


class AB : public virtual A
{
public:
AB() {;}
};


class AC : public virtual A
{
public:
AC() {;}
virtual void f(){;}
};


class ABC : public AB, public AC
{
public:
~ABC(){;}
void f(){println();}
};


int main(int argc, char* argv[])
{
ABC* p= new ABC;
A* pp=p; //[1]
AC* ppp= ????_cast<AC*> (pp); // [2]
ppp->f();

return 0;
}
 
I

Ivan Vecerina

Pierre Couderc said:
In the following example, I want that ABC::f() be called. Is it possible?
Here I have simplified the problem, but the point is that I have to store
the pointer on the ABC object in a pointer on a A object (line [1]).
and when I use it (line [2]), I know that pp points on a AC object but not
on a ABC object.

I have tried many casts (static_cast, dynamic_cast, even reinterpret_cast)
but I do not success.

Is there a pure C++ solution, and else is there a MS VC6++ solution?

Only essential code preserved below:
class A
{
public:
virtual ~A(){;}
}; ....
class AB : public virtual A ....
class AC : public virtual A
{
public:
virtual void f(){;}
}; ....
class ABC : public AB, public AC
{
public:
~ABC(){;}
void f(){println();}
};


int main(int argc, char* argv[])
{
ABC* p= new ABC;
A* pp=p; //[1]
AC* ppp= ????_cast<AC*> (pp); // [2]

Unless I missed something, dynamic_cast should work
here. In what way did this cast fail?
ppp->f();
-> ok, shall call ABC::f();



Salutations-Ivan
 
M

Mark Wright

One joyful day (Tue, 07 Sep 2004 10:59:57 +0200 to be precise), Pierre
In the following example, I want that ABC::f() be called. Is it possible?
Here I have simplified the problem, but the point is that I have to
store the pointer on the ABC object in a pointer on a A object (line [1]).
and when I use it (line [2]), I know that pp points on a AC object but
not on a ABC object.

I have tried many casts (static_cast, dynamic_cast, even
reinterpret_cast) but I do not success.

Is there a pure C++ solution, and else is there a MS VC6++ solution?
<...>

Please define success. Other than the println() it compiles fine on VC++
6 using dynamic_cast<AC *>. Do you have RTTI enabled in the project
options?

Also, running the code shows that the call to f() does indeed call
ABC::f() as it should.

Mark Wright
- (e-mail address removed)

================Today's Thought====================
"In places where books are burned, one day,
people will be burned" - Heinrich Heine, Germany -
100 years later, Hitler proved him right
===================================================
 
P

Pierre Couderc

That was the problem. RTTI was not enabled.
Mmm, I am glad that my problem was a MSVC problem and not a pure C++
problem...

Thank you all.
Pierre Couderc

Mark said:
One joyful day (Tue, 07 Sep 2004 10:59:57 +0200 to be precise), Pierre
Couderc <[email protected]> decided that the Usenet community
would benefit from this remarkable comment:

In the following example, I want that ABC::f() be called. Is it possible?
Here I have simplified the problem, but the point is that I have to
store the pointer on the ABC object in a pointer on a A object (line [1]).
and when I use it (line [2]), I know that pp points on a AC object but
not on a ABC object.

I have tried many casts (static_cast, dynamic_cast, even
reinterpret_cast) but I do not success.

Is there a pure C++ solution, and else is there a MS VC6++ solution?

<...>

Please define success. Other than the println() it compiles fine on VC++
6 using dynamic_cast<AC *>. Do you have RTTI enabled in the project
options?

Also, running the code shows that the call to f() does indeed call
ABC::f() as it should.

Mark Wright
- (e-mail address removed)

================Today's Thought====================
"In places where books are burned, one day,
people will be burned" - Heinrich Heine, Germany -
100 years later, Hitler proved him right
===================================================
 
T

tom_usenet

In the following example, I want that ABC::f() be called. Is it possible?
Here I have simplified the problem, but the point is that I have to
store the pointer on the ABC object in a pointer on a A object (line [1]).
and when I use it (line [2]), I know that pp points on a AC object but
not on a ABC object.

I have tried many casts (static_cast, dynamic_cast, even
reinterpret_cast) but I do not success.

dynamic_cast is the correct cast to use when casting down a virtual
heirarchy.
Is there a pure C++ solution, and else is there a MS VC6++ solution?

Have you got RTTI enabled? If so, it should work as is.

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

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top