 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Tue May 15, 2007 12:41 am Post subject: Casting from a virtual base class to a parent in a diamond |
|
|
class a {};
class b : public a {};
class c : public a {};
class d : public virtual b, public virtual c {};
a * ptr = 0;
d * ptr2 = static_cast<d *>(ptr);
d * ptr3 = static_cast<d *>(static_cast<b *>(ptr));
If I have a pointer to class a, how do I turn that into a pointer to
class d? I get an ambiguity error for the ptr2 conversion, and I get
an implied error for the ptr3 conversion. Compiler is MSVC++ 8.0.5.
Thanks!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Sebastian Redl Guest
|
Posted: Tue May 15, 2007 4:52 am Post subject: Re: Casting from a virtual base class to a parent in a diamo |
|
|
On Mon, 14 May 2007 lindahlb (AT) hotmail (DOT) com wrote:
| Quote: | If I have a pointer to class a, how do I turn that into a pointer to
class d? I get an ambiguity error for the ptr2 conversion, and I get
an implied error for the ptr3 conversion. Compiler is MSVC++ 8.0.5.
|
What's an implied error?
Anyway. 5.2.9/9 says:
-----
An rvalue of type "pointer to cv1 B," where B is a class type, can be
converted to an rvalue of type "pointer to cv2 D," where D is a class
derived from B, if a valid standard conversion from "pointer to D" to
"pointer to B" exists, cv2 is the same cv-qualification as, or greater
cv-qualification than, cv1, and B is neither a virtual base class of D nor
a base class of a virtual base class of D.
-----
The last clause is what prevents your cast. The reason is that the offset
from an object and one of its virtual bases (or a subobject thereof) is
not known at compile-time.
Sebastian Redl
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Tue May 15, 2007 4:52 am Post subject: Re: Casting from a virtual base class to a parent in a diamo |
|
|
lindahlb (AT) hotmail (DOT) com wrote:
| Quote: | class a {};
class b : public a {};
class c : public a {};
class d : public virtual b, public virtual c {};
|
This is a suspicious usage of virtual inheritance.
To make the "diamond" hierarchy, it's the class to be shared
that needs to be virtually inherited; in this case, class a.
class a {};
class b : public virtual a {};
class c : public virtual a {};
class d : public b, public c {};
| Quote: | a * ptr = 0;
d * ptr2 = static_cast<d *>(ptr);
d * ptr3 = static_cast<d *>(static_cast<b *>(ptr));
If I have a pointer to class a, how do I turn that into a pointer to
class d? I get an ambiguity error for the ptr2 conversion, and I get
an implied error for the ptr3 conversion. Compiler is MSVC++ 8.0.5.
|
Given a pointer to a virtual base, you don't know the exact layout of
the object statically. Make class a polymorphic (by adding a virtual
function), and use dynamic_cast.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|