// ( c7.C ) // コンストラクタの検討−Line( Point* a ) の場合 // #include class Point { int x ; public: Point( int x1=0 ) { x = x1 ; cout << "make Point" <<"\n"; } ~Point() { cout << "del Point\n" ; } // 以下暗黙の定義を明示して記述した Point( const Point& p ) { 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( Point* a ) { p = a ; cout << "make Line" <<"\n"; } // Line( Point* a ):p(a) { cout << "make Line" <<"\n"; } ~Line() { cout << "del Line\n" ; } }; main() { Point a( 10 ); Line b( &a ); } /* [結果] 検討1:Line( Point* a ):p(a) { .. どちらも同じ結果になった。 Line( Point* a ) { p = a ; .. make Point make Line del Line del Point 検討2:main() { Line a( new point(10) ); } ちなみにこれは次のような結果になった。 Point のデストラクタが効いていないことに注意。実際のアプリケ−ションには、 こんな記述はあまりしないと思われるから気にしなくてもいいかも知れない?。 make Point make Line del Line [参考] ポインタ型のクラスをメンバ変数に持つ Line の設定には、暗黙の定義は関係しないこ とが分かった。この例では Point のポインタそのものをやり取りしていることになる。 */