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. Funny C++ code

Funny C++ code

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
7 Posts 4 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.
  • A Offline
    A Offline
    Alex_Y
    wrote on last edited by
    #1

    I was freaked out with this code until went to disassembly Find what you need to fix problem? #include "stdafx.h" class CA { public: // do not add default constructor here // solve problem with out it CA(int nA) { m_nA = nA; } int m_nA; }; class CB : virtual public CA { public: CB(int nB) : CA(nB) { m_nB = nB; } int m_nB; }; class CC : virtual public CA { public: CC(int nC) : CA(nC) { m_nC = nC; } int m_nC; }; class CD : public CC, public CB { public: CD(int nD) : CC(nD), CB(nD) { m_nD = nD; } int m_nD; }; int _tmain(int argc, _TCHAR* argv[]) { CD oD(1); //error C2512: 'CA::CA' : no appropriate default constructor available return 0; } //funny ha? :) -- modified at 10:21 Friday 26th August, 2005

    J S A 3 Replies Last reply
    0
    • A Alex_Y

      I was freaked out with this code until went to disassembly Find what you need to fix problem? #include "stdafx.h" class CA { public: // do not add default constructor here // solve problem with out it CA(int nA) { m_nA = nA; } int m_nA; }; class CB : virtual public CA { public: CB(int nB) : CA(nB) { m_nB = nB; } int m_nB; }; class CC : virtual public CA { public: CC(int nC) : CA(nC) { m_nC = nC; } int m_nC; }; class CD : public CC, public CB { public: CD(int nD) : CC(nD), CB(nD) { m_nD = nD; } int m_nD; }; int _tmain(int argc, _TCHAR* argv[]) { CD oD(1); //error C2512: 'CA::CA' : no appropriate default constructor available return 0; } //funny ha? :) -- modified at 10:21 Friday 26th August, 2005

      J Offline
      J Offline
      Joel Holdsworth
      wrote on last edited by
      #2

      Aha! This is obviously the classic "Diamond-of-Death" inheritance problem in the code. If you have this in your code, then you seriously need to rethink! Joel Holdsworth -- modified at 10:43 Friday 26th August, 2005

      A A 2 Replies Last reply
      0
      • A Alex_Y

        I was freaked out with this code until went to disassembly Find what you need to fix problem? #include "stdafx.h" class CA { public: // do not add default constructor here // solve problem with out it CA(int nA) { m_nA = nA; } int m_nA; }; class CB : virtual public CA { public: CB(int nB) : CA(nB) { m_nB = nB; } int m_nB; }; class CC : virtual public CA { public: CC(int nC) : CA(nC) { m_nC = nC; } int m_nC; }; class CD : public CC, public CB { public: CD(int nD) : CC(nD), CB(nD) { m_nD = nD; } int m_nD; }; int _tmain(int argc, _TCHAR* argv[]) { CD oD(1); //error C2512: 'CA::CA' : no appropriate default constructor available return 0; } //funny ha? :) -- modified at 10:21 Friday 26th August, 2005

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

        yes it will show error.u have to mention default construtor CD(int nD) : CC(nD), CB(nD) if u instantiated object of Class CD say CD cd(4); then both CC and CB( CC(int nC) : CA(nC) and CB(int nB):CA(nC)) will try to assign the data member of CA.whereas it is Virtually inherited in both Classes(CC,CB).So only one copy of Class CA has to be present. -- modified at 11:10 Friday 26th August, 2005

        A 1 Reply Last reply
        0
        • J Joel Holdsworth

          Aha! This is obviously the classic "Diamond-of-Death" inheritance problem in the code. If you have this in your code, then you seriously need to rethink! Joel Holdsworth -- modified at 10:43 Friday 26th August, 2005

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          u should provide default contructor if you define argument contructor

          1 Reply Last reply
          0
          • S sunit5

            yes it will show error.u have to mention default construtor CD(int nD) : CC(nD), CB(nD) if u instantiated object of Class CD say CD cd(4); then both CC and CB( CC(int nC) : CA(nC) and CB(int nB):CA(nC)) will try to assign the data member of CA.whereas it is Virtually inherited in both Classes(CC,CB).So only one copy of Class CA has to be present. -- modified at 11:10 Friday 26th August, 2005

            A Offline
            A Offline
            Alex_Y
            wrote on last edited by
            #5

            Replace constructor declaration CD(int nD) : CA(nD), CC(nD), CB(nD) Just so simple :)

            1 Reply Last reply
            0
            • J Joel Holdsworth

              Aha! This is obviously the classic "Diamond-of-Death" inheritance problem in the code. If you have this in your code, then you seriously need to rethink! Joel Holdsworth -- modified at 10:43 Friday 26th August, 2005

              A Offline
              A Offline
              Alex_Y
              wrote on last edited by
              #6

              Replace constructor declaration CD(int nD) : CA(nD), CC(nD), CB(nD) Just so simple :)

              1 Reply Last reply
              0
              • A Alex_Y

                I was freaked out with this code until went to disassembly Find what you need to fix problem? #include "stdafx.h" class CA { public: // do not add default constructor here // solve problem with out it CA(int nA) { m_nA = nA; } int m_nA; }; class CB : virtual public CA { public: CB(int nB) : CA(nB) { m_nB = nB; } int m_nB; }; class CC : virtual public CA { public: CC(int nC) : CA(nC) { m_nC = nC; } int m_nC; }; class CD : public CC, public CB { public: CD(int nD) : CC(nD), CB(nD) { m_nD = nD; } int m_nD; }; int _tmain(int argc, _TCHAR* argv[]) { CD oD(1); //error C2512: 'CA::CA' : no appropriate default constructor available return 0; } //funny ha? :) -- modified at 10:21 Friday 26th August, 2005

                A Offline
                A Offline
                Alex_Y
                wrote on last edited by
                #7

                Replace constructor declaration CD(int nD) : CA(nD), CC(nD), CB(nD) Just so simple :)

                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