#include <iostream>
using namespace std;
class Internet
{
public:
Internet(char *name,char *address)
{
cout<<"载入构造函数"<<endl;
strcpy(Internet::name,name);
}
Internet(Internet &temp)
{
cout<<"载入COPY构造函数"<<endl;
strcpy(Internet::name,temp.name);
cin.get();
}
~Internet()
{
cout<<"载入析构函数!";
}
public:
char name[20];
char address[20];
};
void main()
{
Internet a=Internet("中国软件开发实验室","www.cndev-lab.com");
cout<<a.name;
cin.get();
}
上面代码的运行结果有点“出人意料”,从思维逻辑上说,当无名对象创建了后,是应该调用自定义拷贝构造函数,或者是默认拷贝构造函数来完成复制过程的,但事实上系统并没有这么做,因为无名对象使用过后在整个程序中就失去了作用,对于这种情况c++会把代码看成是:
Internet a("中国软件开发实验室",www.cndev-lab.com);
省略了创建无名对象这一过程,所以说不会调用拷贝构造函数。