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. ATL / WTL / STL
  4. Multiple Inheritance question

Multiple Inheritance question

Scheduled Pinned Locked Moved ATL / WTL / STL
oopquestion
6 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.
  • S Offline
    S Offline
    samzcs
    wrote on last edited by
    #1

    class CBase {
    string id;
    public:
    void show() {
    cout << id << endl;
    }
    };

    class CDerive1 : public CBase { };
    class CDerive2 : public CBase { };
    class CSon : public CDerive2, public CDerive1 { };

    int main ( )
    {
    CSon s;
    cout << &s << endl;
    cout << "---------" << endl;

    CDerive1 \*pd1 = &s;
    cout <<  pd1 << " &S: " << &s <<  endl;
    CDerive2 \*pd2 = &s;
    cout <<  pd2 << " &S: " << &s <<  endl;
    cout <<  "---------" << endl;
    

    The output of this code is : 0035FB20 --------- 0035FB3C 0035FB20 -------- don't know why cout << pd1, and cout << pd2 are different, they are assigned the same value &s.

    L V 2 Replies Last reply
    0
    • S samzcs

      class CBase {
      string id;
      public:
      void show() {
      cout << id << endl;
      }
      };

      class CDerive1 : public CBase { };
      class CDerive2 : public CBase { };
      class CSon : public CDerive2, public CDerive1 { };

      int main ( )
      {
      CSon s;
      cout << &s << endl;
      cout << "---------" << endl;

      CDerive1 \*pd1 = &s;
      cout <<  pd1 << " &S: " << &s <<  endl;
      CDerive2 \*pd2 = &s;
      cout <<  pd2 << " &S: " << &s <<  endl;
      cout <<  "---------" << endl;
      

      The output of this code is : 0035FB20 --------- 0035FB3C 0035FB20 -------- don't know why cout << pd1, and cout << pd2 are different, they are assigned the same value &s.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Your output does not match the code.

      S 1 Reply Last reply
      0
      • L Lost User

        Your output does not match the code.

        S Offline
        S Offline
        samzcs
        wrote on last edited by
        #3

        Tested the code in QT 5.7 again, output: 0x28fe60 ---------- 0x28fe78 0x28fe60 --------

        L 1 Reply Last reply
        0
        • S samzcs

          Tested the code in QT 5.7 again, output: 0x28fe60 ---------- 0x28fe78 0x28fe60 --------

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The output still does not match your code.

          1 Reply Last reply
          0
          • S samzcs

            class CBase {
            string id;
            public:
            void show() {
            cout << id << endl;
            }
            };

            class CDerive1 : public CBase { };
            class CDerive2 : public CBase { };
            class CSon : public CDerive2, public CDerive1 { };

            int main ( )
            {
            CSon s;
            cout << &s << endl;
            cout << "---------" << endl;

            CDerive1 \*pd1 = &s;
            cout <<  pd1 << " &S: " << &s <<  endl;
            CDerive2 \*pd2 = &s;
            cout <<  pd2 << " &S: " << &s <<  endl;
            cout <<  "---------" << endl;
            

            The output of this code is : 0035FB20 --------- 0035FB3C 0035FB20 -------- don't know why cout << pd1, and cout << pd2 are different, they are assigned the same value &s.

            V Offline
            V Offline
            Vi2
            wrote on last edited by
            #5

            don't know why cout << pd1, and cout << pd2 are different, they are assigned the same value &s.

            It is surprise of Multiple Inheritance. CSon consist of 2 blocks: CDerive2 block and CDerive1 block, each of them has own address. [CDerive2][CDerive1] or [ C S o n ] Therefore address of CSon instance is equal to address of CDerive2 part of CSon, but is not equal to address of CDerive1 part of CSon.

            With best wishes, Vita

            S 1 Reply Last reply
            0
            • V Vi2

              don't know why cout << pd1, and cout << pd2 are different, they are assigned the same value &s.

              It is surprise of Multiple Inheritance. CSon consist of 2 blocks: CDerive2 block and CDerive1 block, each of them has own address. [CDerive2][CDerive1] or [ C S o n ] Therefore address of CSon instance is equal to address of CDerive2 part of CSon, but is not equal to address of CDerive1 part of CSon.

              With best wishes, Vita

              S Offline
              S Offline
              samzcs
              wrote on last edited by
              #6

              Thanks

              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