新手问题 请执教
wdxtn 2005-12-07 01:18:46 // ch10.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Counter
{
public:
Counter()
{
this->value=0;
}
~Counter()
{
}
int GetValue()
{
return this->value;
}
void SetValue(int value)
{
this->value=value;
}
void Increcement()
{
++value;
}
Counter operator++()
{
++value;
Counter temp;
temp.SetValue(value); //这里返回临时对象是不是不安全?到底怎么不安全?程序为什么还能正常运行?谢谢
return temp;
}
protected:
private:
int value;
};
int _tmain(int argc, _TCHAR* argv[])
{
Counter i;
cout<<i.GetValue()<<endl;
i.Increcement();
cout<<i.GetValue()<<endl;
++i;
cout<<i.GetValue()<<endl;
Counter a=++i;
cout<<a.GetValue()<<endl;
cout<<i.GetValue()<<endl;
return 0;
}