multiple inheritance casting problems
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: multiple inheritance casting problems

  1. #1
    Join Date
    Nov 2003
    Location
    London
    Posts
    35

    multiple inheritance casting problems

    Hi Guys,

    have programmed myself into a corner!

    /////////////////////////////////////////////////////////////////

    Struct Left
    {
    virtual void LeftFunc() = 0;
    };
    typedef vector<Left*> LeftVector;

    Struct Right
    {
    virtual void RightFunc() = 0;
    };
    typedef vector<Right*> RightVector;

    Struct Concrete1
    : public Left, public Right
    {
    virtual void LeftFunc() {...}
    virtual void RightFunc() {...}
    };

    Struct Concrete2
    : public Left, public Right
    {
    virtual void LeftFunc() {...}
    virtual void RightFunc() {...}
    };

    // I need to store objects as type Left*
    LeftVector lv;
    lv.push_back(new Concrete1());
    lv.push_back(new Concrete2());

    // I have another vector from which I need to use the Right methods from these objects
    // I need to be able to do the following say
    for ( i = 0; i < rv.size(); ++i )
    rv[i]->RightFunc();

    // How do I put them in?
    Left * c1 = lv[0];
    Left * c2 = lv[1];
    RightVector rv;
    rv.push_back( ??c1?? );
    rv.push_back( ??c2?? );


    /////////////////////////////////////////////////////////////////

    I have tried
    rv.push_back( reinterpret_cast<Right*>(c1) );
    rv.push_back( reinterpret_cast<Right*>(c2) );

    This compiles but crashes as if the functions are not being found properly in the class.
    Left has to be a base class in its own right, ie. it can't derive from Right which would solve the problem easily.

    Any help very much appreciated.
    Many thanks,
    LJHW

  2. #2
    Join Date
    Nov 2003
    Location
    London
    Posts
    35

    Sorry Guys!

    Think I must be a bit tired and over-worked.
    It's as simple as

    // How do I put them in?
    Left * c1 = lv[0];
    Left * c2 = lv[1];
    RightVector rv;
    rv.push_back( dynamic_cast<Right*>(c1) );
    rv.push_back( dynamic_cast<Right*>(c2) );

    Cheers anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center