开发者

C++ Matrix Class with Operator Overloading

I was implementing a small dense matrix class and instead of plan get/set operators I wanted to use operator overloading to make the API more usable and coherent.

What I want to achieve is pretty simple:

template<typename T>
class Matrix
{
public:

    /* ... Leaving out CTOR and Memory Management for Simplicity */

    T operator() (unsigned x, unsigned y){/* ... */ }
};


Matrix<int> m(10,10);
int value = m(5,3); // get the value at index 5,3
m(5,3) = 99; // set the value at index 5,3

While getting the value is straight forward by overloading operator(), I can't get my head around defining the setter. From what I understood the operator precedence would call operator() before the assignment, however it is not possible to overload operator(开发者_如何学Go) to return a correct lvalue.

What is the best approach to solve this problem?


I dispute that "it's not possible" to do the correct thing:

struct Matrix
{
  int & operator()(size_t i, size_t j) { return data[i * Cols + j]; }
  const int & operator()(size_t i, size_t j) const { return data[i * Cols + j]; }

  /* ... */

private:
  const size_t Rows, Cols;
  int data[Rows * Cols];  // not real code!
};

Now you can say, m(2,3) = m(3,2) = -1; etc.


The answer to your question is what Kerrek already stated: you can provide an overload by changing the signature of the operator, which in this case can be achieved by modifying the const-ness of the function.

But I would recommend that you at least consider providing a separate setter for the values. The reason is that once you return references to your internal data structures you loose control of what external code does with your data. It might be ok in this case, but consider that if you decided to add range validation to the implementation (i.e. verify that no value in the matrix is above X or below Y), or wished to optimize some calculation on the matrix (say the sum of all of the elements in the matrix is an often checked value, and you want to optimize away the calculation by pre-caching the value and updating it on each field change) it is much easier to control with a method that receives the value to set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜