classA{ private: int & a; int _b; public: A(int target, int b) : a(target), _b(b){ cout << "Constructor Function" << endl; } // A(int target, int b) { // a = target; 这里会报错 // _b = b; // } voidprintA(){ cout << "a is " << a << " b is " << _b << endl; } };
intmain(void){ int a = 20, a2 = 15; A r(a, a2); r.printA(); A r2(a2, a); r2.printA(); return0; }
/* 该程序的输出结果为: =================== Constructor Function a is 20 b is 15 Constructor Function a is 15 b is 20 =================== */