我一直在实现一个自定义模板矩阵类,我有一个函数,我需要一些帮助。我试图重载运算符+ =为此,我使用了已经实现并且正在工作的重载运算符[]。问题是,我不知道如何将'this'指针与操作符[]结合。C++:重载矩阵模板的+ =运算符
这里就是我想要做的事:
Matrix & operator+= (const Matrix & rhs)
{
if(this->numrows() != rhs.numrows() || this->numcols() != rhs.numrows())
{
cout << "ERR0R: Cannot add matrices of different dimensions." << endl;
return *this;
}
else
{
theType temp1, temp2, temp3;
for(int i = 0; i < this->numrows(); i++)
{
for(int j = 0; j < this->numcols(); j++)
{
temp1 = this->[i][j];
temp2 = rhs[i][j];
temp3 = temp1 + temp2;
this->[i][j] = temp3;
}
}
return *this;
}
}
不管我的错误/业余/冗余编码,:P我主要关注的是我如何可以使用this指针以同样的方式我打电话“RHS [i] [j]”。 (因为没有这个 - > [i] [j]或这个[i] [j]工作)
我在想也许它会工作很长的路< <例如:this-> operator [](i )>>但我无法弄清楚如何将双括号纳入。或者也许完全有另一种选择。我希望我能很好地解释自己。我有一种感觉,答案很简单。我只是难住。任何帮助表示赞赏。
谢谢。
[Check this out!](http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10) – Naszta