简体   繁体   中英

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM