拷贝构造函数问题????????????????高手帮忙!!!!!!!!!!!!!
bumm 2005-09-01 05:37:45 代码如下:
#include <iostream>
using namespace std;
class Internet
{
protected:
char name[20];
char address[30];
char* cname;
public:
Internet(char* name,char* address)
{
cout<<"载入构造函数"<<endl;
strcpy(Internet::name,name);
strcpy(Internet::address,address);
cname = new char[strlen(name)+1];
if(cname != NULL)
{
strcpy(Internet::cname,name);
}
}
Internet(Internet &temp)
{
cout<<"载入COPY构造函数"<<endl;
strcpy(Internet::name,temp.name);
strcpy(Internet::address,temp.address);
cname = new char[strlen(name)+1];
if(cname != NULL)
{
strcpy(Internet::cname,name);
}
}
~Internet()
{
cout<<"载入析构函数"<<endl;
delete[] cname;
cin.get();
}
void show();
};
void Internet::show()
{
cout<<name<<":"<<address<<cname<<endl;
}
void test(Internet ts)
{
cout<<"载入test函数"<<endl;
}
void main()
{
Internet a("中国软件开发实验室","www.cndev-lab.com");
Internet b = a;
b.show();
test(b);
}
_____________________________________________________________________________________
输出结果如下:
载入构造函数
载入COPY构造函数
中国软件开发实验室:www.cndev-lab.com中国软件开发实验室
载入COPY构造函数
载入test函数
载入析构函数
????????为什么输出两遍"载入COPY构造函数"???????????