以前我们的教程中讨论过函数返回对象产生临时变量的问题,接下来我们来看一下在函数中返回自定义类型对象是否也遵循此规则产生临时对象!
先运行下列代码:
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必著名出处和作者
#include <iostream>
usingnamespacestd;
classInternet
{
public:
Internet()
{
};
Internet(char*name,char*address)
{
cout<<"载入构造函数"<strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
Internet(Internet &temp)
{
cout<<"载入COPY构造函数"<strcpy(Internet::name,temp.name);
strcpy(Internet::address,temp.address);
cin.get();
}
~Internet()
{
cout<<"载入析构函数!";
cin.get();
}
protected:
charname[20];
charaddress[20];
};
Internet tp()
{
Internet b("中国软件开发实验室","www.cndev-lab.com");
returnb;
}
voidmain()
{
Internet a;
a=tp();
}
从上面的代码运行结果可以看出,程序一共载入过析构函数三次,证明了由函数返回自定义类型对象同样会产生临时变量,事实上对象a得到的就是这个临时Internet类类型对象temp的值。
这一下节的内容我们来说一下无名对象。
利用无名对象初始化对象系统不会不调用拷贝构造函数。
那么什么又是无名对象呢?
很简单,如果在上面程序的main函数中有:
Internet ("中国软件开发实验室","www.cndev-lab.com");
这样的一句语句就会产生一个无名对象,无名对象会调用构造函数但利用无名对象初始化对象系统不会不调用拷贝构造函数!
下面三段代码是很见到的三种利用无名对象初始化对象的例子。
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必著名出处和作者
#include <iostream>
usingnamespacestd;
classInternet
{
public:
Internet(char*name,char*address)
{
cout<<"载入构造函数"<strcpy(Internet::name,name);
}
Internet(Internet &temp)
{
cout<<"载入COPY构造函数"<strcpy(Internet::name,temp.name);
cin.get();
}
~Internet()
{
cout<<"载入析构函数!";
cin.get();
}
public:
charname[20];
charaddress[20];
};
voidmain()
{
Internet a=Internet("中国软件开发实验室","www.cndev-lab.com");
cout<cin.get();
}
上面代码的运行结果有点“出人意料”,从思维逻辑上说,当无名对象创建了后,是应该调用自定义拷贝构造函数,或者是默认拷贝构造函数来完成复制过程的,但事实上系统并没有这么做,因为无名对象使用过后在整个程序中就失去了作用,对于这种情况c++会把代码看成是:
Internet a("中国软件开发实验室","www.cndev-lab.com");
省略了创建无名对象这一过程,所以说不会调用拷贝构造函数。
////////////////Kevin Lynx//////
最近我打算自己写个链表模板类~~然后在模扳上就遇到了个很奇怪的问题~~(祥见http://bbs.gameres.com/showthread.asp?page=end&threadid=49258)~~然后看到了以上的文章~~以下是我整理后的代码,并附运行结果,程序在Dev--C++下运行:
#include <stdlib.h>
#include <iostream>
using namespace std;
class Internet
{
public:
Internet()
{
cout <<"载入空构造函数" <<endl;
};
Internet(char*name,char*address)
{
cout<<"载入传值构造函数"<<endl;
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
Internet(Internet &temp)
{
cout<<"载入COPY构造函数"<<endl;
strcpy(Internet::name,temp.name);
strcpy(Internet::address,temp.address);
//cin.get();
}
~Internet()
{
cout<<"载入析构函数!"<<endl;
//cin.get();
}
protected:
char name[20];
char address[20];
};
//////////////
Internet tp()
{
Internet b("中国软件开发实验室","www.cndev-lab.com");
return b;
}
int main()
{
Internet a;
cout <<"Will call the tp function"<<endl;
a=tp();
cout <<"will end "<<endl;
system("pause");
return 0;
}