Thread: C++ multiple inheritance problem

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    4

    Talking C++ multiple inheritance problem

    I want to use a class composed of all pure virtual functions as like 'interface' in java. but instantiating a new derived class as interface class is not allowed.

    like following code:

    class ISampleProvider{
    virtual CObject getSample() = 0;
    }

    class BaseClass{
    method A...
    method B...
    }

    class A : public BaseClass, public ISampleProvider{
    method A...
    mehtod B...

    // implementation for ISampleProvider
    CObject getSample(){
    return m_thisSample;
    }
    }

    void main(){

    //A *a = new A ; // this expression does not make problems.
    BaseClass *a = new A;
    ISampleProvider *sample =
    dynamic_cast<ISampleProvider*>(a);

    if(sample)
    sample->getSample(); // error occurs
    }
    }

    error occurs just like:

    " The value of ESP was not properly saved accross a function call. his is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."


    Like this, main() can get the sample object if the class implemented ISampleViewable interface, regardless of the type of the class.
    It's a powerful expression of Java, but I think this is not supported in C++ 'cause the typing policy or 'virtual' mechanism of C++
    cannot support it.

    I wanna know whether this expression is not allowed originally in C++ or some another way exists...

    thanx
    Last edited by pongsor; 02-08-2002 at 01:53 AM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    sample.getSample();

    Maybe a -> would help.

    What errormessage pops up ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    4
    I updated the thread I've posted.
    please read again my posting where source and error message.

    thanx for your attention.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    PHP Code:
    #include <afx.h>

    class ISampleProvider

    public:
        
    virtual CObjectgetSample() = 0
    }; 

    class 
    BaseClass

    public:
        
    void Test();
    }; 

    class 
    : public BaseClass, public ISampleProvider

    private:
        
    CObjectm_thisSample;

    public:
        
    CObjectgetSample()
        { 
            return 
    m_thisSample
        } 
    }; 

    int main()


        
    BaseClass *= new A

        
    // you are casting the baseclass to ISampleProvider here
        // though this happens to be ok, make sure it's no problem
        // with the design, because BaseClass has no ISampleProvider
        // interface. Only A has, so you might want to cast it to A 
        // fist, then to ISampleProvider.

        
    ISampleProvider *sample = (ISampleProvider*)a

        
    sample->getSample();

        return 
    0;


    // I changed the CObject to CObject*, because CObject has no public
    // copy constructor or assignment operator and you don't want to 
    // copy the element anyway, that would be horribly inefficient.
    // What you have as object in Java is a pointer to an object in C++. 
    This code compiles fine on VC6SP5.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    4
    Well, I missed it.
    but,
    I'm sorry that It is not my question point.

    My Question is how any classes cast into specific interface class
    in order to extending facilities to a class by implementing it.

    I know your code above will be compiled well, but It does not work well. It will make a null-pointer error. and you can correct the problem by replacing the line 'BaseClass *a = new A' into 'A *a = new A' simply.

    I guess the real why is the OOP mechanism of C++ wouldn't support it. The declaration type (handle) may effect on casting capabilities.

    thanx.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    well, as I mentioned, getting the interface from the baseclass is not possible, only A has this interface. The following works fine:

    PHP Code:
    #include <afx.h>

    class ISampleProvider

    public:
        
    virtual CObjectgetSample() = 0
    }; 

    class 
    BaseClass

    public:
        
    void Test();
    }; 

    class 
    : public BaseClass, public ISampleProvider


    private:
        
    CObjectm_thisSample;

    public:
        
    A()
        {
            
    m_thisSample NULL;
        }

        
    virtual CObjectgetSample()
        { 
            return 
    m_thisSample
        } 
    }; 

    int main()


        
    BaseClass *= new A(); 

        
    // you are casting the baseclass to ISampleProvider here
        // though this happens to be ok, make sure it's no problem
        // with the design, because BaseClass has no ISampleProvider
        // interface. Only A has, so you might want to cast it to A 
        // first, then to ISampleProvider.

        // EDIT: Right, cast it to A first, because Baseclass ( pointer a ) 
        // has NO ISampleProvider Interface. So casting from Baseclass
        // to ISampleProvider will result in errors.

        
    ISampleProvidersample = (ISampleProvider*)(A*)a

        
    printf("%p\n"sample->getSample() );

        return 
    0;


    // this prints out 0x00000000 as expected 
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Unregistered
    Guest

    Smile

    well, Casting into A first is a good way to accomplish it. but, I assume that there's no information 'bout the class type of interface casting at all.

    following UML notation shows this case:

    BaseClass
    ¡è
    A---o ISampleViewable

    you can do it in Java like following:

    class A extends BaseClass implements ISampleViewable{
    ....
    }

    void engine(BaseClass b){
    __...
    __b.methodA();
    __....

    __//if b is a 'Sample providable'--implements ISampleProvider--, then following codes are available

    __ISampleViewable sample = (ISampleViewable)b; //If not allowed, a casting exception will occurs
    __sample.getSample();
    }

    void static main(...){
    __...
    __engine(new A()); // sample viewable (provider)
    __engine(new BaseClass()); // fundamental functions works only
    }


    Probably, C++'s VTable mechanism cannot support the case like Java.

    The Only thing I doubt is 'reinterpret_cast' do this?
    or another way?

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    I'm not entirely sure if this is what you're after, but you could use dynamic cast (as long as you enable RTTI). Extending nvoigts code -

    Code:
    #include <afx.h>
    
    class ISampleProvider
    { 
    public:
        virtual CObject* getSample() = 0; 
    }; 
    
    class BaseClass
    { 
    public:
        virtual void Test(){};
    }; 
    
    class A : public BaseClass, public ISampleProvider
    { 
    
    private:
        CObject* m_thisSample;
    
    public:
        A()
        {
            m_thisSample = NULL;
        }
    
        virtual CObject* getSample()
        { 
            return m_thisSample; 
        } 
    }; 
    
    int main()
    { 
    
        BaseClass *a = new A(); 
        BaseClass *b = new BaseClass();
    
        ISampleProvider* sample = dynamic_cast<ISampleProvider*>(a); 
    
        if(sample)
            printf("%p\n", sample->getSample() );
    
        ISampleProvider* fail = dynamic_cast<ISampleProvider*>(b);
    
        if(fail==0)
            printf("Throw casting exception\n");
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple inheritance problem please help:P
    By miguel811 in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2009, 02:55 AM
  2. Exception handling framework based on multiple inheritance
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2007, 10:17 AM
  3. Inheritance problem, help please!
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2002, 07:02 AM
  4. Multiple Inheritance Ambiguity
    By FillYourBrain in forum C++ Programming
    Replies: 21
    Last Post: 08-23-2002, 10:31 AM
  5. Multiple virtual inheritance
    By kitten in forum C++ Programming
    Replies: 3
    Last Post: 08-10-2001, 10:04 PM