关于copy constructor

/************************
 **以下测试在vc7.1中进行**
************************/
#include <vector>
#include <iostream>

using namespace std;

class A{
public:
 A(){
  cout << "A()" << endl;
 }
 A ( const A &a )
 {
  cout << "A ( const A &a )" << endl;
 }
};

class B : public A{
public:
 B(){
  cout << "B()" << endl;
 }

 B ( const A &a )
 {
  cout << "B ( const A &a )" << endl;
  }

 //B ( const B &b )
 //{
 // cout << "B ( const B &b )" << endl;
 //}

};


void main()
{
 B b;
 vector<B> b_vec;

 cout << "before push back " << endl;

 b_vec.push_back( b );

 cout << "before resize" << endl;

 b_vec.resize( 2 );

 cout << "before A a = b" << endl;
 
 A a = b;

 cout << "before B b2 = a" << endl;

 B b2 = a;

 cout << "before B b3 = b" << endl;

 B b3 = b;

 cout << "before return" << endl;

    return;
}

输出:
A()
B()
before push back
A ( const A &a )
A ( const A &a )
before resize
A()
B()
A ( const A &a )
A ( const A &a )
A ( const A &a )
before A a = b
A ( const A &a )
before B b2 = a
A()
B ( const A &a )
before B b3 = b
A ( const A &a )
before return

去掉注释后的执行输出:
A()
B()
before push back
A()
B ( const B &b )
A()
B ( const B &b )
before resize
A()
B()
A()
B ( const B &b )
A()
B ( const B &b )
A()
B ( const B &b )
before A a = b
A ( const A &a )
before B b2 = a
A()
B ( const A &a )
before B b3 = b
A()
B ( const B &b )
before return

//程序改为:
#include <vector>
#include <iostream>

using namespace std;

class A{
public:
 A(){
  cout << "A()" << endl;
 }
 A ( const A &a )
 {
  cout << "A ( const A &a )" << endl;
 }
};

class B : public A{
public:
 B(){
  cout << "B()" << endl;
 }

 //B ( const A &a )
 //{
 // cout << "B ( const A &a )" << endl;
 // }

 B ( const B &b )
 {
  cout << "B ( const B &b )" << endl;
 }

};


void main()
{
 B b;
 vector<B> b_vec;

 cout << "before push back " << endl;

 b_vec.push_back( b );

 cout << "before resize" << endl;

 b_vec.resize( 2 );

 cout << "before A a = b" << endl;
 
 A a = b;

 //cout << "before B b2 = a" << endl;

 //B b2 = a;

 cout << "before B b3 = b" << endl;

 B b3 = b;

 cout << "before return" << endl;

    return;
}

输出:
A()
B()
before push back
A()
B ( const B &b )
A()
B ( const B &b )
before resize
A()
B()
A()
B ( const B &b )
A()
B ( const B &b )
A()
B ( const B &b )
before A a = b
A ( const A &a )
before B b3 = b
A()
B ( const B &b )
before return


程序修改为:
#include <vector>
#include <iostream>

using namespace std;

class A{
public:
 A(){
  cout << "A()" << endl;
 }
 A ( const A &a )
 {
  cout << "A ( const A &a )" << endl;
 }
};

class B : public A{
public:
 B(){
  cout << "B()" << endl;
 }

 //B ( const A &a )
 //{
 // cout << "B ( const A &a )" << endl;
 // }

 //B ( const B &b )
 //{
 // cout << "B ( const B &b )" << endl;
 //}

};


void main()
{
 B b;
 vector<B> b_vec;

 cout << "before push back " << endl;

 b_vec.push_back( b );

 cout << "before resize" << endl;

 b_vec.resize( 2 );

 cout << "before A a = b" << endl;
 
 A a = b;

 //cout << "before B b2 = a" << endl;

 //B b2 = a;

 cout << "before B b3 = b" << endl;

 B b3 = b;

 cout << "before return" << endl;

    return;
}
输出:
A()
B()
before push back
A ( const A &a )
A ( const A &a )
before resize
A()
B()
A ( const A &a )
A ( const A &a )
A ( const A &a )
before A a = b
A ( const A &a )
before B b3 = b
A ( const A &a )
before return

结论:
1 在VC.Net中 编译器合成的子类copy constructor中会自动调用基类的copy constructor, 用户自定义的子类copy constructor中如果需要调用基类的
copy constructor,用户需要手动添加相应代码;
2 copy constructor的参数可以是基类或同类,在调用时选择匹配
  如果实参是基类而没有定义形参为基类的copy constructor,编译错误
  ................已经................................,调用之
  ..........同类..没有..........同类..................,编译器合成
  ................已经................................,调用之
3 copy constructor的正确写法是:参数为同类型,在子类的copy constructor中调用基类的copy constructor;

正确写法:
#include <vector>
#include <iostream>

using namespace std;

class A{
public:
 A(){
  cout << "A()" << endl;
 }
 A ( const A &a )
 {
  cout << "A ( const A &a )" << endl;
 }
};

class B : public A{
public:
 B(){
  cout << "B()" << endl;
 }

 //B ( const A &a )
 //{
 // cout << "B ( const A &a )" << endl;
 // }

 B ( const B &b ) : A ( b )
 {
  cout << "B ( const B &b )" << endl;
 }

};


void main()
{
 B b;
 vector<B> b_vec;

 cout << "before push back " << endl;

 b_vec.push_back( b );

 cout << "before resize" << endl;

 b_vec.resize( 2 );

 cout << "before A a = b" << endl;
 
 A a = b;

 //cout << "before B b2 = a" << endl;

 //B b2 = a;

 cout << "before B b3 = b" << endl;

 B b3 = b;

 cout << "before return" << endl;

    return;
}

输出:
A()
B()
before push back
A ( const A &a )
B ( const B &b )
A ( const A &a )
B ( const B &b )
before resize
A()
B()
A ( const A &a )
B ( const B &b )
A ( const A &a )
B ( const B &b )
A ( const A &a )
B ( const B &b )
before A a = b
A ( const A &a )
before B b3 = b
A ( const A &a )
B ( const B &b )
before return


在定义copy constructor还应该考虑实参可能是一个刚构造起来的对象

posted on 2004-07-27 21:42  哲学 艺术 程序 人生  阅读(436)  评论(0)    收藏  举报

导航