关于c++中的操作符重载
About operator overloading in C++
我有一个关于操作符的问题,假设我有一个类myclass并且我重载了它的操作符*=,[]和+
可以使用this->*=, this->[], *this + *this…在成员函数中访问它们吗?div ?
是的,您可以通过多种方式访问它们。例如,您可以这样做:
*this + something
或者:
this->operator+(something)
this
只是一个指针。您可以使用任何指针执行以下所有操作。
这是首选的方式,因为它不会丢失操作语法:
(*this)[2]
(*this)(foo, bar)
*this / 3
*this * (that - 3) + 5
这只是对指针解引用。
你也可以使用他们的名字:
this->operator[](2)
this->operator() (foo, bar)
this->operator/ (3)
this->operator*(that - 3) + 5
如果您不使用外部操作符,它应该是这样工作的:this->operator[](args)
指针有一种特殊的语法,就像这样:
this->oprator[](0)
this->operator+(*this)