You can change the following line:
int operator()(size_t i, size_t j){
To:
int & operator()(size_t i, size_t j){
Returning a refernce (L value reference to be precise) to the element in the Matrix will allow you to assign to it.
Update: Some notes to complete my answer:
- As @user4581301 commented: you can see more info about C++ value categories here: What are rvalues, lvalues, xvalues, glvalues, and prvalues?
- As @Darth-CodeX and @HolyBlackCat mentioned, it is advisable to add a
const
overload for operator()
:
int const & operator()(int i, int j) const { /* same implementation */ }
You can use it with a const Matrix
for reading elements values: if you have e.g. a Matrix const & m
, you can use int val = m(0,0)
to read an element value (of cource you will not be able to use it for assignment due to constness).