Rodrigo Valente
Rodrigo Valente

Reputation: 319

How to use a function pointer in a overloaded operator

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

Answers (1)

Anonymous
Anonymous

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

Related Questions