重载operator()并在类中使用

hr0*_*r0m 2 c++ operator-overloading

我已经成功重载了()运算符.

inline real& Array::operator ()(int j,int i)
{                   
    //TODO checks                          
   return array_[i + j*xSize_];            
}
Run Code Online (Sandbox Code Playgroud)

它也可以正常工作.但是我没想到,如何在课堂内访问它.例如,以下函数打印所有元素:

for(int y = ySize_ -1; y >= 0; --y)
{
    for( int x = 0; x < xSize_; ++x)
    {
        std::cout << this(x,y);
    }
    std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我如何访问它呢?

R S*_*ahu 7

您可以使用以下语法:

(*this)(x, y);
Run Code Online (Sandbox Code Playgroud)

或更长的形式:

this->operator()(x, y);
Run Code Online (Sandbox Code Playgroud)

要么

operator()(x,y);
Run Code Online (Sandbox Code Playgroud)