首先要说明的是,若用户没有定义,C++隐式声明一个拷贝构造函数和一个赋值运算符(完成按数据成员复制的动作)。二者很像,但是在下边这点上有很大的不同:
拷贝构造函数是只在对象实例化时才会被调用,也就是说,在拷贝构造函数调用期间,这个对象处于一个未决状态(直到拷贝构造函数被成功调用),另外拷贝构造函数不返回任何值,void都没有。
拷贝构造函数应用的场合由以下几个方面:
1 函数的参数是一个对象,并且是值传递方式
2 函数的返回值是一个对象,并且是值传递方式
3 用一个对象初始化另外一个对象

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
36
37
38
39
#include<iostream>
using namespace std;

class Test
{
public:
Test()
{
ctor_count++;
cout<<"ctor "<<ctor_count<<endl;
}

Test(const Test & r)
{
ctor_count++;
cout<<"copy ctor "<<ctor_count<<endl;
}

Test & operator= (const Test& r)
{
ctor_count++;
cout<<"assignment op "<<ctor_count<<endl;
return *this;
}

private:
static int ctor_count; //only a declaration
};

int Test::ctor_count=0; // definition + initialization


int main() {
Test test;
Test test1=test;
Test test2(test);
Test test3=test2=test1;
return 0;
}


输出结果
[root@xxx]$ ./a.out
ctor 1
copy ctor 2
copy ctor 3
assignment op 4
copy ctor 5

我们看到:
Test test; //实例化test对象时调用了默认构造函数
Test test1=test;// test1使用了拷贝构造函数(因为这是一个新的对象产生)
Test test2(test);//test2时也是用了拷贝构造函数,
Test test3=test2=test1;//而test2=test1则使用了操作符重载(没有新的对象产生),
Test test3=test2=test1;//test3=test2则使用了拷贝构造函数,原因同上。

所以要看是不是有新的对象产生,才能决定到底是调用了拷贝构造函数,还是赋值运算符。