E
et al.
Hi! I am learning C++, and I am trying to understand how operators
work, and should work. So, I've decided to do yet another matrix class!
Anyway, I'm stuck with deciding how I should implement things here,
mainly regarding operator overloading.
What I've done so far is this:
class cmatrix
{
public:
explicit cmatrix(unsigned int rows, unsigned int cols);
explicit cmatrix(cmatrix& src);
virtual ~cmatrix();
virtual double& at(unsigned int r, unsigned int c);
virtual cmatrix& operator=(const cmatrix src);
private:
vector<unsigned int, double> storage;
};
and I've successfully implemented the operator= (or better: it works),
but now I've doubts with other operations! Before that, is my copy
constructor well defined? And a silly question... does the operator=
need a reference, or is it ok to leave it as I wrote?
My question is easy: how do I multiply two matrices? I mean, in the
"definition" sense, this is not an algorithmic doubt! Let's suppose I
want to write this:
M = N * Q;
with M, N, and Q being cmatrix instances. That is the equivalent,
according to Stroustrup's book, of
M.operator=(N.operator*(Q));
The problem is... How should I declare the operator* ? I don't want to
modify N, and I'd like to avoid pointers! Forgive this silly question,
but I'm still learning!
Another problem is with cvector, derived from cmatrix. Will the same
operator work? Like:
v = M * w;
or worse, what if I want to overwrite a cvector or cmatrix?
v = M * v;
Thanks for any help you can give me!
work, and should work. So, I've decided to do yet another matrix class!
Anyway, I'm stuck with deciding how I should implement things here,
mainly regarding operator overloading.
What I've done so far is this:
class cmatrix
{
public:
explicit cmatrix(unsigned int rows, unsigned int cols);
explicit cmatrix(cmatrix& src);
virtual ~cmatrix();
virtual double& at(unsigned int r, unsigned int c);
virtual cmatrix& operator=(const cmatrix src);
private:
vector<unsigned int, double> storage;
};
and I've successfully implemented the operator= (or better: it works),
but now I've doubts with other operations! Before that, is my copy
constructor well defined? And a silly question... does the operator=
need a reference, or is it ok to leave it as I wrote?
My question is easy: how do I multiply two matrices? I mean, in the
"definition" sense, this is not an algorithmic doubt! Let's suppose I
want to write this:
M = N * Q;
with M, N, and Q being cmatrix instances. That is the equivalent,
according to Stroustrup's book, of
M.operator=(N.operator*(Q));
The problem is... How should I declare the operator* ? I don't want to
modify N, and I'd like to avoid pointers! Forgive this silly question,
but I'm still learning!
Another problem is with cvector, derived from cmatrix. Will the same
operator work? Like:
v = M * w;
or worse, what if I want to overwrite a cvector or cmatrix?
v = M * v;
Thanks for any help you can give me!