1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <iostream>
class Test { private: int a; int b; public: Test(int A, int B):a(A), b(B){}; Test& addA(Test &t); int getA(){return this -> a;}; int getB(){return this -> b;}; };
Test& Test::addA(Test& t) { int a = t.getA(); this -> a += a; return *this; }
int main() { Test t1(1, 2); Test t2(3, 4); std::cout << "before: " << t1.getA() << std::endl; t1.addA(t2).addA(t2).addA(t2); std::cout << "after: " << t1.getA() << std::endl; return 0; }
|