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