C is including A,B classes. and B is including A class.
When copy from C2 to C1, How to make "Copy Constructors, Assignment Operators"?
C c1;
C c2(xx,yy);
c1 = c2;
Below source code is example of these case.
----------------------------------------------------------------------------------------------
#include <vector> using namespace std; struct coordi{ int x; int y; }; class A{ public: A():val1(0),val2(0){} A(int inV1){ val1 = inV1; } A(int inV1, vector<coordi> inV2){ val1 = inV1; val2 = inV2; } A(const A &in) { val1 = in.val1; val2 = in.val2; } bool operator = (const A &inA ) { val1 = inA.val1; val2 = inA.val2; return 0; } void printfAll() { printf("class A\n"); printf("val1 = %d\n", val1); for(int i=0; i<val2.size(); ++i) { printf("val2[%d] - x = %d, y=%d\n", i, val2[i].x, val2[i].y ); } printf("\n"); } private: int val1; vector<coordi> val2; }; class B{ public: ////////////////////////////////////////////////////////////////////////// B():val1(0),val2(0),class1(){} B(int inV1){ val1 = inV1; } B(int inV1, vector<coordi> inV2 ){ val1 = inV1; val2 = inV2; } B(int inV1, vector<coordi> inV2, A inV3 ){ val1 = inV1; val2 = inV2; class1 = inV3; } ////////////////////////////////////////////////////////////////////////// B(const B &in) { val1 = in.val1; val2 = in.val2; class1 = in.class1; } bool operator = (const B &inB ) { val1 = inB.val1; val2 = inB.val2; class1 = inB.class1; return 0; } ////////////////////////////////////////////////////////////////////////// void printfAll() { printf("class B \n"); printf("val1 = %d\n", val1); for(int i=0; i<val2.size(); ++i) { printf("val2[%d] - x = %d, y=%d\n", i, val2[i].x, val2[i].y ); } printf("\n"); class1.printfAll(); } private: A class1; int val1; vector<coordi> val2; }; class C{ public: ////////////////////////////////////////////////////////////////////////// C():val1(0),val2(0),class1(),class2(){} C(int inV1){ val1 = inV1; } C(int inV1, vector<coordi> inV2 ){ val1 = inV1; val2 = inV2; } C(int inV1, vector<coordi> inV2, A inV3 ){ val1 = inV1; val2 = inV2; class1 = inV3; } C(int inV1, vector<coordi> inV2, A inV3 , B inV4){ val1 = inV1; val2 = inV2; class1 = inV3; class2 = inV4; } ////////////////////////////////////////////////////////////////////////// C(const C &in) { val1 = in.val1; val2 = in.val2; class1 = in.class1; class2 = in.class2; } bool operator = (const C &inC ) { val1 = inC.val1; val2 = inC.val2; class1 = inC.class1; class2 = inC.class2; return 0; } ////////////////////////////////////////////////////////////////////////// void printfAll() { printf("class C \n"); printf("val1 = %d\n", val1); for(int i=0; i<val2.size(); ++i) { printf("val2[%d] - x = %d, y=%d\n", i, val2[i].x, val2[i].y ); } printf("\n"); printf("class 1\n"); class1.printfAll(); printf("class 2\n"); class2.printfAll(); } private: A class1; B class2; int val1; vector<coordi> val2; }; int _tmain(int argc, _TCHAR* argv[]) { ////////////////////////////////////////////////////////////////////////// int sampleV1 = 100; vector< coordi > sampleV2; for(int i=0; i< 10; ++i) { coordi tCoordi; tCoordi.x = i; tCoordi.y = i*i; sampleV2.push_back(tCoordi); } ////////////////////////////////////////////////////////////////////////// A cA1; cA1.printfAll(); A cA2(sampleV1); cA2.printfAll(); A cA3(sampleV1, sampleV2); cA3.printfAll(); printf("===============\n"); ////////////////////////////////////////////////////////////////////////// B cB1; cB1.printfAll(); B cB2(sampleV1, sampleV2); cB2.printfAll(); B cB3(sampleV1,sampleV2,cA3); cB3.printfAll(); B cB4(cB3); cB4.printfAll(); B cB5; cB5 = cB4; cB5.printfAll(); ////////////////////////////////////////////////////////////////////////// C cC1; cC1.printfAll(); C cC2(sampleV1); cC2.printfAll(); C cC3(sampleV1, sampleV2); cC3.printfAll(); C cC4(sampleV1, sampleV2,cA3); cC4.printfAll(); C cC5(sampleV1, sampleV2,cA3,cB3); cC5.printfAll(); C cC6(cC5); cC6.printfAll(); C cC7; cC7 = cC5; cC7.printfAll(); return 0; }
You can download source code <here>.
No comments:
Post a Comment