![]() |
#2
rjsp2013-12-03 16:47
|

#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"创建了一个对象,现有"<<++num<<"个对象"<<endl;
}
~A()
{
cout<<"销毁了一个对象,还剩"<<--num<<"个对象"<<endl;
}
static int num;
};
int A::num=0;
template<typename T>
T fun(T t) //如果改成按引用T &fun(T &t){return t;}就调用一次构造和一次析构
{
return t;
}
int main(void)
{
A a;
fun(a);
return 0;
}
运行如下
只有本站会员才能查看附件,请 登录
就这样为什么只调用一次构造函数却连续调用了3次析构函数
另外如注释所写,能不能分别解释下T fun(T &t),T& fun(T &&t)和T&& fun(T &&t)以及T fun(T &&t)这几种区别