bl**********@gmail.com wrote:

I d rather have a Matlab syntax such as:

// create a Matrix of size 3,4 and fills it w/ random values.

Matrix<double> M=rand(3,4)

But since this function changes the state of the object, I ought to

make it a member function.

ie.

Matrix<double> M(3,4);

M.rand();

which I dont really like too much.

What is my best option (while maintaining a matlab-like notation)?

Add a static function to your 'Matrix' class template and let it create

another instance of 'Matrix'. It's called "a factory method":

template<class T> class Matrix {

...

public:

static Matrix rand(int n, int m) {

Matrix temp(n, m);

... // randomize them

return temp;

}

};

Matrix<double> M = Matrix<double>::rand(3, 4);

You could of course simply have a non-member function, but it would need

to be a template:

template<class T> Matrix<T> rand_matrix(int n, int m) {

...

}

and then you call it:

Matrix<double> M = rand_matrix<double>(3, 4);

V