C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Casting from a virtual base class to a parent in a diamond

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Tue May 15, 2007 12:41 am    Post subject: Casting from a virtual base class to a parent in a diamond Reply with quote



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





PostPosted: Tue May 15, 2007 4:52 am    Post subject: Re: Casting from a virtual base class to a parent in a diamo Reply with quote



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





PostPosted: Tue May 15, 2007 4:52 am    Post subject: Re: Casting from a virtual base class to a parent in a diamo Reply with quote



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
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group