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

Threaded View

  1. #1
    Join Date
    Sep 2001
    Location
    Turkey
    Posts
    173

    multi-inheritance & cast

    Hi,

    I have a strange problem.
    Firstly let me figure out my classes.


    class IQueable //abstract class, i.e. interface
    {
    virtual int GetPriority() = 0; //pure virtual fnc.
    };


    class CQueue()//real class; implemented
    {
    void Enqueue( IQueable* apElem );
    IQueable* Dequeue( IQueable* apElem );
    ...
    };


    class CBaseTask //real base class; implemented
    {
    int GetStatus();
    void SetStatus( int anStat );
    int m_nStatus; //default is initialized 0.
    };

    //real multi-inherited class; implemented
    class CDerivedQueableTask : public CBaseTask, public IQueable
    {
    virtual int GetPriority(){ return 2; };
    ...
    }

    void main()
    {
    CQueue* pQueue = new CQueue();
    CDerivedQueableTask* pTask = new CDerivedQueableTask();
    pTask->SetStatus( 1 );
    //the above task is deriven from IQueable
    pQueue->Enqueue( pTask );

    CBaseTask* pBT = (CBaseTask*)pQueue->Dequeue();
    //the above base is not deriven from IQueable,
    //but I am sure that it is CDerivedQueableTask which implemets IQueable.
    //it is just a few line above...
    pBT->GetStatus(); //returns some meaningless value; e.g. 701356
    }


    As you see if I cast the dequeued object( CDerivedQueableTask ) to CTask, I get inconsistent result.
    If I change it to the following, it works!
    CDerivedQueableTask* pTask = (CDerivedQueableTask*)pQueue->Dequeue();


    What may be the problem ?
    Last edited by the one; November 13th, 2003 at 08:29 AM.

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