一元运算符带参数重载的作用?
带参数重载有什么用?我带类内声明了带参数重载的一元运算符重载并类外定义了,
那么可以调用带参重载吗?如何使用?
// C++template.cpp: 定义控制台应用程序的入口点。
//
#include<iostream>
class Base
{
public:
Base(int x =0,int y =0,double d = 0):m_ix(x),m_iy(y),m_id(d) {}
~Base(){}
//输出
void PrintInfo();
//重载
Base& operator-();
Base& operator-(int);
Base& operator-(double);
private:
int m_ix;
int m_iy;
double m_id;
};
void Base::PrintInfo()
{
std::cout << "m_ix:" << this->m_ix << " m_iy:" << this->m_iy << " m_id:" << this->m_id << std::endl;
}
//--------重载
Base& Base::operator-()
{
this->m_ix = -(this->m_ix);
this->m_iy = -(this->m_iy);
this->m_id = -(this->m_id);
return *this;
}
Base& Base::operator-(int)
{
this->m_ix = -(this->m_ix);
this->m_iy = -(this->m_iy);
return *this;
}
Base& Base::operator-(double)
{
this->m_id = -(this->m_id);
return *this;
}
//------主函数
int main()
{
Base b(1,1,1.);
-b;
b.PrintInfo();
return 0;
}