重载运算符,i++和++i是怎么知道执行的是哪个operator函数,虽然第二个带了个参数。
#include <iostream>
using
namespace std;
class dome
{
public:
dome(){cout<<"构造函数"<<endl;};
dome(int i){cout<<"带参构造函数"<<endl;x=i;};
~dome(){cout<<"析构函数"<<endl;};
dome(const dome&one){x=one.x;cout<<"复制值"<<endl;}
void
set(int i){x=i;}
int
get(){return x;}
dome &operator++()
{
++x;
cout<<"operator执行"<<endl;
return
*this;
}
dome operator++(int o)
{
dome temp(*this);
++x;
return temp;
}
private:
int x;
};
int main()
{
dome n;
n.set(5);
cout<<n.get()<<endl;
dome y=n++;
cout<<n.get()<<endl;
cout<<y.get()<<endl;
return
0;
}; |