Problem type-casting to a derived class

Discussion in 'C++' started by Rui Maciel, May 11, 2010.

  1. Rui Maciel

    Rui Maciel Guest

    Consider that I have the following classes: Base, A, B, Derived

    Class A inherits class Base and class Derived inherits both class A and class B. So, the
    inheritance diagram would be something like (mind the ASCII art)

    Base
    |
    A B
    -------
    |
    Derived


    Let's assume that I get a pointer to an object of type Base. Yet, I need to access attributes which
    are defined in class B.

    The thing is, a dynamic_cast<> from Base to B fails to materialize and a reinterpret_cast<> also
    appears to fail.

    So, is this possible? If so, what's the secret?


    Thanks in advance,
    Rui Maciel
    Rui Maciel, May 11, 2010
    #1
    1. Advertising

  2. Rui Maciel

    Rui Maciel Guest

    Rui Maciel wrote:

    > Consider that I have the following classes: Base, A, B, Derived
    >
    > Class A inherits class Base and class Derived inherits both class A and
    > class B. So, the inheritance diagram would be something like (mind the
    > ASCII art)
    >
    > Base
    > |
    > A B
    > -------
    > |
    > Derived
    >
    >
    > Let's assume that I get a pointer to an object of type Base. Yet, I need
    > to access attributes which are defined in class B.
    >
    > The thing is, a dynamic_cast<> from Base to B fails to materialize and a
    > reinterpret_cast<> also appears to fail.


    As added info, class A and class Derived may be any class from a given set of classes. To be more
    specific, class A is a standard GUI widget, class B is a class intended to store attributes and
    methods which must be common to a set of widgets and class Derived is a customized widget.


    Rui Maciel
    Rui Maciel, May 11, 2010
    #2
    1. Advertising

  3. Rui Maciel

    Ian Collins Guest

    On 05/11/10 01:25 PM, Rui Maciel wrote:
    > Consider that I have the following classes: Base, A, B, Derived
    >
    > Class A inherits class Base and class Derived inherits both class A and class B. So, the
    > inheritance diagram would be something like (mind the ASCII art)
    >
    > Base
    > |
    > A B
    > -------
    > |
    > Derived
    >
    >
    > Let's assume that I get a pointer to an object of type Base. Yet, I need to access attributes which
    > are defined in class B.


    So you have to cast to something derived from B, which in this case
    would be a Derived.

    > The thing is, a dynamic_cast<> from Base to B fails to materialize and a reinterpret_cast<> also
    > appears to fail.


    Obviously, there isn't a polymorphic relationship between Base and B.

    --
    Ian Collins
    Ian Collins, May 11, 2010
    #3
  4. Rui Maciel

    Rui Maciel Guest

    Ian Collins wrote:

    > On 05/11/10 01:25 PM, Rui Maciel wrote:
    >> Consider that I have the following classes: Base, A, B, Derived
    >>
    >> Class A inherits class Base and class Derived inherits both class A and
    >> class B. So, the inheritance diagram would be something like (mind the
    >> ASCII art)
    >>
    >> Base
    >> |
    >> A B
    >> -------
    >> |
    >> Derived
    >>
    >>
    >> Let's assume that I get a pointer to an object of type Base. Yet, I need
    >> to access attributes which are defined in class B.

    >
    > So you have to cast to something derived from B, which in this case
    > would be a Derived.


    No exactly. I need to cast an object of type Base to an object of type B, which is a common base
    class of Derived but which isn't a derived class of Base.


    Rui Maciel
    Rui Maciel, May 11, 2010
    #4
  5. Rui Maciel <> writes:

    > Consider that I have the following classes: Base, A, B, Derived
    >
    > Class A inherits class Base and class Derived inherits both class A
    > and class B. So, the inheritance diagram would be something like
    > (mind the ASCII art)
    >
    > Base
    > |
    > A B
    > -------
    > |
    > Derived
    >
    >
    > Let's assume that I get a pointer to an object of type Base. Yet, I
    > need to access attributes which are defined in class B.
    >
    > The thing is, a dynamic_cast<> from Base to B fails to materialize and
    > a reinterpret_cast<> also appears to fail.


    In what way does the dynamic_cast<B*> fail?

    > So, is this possible? If so, what's the secret?


    10:23:05 Paul Bibbings@JIJOU
    /cygdrive/d/CPPProjects/CLCPP $cat casting_to_derived.cpp // file: casting_to_derived.cpp

    #include <cassert>

    class Base {
    public:
    virtual ~Base() { }
    };

    class A : public Base { };

    class B { };

    class Derived : public A, public B { };

    int main()
    {
    Derived d;
    Base *b_ptr = &d;

    assert(dynamic_cast<B*>(b_ptr));
    }


    10:23:10 Paul Bibbings@JIJOU
    /cygdrive/d/CPPProjects/CLCPP $g++ -static -o casting_to_derived
    casting_to_derived.cpp

    10:23:15 Paul Bibbings@JIJOU
    /cygdrive/d/CPPProjects/CLCPP $./casting_to_derived

    10:23:23 Paul Bibbings@JIJOU
    /cygdrive/d/CPPProjects/CLCPP $

    Regards

    Paul Bibbings
    Paul Bibbings, May 11, 2010
    #5
  6. Paul Bibbings <> writes:

    > Rui Maciel <> writes:
    >
    >> Consider that I have the following classes: Base, A, B, Derived
    >>
    >> Class A inherits class Base and class Derived inherits both class A
    >> and class B. So, the inheritance diagram would be something like
    >> (mind the ASCII art)
    >>
    >> Base
    >> |
    >> A B
    >> -------
    >> |
    >> Derived
    >>
    >>
    >> Let's assume that I get a pointer to an object of type Base. Yet, I
    >> need to access attributes which are defined in class B.
    >>
    >> The thing is, a dynamic_cast<> from Base to B fails to materialize and
    >> a reinterpret_cast<> also appears to fail.

    >
    > In what way does the dynamic_cast<B*> fail?


    That was a stupid thing to ask, of course. What I /meant/ to ask was,
    how are you structuring your classes in an example that illustrates it
    failing?

    Regards

    Paul Bibbings
    Paul Bibbings, May 11, 2010
    #6
  7. Rui Maciel

    Ian Collins Guest

    On 05/11/10 09:00 PM, Rui Maciel wrote:
    > Ian Collins wrote:
    >
    >> On 05/11/10 01:25 PM, Rui Maciel wrote:
    >>> Consider that I have the following classes: Base, A, B, Derived
    >>>
    >>> Class A inherits class Base and class Derived inherits both class A and
    >>> class B. So, the inheritance diagram would be something like (mind the
    >>> ASCII art)
    >>>
    >>> Base
    >>> |
    >>> A B
    >>> -------
    >>> |
    >>> Derived
    >>>
    >>>
    >>> Let's assume that I get a pointer to an object of type Base. Yet, I need
    >>> to access attributes which are defined in class B.

    >>
    >> So you have to cast to something derived from B, which in this case
    >> would be a Derived.

    >
    > No exactly. I need to cast an object of type Base to an object of type B, which is a common base
    > class of Derived but which isn't a derived class of Base.


    Why?

    An "object of type Base" is a base, do you mean a Base pointer or
    reference? If so, what was assigned to said pointer or reference? If
    it was a Derived, just dynamic_cast to a Derived and access the data in
    B through that.

    --
    Ian Collins
    Ian Collins, May 11, 2010
    #7
  8. Rui Maciel

    Rui Maciel Guest

    Rui Maciel wrote:

    > Consider that I have the following classes: Base, A, B, Derived
    >
    > Class A inherits class Base and class Derived inherits both class A and
    > class B. So, the inheritance diagram would be something like (mind the
    > ASCII art)
    >
    > Base
    > |
    > A B
    > -------
    > |
    > Derived

    <snip/>

    Scratch that. I've managed to get it to work. A dynamic_cast<> worked as expected after I've fixed
    a small problem regarding the inheritance of class B, which was being derived by class Derived
    through private inheritance instead of public. Fixing this, the dynamic_cast<> makes it possible to
    pick up a pointer to an object of class Base, dynamic_cast<>ing it to class B and then access class
    B's methods. Nice.


    Thanks for the help.
    Rui Maciel
    Rui Maciel, May 12, 2010
    #8
  9. Rui Maciel

    Rui Maciel Guest

    Paul Bibbings wrote:

    > That was a stupid thing to ask, of course. What I /meant/ to ask was,
    > how are you structuring your classes in an example that illustrates it
    > failing?


    While producing a working example I've managed to find out where I screwed up and proceed to fix it.
    Good stuff.


    Thanks for the help,
    Rui Maciel
    Rui Maciel, May 12, 2010
    #9
  10. Rui Maciel

    Rui Maciel Guest

    Pete Becker wrote:

    > That's why you should post code and not hand-waving descriptions of
    > what you think your code is doing.


    Do you get any pleasure from posting comments that make you sound a tad arrogant?


    Rui Maciel
    Rui Maciel, May 13, 2010
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frederic Rentsch

    Type casting a base class to a derived one?

    Frederic Rentsch, Jan 11, 2007, in forum: Python
    Replies:
    11
    Views:
    1,316
    Chris Mellon
    Jan 24, 2007
  2. Replies:
    4
    Views:
    853
    Ian Collins
    Mar 30, 2007
  3. Replies:
    1
    Views:
    384
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    372
    Victor Bazarov
    May 23, 2007
  5. Alex Shen
    Replies:
    5
    Views:
    407
    Alex Shen
    Oct 30, 2010
Loading...

Share This Page