Multiple Inheritance doubt
-
My code snippet is something like this one, class Base1 { public: Base1() { cout<<"Base1()"<<endl; } virtual void method() { cout<<"Base1::method()"<<endl; } }; class Base2 { public: Base2() { cout<<"Base2()"<<endl; } virtual void method() { cout<<"Base2::method()"<<endl; } }; class Derived:public Base1, public Base2 { public: Derived() { cout<<"Derived()"<<endl; } void method() { cout<<"Derived::method()"<<endl; } }; int _tmain(int argc, _TCHAR* argv[]) { // Derived *d = static_cast<Derived *>(new Base1); // works fine // application crash at Base2::method call, even though Base2() constructor is invoked Derived *d = static_cast<Derived *>(new Base2); d->method(); return 0; } With Base1, application works fine. But when I try to use Base2, application crashes at method() call. And if I change the sequence in Derived class declaration to... class Derived:public Base2, public Base1 then Base2 object works perfectly but Base1 crashes. So, my question is why up-casting (or down-casting, not sure about specific word) works only with primary base class and not with secondary base classes??
-
My code snippet is something like this one, class Base1 { public: Base1() { cout<<"Base1()"<<endl; } virtual void method() { cout<<"Base1::method()"<<endl; } }; class Base2 { public: Base2() { cout<<"Base2()"<<endl; } virtual void method() { cout<<"Base2::method()"<<endl; } }; class Derived:public Base1, public Base2 { public: Derived() { cout<<"Derived()"<<endl; } void method() { cout<<"Derived::method()"<<endl; } }; int _tmain(int argc, _TCHAR* argv[]) { // Derived *d = static_cast<Derived *>(new Base1); // works fine // application crash at Base2::method call, even though Base2() constructor is invoked Derived *d = static_cast<Derived *>(new Base2); d->method(); return 0; } With Base1, application works fine. But when I try to use Base2, application crashes at method() call. And if I change the sequence in Derived class declaration to... class Derived:public Base2, public Base1 then Base2 object works perfectly but Base1 crashes. So, my question is why up-casting (or down-casting, not sure about specific word) works only with primary base class and not with secondary base classes??
Try reinterpret_cast insted of static_cast. I dont know the reason why it is so. I will look it in details and update this post later.
The secret of life is not enjoyment but education through experience. - Swami Vivekananda.
-
My code snippet is something like this one, class Base1 { public: Base1() { cout<<"Base1()"<<endl; } virtual void method() { cout<<"Base1::method()"<<endl; } }; class Base2 { public: Base2() { cout<<"Base2()"<<endl; } virtual void method() { cout<<"Base2::method()"<<endl; } }; class Derived:public Base1, public Base2 { public: Derived() { cout<<"Derived()"<<endl; } void method() { cout<<"Derived::method()"<<endl; } }; int _tmain(int argc, _TCHAR* argv[]) { // Derived *d = static_cast<Derived *>(new Base1); // works fine // application crash at Base2::method call, even though Base2() constructor is invoked Derived *d = static_cast<Derived *>(new Base2); d->method(); return 0; } With Base1, application works fine. But when I try to use Base2, application crashes at method() call. And if I change the sequence in Derived class declaration to... class Derived:public Base2, public Base1 then Base2 object works perfectly but Base1 crashes. So, my question is why up-casting (or down-casting, not sure about specific word) works only with primary base class and not with secondary base classes??
I think this article[^] will answer all your questions.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
I think this article[^] will answer all your questions.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++Thanks, the article is really good.