Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multiple Inheritance doubt

Multiple Inheritance doubt

Scheduled Pinned Locked Moved C / C++ / MFC
questionoop
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chirag_chauhan
    wrote on last edited by
    #1

    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??

    M C 2 Replies Last reply
    0
    • C chirag_chauhan

      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??

      M Offline
      M Offline
      Mahesh Kulkarni
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • C chirag_chauhan

        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??

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        I think this article[^] will answer all your questions.

        Cédric Moonen Software developer
        Charting control [v2.0] OpenGL game tutorial in C++

        C 1 Reply Last reply
        0
        • C Cedric Moonen

          I think this article[^] will answer all your questions.

          Cédric Moonen Software developer
          Charting control [v2.0] OpenGL game tutorial in C++

          C Offline
          C Offline
          chirag_chauhan
          wrote on last edited by
          #4

          Thanks, the article is really good.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups