【问题标题】:Overloaded operator () vs member function get()重载运算符 () 与成员函数 get()
【发布时间】:2013-12-10 23:11:13
【问题描述】:

所以在我的代码中我有

double Matrix::get(int i, int j){
     return data[i][j];
}

double Matrix::operator()(int i, int j){
      return data[i][j];
}

问题是,课外我可以调用

Matrix A;
A(i,j)

类里面不知道怎么引用对象(A) 这样

Matrix::somefunction(){
    this(i,j)  ???
}

如何引用调用对象?

【问题讨论】:

  • (*this)(i, j)this->operator()(i, j)
  • operator()(i, j) 就足够了

标签: c++ object this operator-keyword


【解决方案1】:

你已经很接近了:

(*this)(i,j)

【讨论】:

  • 谢谢!那令人沮丧的关闭哈哈
【解决方案2】:

你也可以这样称呼

    operator()(i,j);

或(如前所述)

    (*this)(i,j);

【讨论】: