今天因为改到一个代码,所以遇到一个问题。函数里局部变量返回的问题。
以下是代码,就是运用类模板,重载了+操作:
一。最原始的代码如下:
<pre name="code" class="cpp">#include<iostream>
#include<cstdlib>
template<typename T>
class T_Counter
{
public:
T_Counter(T aa)
{
a=aa;
}
T_Counter()
{
a=0;
}
~T_Counter()
{
cout<<"T_Counter "<<a<<" destruct!"<<endl;
};
T_Counter<T>& operator +(const T_Counter<T> t)
{
T_Counter<T> temp;
temp.a=a+t.a;
return temp;
}
friend ostream& operator<<(ostream &output,T_Counter<T>& t)
{
output<<t.a;
return output;
}
friend istream& operator>>(istream &input,T_Counter<T> &t)
{
input>>t.a;
return input;
}
private:
T a;
};
int main()
{
T_Counter<int> a(100),b(20);
<span style="white-space:pre"> </span>c = a+b;
cout<<c<<endl;
}
在win7+VS2012下可以编译通过,结果如下:
<pre name="code" class="cpp">T_Counter 120 destruct!
<pre name="code" class="cpp">T_Counter 20 destruct!
120
T_Counter 120 destruct!
T_Counter 20 destruct!
T_Counter 100 destruct!
我的理解是:首先输入参数是传值,那么首先会创建一个局部变量来接收传入的值,也就是t。接下来就是构造temp。所以前两行就是因为在<pre name="code" class="cpp">T_Counter<T>& operator +(const T_Counter<T> t)返回前销毁了两个临时变量,调用了相应的析构函数。但是这里的返回引用就有问题,因为
返回引用其实也就是返回临时变量temp的地址,但是因为temp地址所指向的东西在return前已经被销毁了。这样肯定会出错,但却运行正常。
但是这段代码在ubuntu kylin下编译不过,就因为返回引用。
二。接下来,我把<span style="font-family: Arial, Helvetica, sans-serif;">T_Counter<T>& operator +(const T_Counter<T>& t)改成如下:</span>
<span style="font-family: Arial, Helvetica, sans-serif;">T_Counter<T> operator +(const T_Counter<T>& t)</span>
<pre name="code" class="cpp"><pre name="code" class="cpp">
{
T_Counter<T> temp;
temp.a=a+t.a;
return temp;
}
在两个系统下皆通过。
运行结果如下:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUsAAABxCAYAAABcIYHhAAAgAElEQVR4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QgYPPk6SZQUcO/Rn9E/raVzuQ/rMvE4aYFH/c9y8p9Ey+hDuo5fjF6Wk6iutaFHFnExZT8voqJxVXTr1rkXywWlcis//YLgO25KYVSriKbm+UWibmsnoGzL7j9R8ds070zjrEn9cTsas2hu87RjHucP/qvtXSTRvSZWhNXbQlSjzzytUoVqK2rI670xezvZIaZQShOe5ZQzpVB2L/DUqzEaWYrYdLSZP4K1Yd1y+WYHn2esEeG5mwRRfqnzdnyZ2kgBdZV5t+xyqE7P5cf5ezpy+wLk9W/htwnL8Ht2I3/qRPeqRVTDeV9YwsluN3JilPad7QjAH5jTJd2hdltabLuMXNJHG1qCs8RkbpBFcgh+7j+9k3/YOlM1fZ2laWZnWc9awNdCXM5rRnr8Hoz56TnsUalmNt39YypZ/c0bDiRc5cM6DUR0qodVgj/rX9Tv+Jtf9rnDV5yR7Z45h1rEM6n3eBUdJFcPMgQ4/T6Tlw92MeHcYv3ocxPvAAXbOncKkGb5oKUWaOdBmxTHOJ3gys7uDwTgsG3XnA8dETq72y9thWDVi2NVgTnq0pIwmznLtWZkczFbnPCENZWFq6hLGwpTSNf5zvnXGI/mRUZbaxdPUN4xpU0N9Q8NQr401L80/pT5i8drWDhvrt5gfHsT5a1NopKzMx0eO8P1r+Q/xNA6f3u//AyMlZHRr+EBrAAAAAElFTkSuQmCC" alt="" />
我的理解:除了返回类型改成返回值,还有输入参数改为引用。查阅了一些资料,因为其实指针或地址都是在函数的栈上的,所以当函数返回时会销毁栈的东西,而值则是在堆上的,所以返回之后并没有问题。而输出结果少了第二行<span style="font-family: Arial, Helvetica, sans-serif;">T_Counter 20 destruct!是因为输入参数传入了引用t,所以并没有构造临时变量,也就没有析构。</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
三。接下来,我把main函数里的c去掉。代码改成如下:
<pre name="code" class="cpp"> T_Counter<int> a(100),b(20);
cout<<a+b<<endl;
问题:ubuntu下提示没有从T_Count<int>到T_Count<int>&的转换。看了一下,是重载+操作符函数的问题,因为
friend ostream& operator<<(ostream &output,T_Counter<T>& t)接收的是一个T_Count<int>&,而我们传入的是一个值。所以就出错了。
改成如下就没错,<span style="font-family: Arial, Helvetica, sans-serif;">friend ostream& operator<<(ostream &output,T_Counter<T> t)。但输出结果有点奇葩。</span>
<span style="font-family: Arial, Helvetica, sans-serif;">输出结果为:</span>
<img src="https://img-blog.csdn.net/20150405164744849?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSW5jeV8xMjE4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" style="font-family: Arial, Helvetica, sans-serif;" alt="" />
这里我就不理解啦。现在到VS下看看