Reputation: 319
I was trying achieve a Matlab way to acess matrixes elements. For example, if i have the following in MatLab:
>> A = [1 2; 3 4]
A =
1 2
3 4
>> A(1,1) = 5
Will generate this output:
A =
5 2
3 4
Is this possible to achieve with the operator () in c++? Thanks in advance.
@edit
Sorry guys, i think the resolve would be achieved by a function pointer.
Ill try to be more plain. I have a class matrix, I want to change a respective value of the class using the operator(), passing the number respective to the row and to the column.
Upvotes: 0
Views: 88
Reputation: 2172
template<size_t I, size_t J>
class M
{
public:
M(double e00, ...)
{
// use arg_list :)
}
double& operator () (int i, int j)
{
return m[i-1][j-1];
}
private:
double m[I][J];
};
M<2,2> A(1,2,3,4);
A(1,1)=5.0;
Upvotes: 2