2つのオブジェクトの型が同じであれば、1つのオブジェクトをもう1つのオブジェクトに代入することができる。
#include <iostream> using namespace std; class myclass { int a, b; public: void set(int i, int j) { a = i; b = j; } void show() { cout << a << ' ' << b << "\n"; } }; int main() { myclass o1, o2; o1.set(10, 4); // o1をo2に代入する o2 = o1; o1.show(); o2.show(); return 0; }
$ g++ sample15.cpp $ ./a.out 10 4 10 4