CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 16

Threaded View

  1. #1
    Join Date
    Sep 2004
    Posts
    561

    Multiple Inheritance Problem

    There was a bug that stumped me for a while and I'd like to know the exact reason why this is happening.
    I'm going to simplify the problem here.

    Code:
    class Base1 - Concrete class
    {
    virtual void DoSomething1(){}
    }
     
    class Base2 - Abstract class
    {
    virtual void DoSomething2()=0;
    }
     
    class Derived : Base1, Base2
    {
    void DoSomething1(){}
    void DoSomething2(){}
    }
    When I create a new instance of Derived and call DoSomething2(), the function that gets invoked is DoSomething1()

    However, here is what the root cause appears to be.

    EDIT: Example was slightly off we are casting back to Base2.
    Code:
     
    Derived *d = new Derived();
    DWORD dPtr = (DWORD)d;
    Base2 *b2 = (Base2*) dPtr;
    b2->DoSomething2(); //DoSomething1() is called
    How come casting to a DWORD and then casting back to Derived* screws things up?
    Last edited by Rigel; May 2nd, 2008 at 08:56 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured