// ( c5.C ) // コンストラクタの検討−Line( const Point a ) の場合 // #include // const Point a の const は無くてもいいが、入れた方が安 // 全な場合が多い。const を付けると引数 a の中味を関数の class Point { // 中で、明示的に変更できないようする。 int x; const int flag ; public: Point( int x1=0 ):flag(0) { x = x1 ; cout << "make Point" <<"\n"; } ~Point() { cout << "del Point : " << flag << endl ; } // 以下暗黙の定義を明示して記述した。:flag(1) は検討のため追加した Point( const Point& p ):flag(1) { x = p.x ; cout << "copy make Point" <<"\n"; } const Point& operator=( const Point& p ) { x = p.x ; cout << "(operator =)\n" ; return *this ; } }; class Line { // 2つのコンストラクタの記述方法:どちらでも構わない! Point p ; public: // Line( const Point a ) { p = a ; cout << "make Line" <<"\n"; } Line( const Point a ):p(a) { cout << "make Line" <<"\n"; } ~Line() { cout << "del Line\n" ; } }; main() { Point a( 10 ); Line b( a ); } /* [結果] make Point 検討1:Line( Point a ) { p = a ; copy make Point コピ−コンストラクタが働いている。 make Point (operator =) これはコンストラクタではない。代入しただけである。 make Line del Point : 1 del Line del Point : 0 del Point : 0 make Point 検討2:Line( Point a ):p(a) { copy make Point copy make Point make Line del Point : 1 del Line del Point : 1 del Point : 0 */