Which cast in mutiple inheritance?

Discussion in 'C++' started by Pierre Couderc, Sep 7, 2004.

  1. 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;
    }
    Pierre Couderc, Sep 7, 2004
    #1
    1. Advertising

  2. "Pierre Couderc" <> wrote in message
    news:chjtad$25ap$...
    > 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
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Ivan Vecerina, Sep 7, 2004
    #2
    1. Advertising

  3. Pierre Couderc

    Mark Wright Guest

    One joyful day (Tue, 07 Sep 2004 10:59:57 +0200 to be precise), Pierre
    Couderc <> 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
    -

    ================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
    ===================================================
    Mark Wright, Sep 7, 2004
    #3
  4. 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 Wright wrote:

    > One joyful day (Tue, 07 Sep 2004 10:59:57 +0200 to be precise), Pierre
    > Couderc <> 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
    > -
    >
    > ================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
    > ===================================================
    Pierre Couderc, Sep 7, 2004
    #4
  5. Pierre Couderc

    tom_usenet Guest

    On Tue, 07 Sep 2004 10:59:57 +0200, Pierre Couderc
    <> wrote:

    >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
    tom_usenet, Sep 7, 2004
    #5
    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. Gopal Prabhakaran

    Can Mutiple webapplication - use same session ?

    Gopal Prabhakaran, Jan 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    626
    Jan Tielens
    Jan 23, 2004
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,056
    Dan Pop
    Feb 10, 2004
  3. EvilRix
    Replies:
    8
    Views:
    619
    Martin Dickopp
    Feb 14, 2004
  4. sudhirlko2001
    Replies:
    2
    Views:
    297
    Srini
    Aug 20, 2005
  5. Pavel
    Replies:
    7
    Views:
    512
    Pavel
    Sep 19, 2010
Loading...

Share This Page