Casting from a virtual base class to a parent in a diamond inheritance? - c++

This is a discussion on Casting from a virtual base class to a parent in a diamond inheritance? - c++ ; 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 = ...

+ Reply to Thread
Results 1 to 5 of 5

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

  1. Default Casting from a virtual base class to a parent in a diamond inheritance?

    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! ]


  2. Default Re: Casting from a virtual base class to a parent in a diamond inheritance?

    On Mon, 14 May 2007 lindahlb@hotmail.com wrote:

    > 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! ]


  3. Default Re: Casting from a virtual base class to a parent in a diamond inheritance?

    lindahlb@hotmail.com wrote:
    > 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 {};

    > 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! ]


  4. Default Re: Casting from a virtual base class to a parent in a diamond inheritance?

    lindahlb@hotmail.com wrote:
    > 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.


    Put a virtual function in a and use dynamic_cast.

    >


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  5. Default Re: Casting from a virtual base class to a parent in a diamond inheritance?

    On May 15, 8:41 am, linda...@hotmail.com wrote:
    > 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!
    >


    class a {};
    class b : public a {};
    class c : public a {};
    class d : public virtual b, public virtual c {};
    The structure seems not to be a diamond inheritance.
    It may be look like:
    class a {};
    class b : virtual public a {};
    class c : virtual public a {};
    class d : public virtual b, public c {};
    do you mean?








    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


+ Reply to Thread

Similar Threads

  1. Casting base class to inherited one
    By Application Development in forum DOTNET
    Replies: 19
    Last Post: 08-08-2007, 06:59 AM
  2. Re: Casting base class to inherited one
    By Application Development in forum CSharp
    Replies: 3
    Last Post: 08-08-2007, 06:59 AM
  3. Re: Casting base class to inherited one
    By Application Development in forum CSharp
    Replies: 6
    Last Post: 08-01-2007, 11:54 AM
  4. Replies: 2
    Last Post: 06-03-2007, 10:00 AM
  5. Replies: 1
    Last Post: 05-31-2007, 08:16 AM