I am writing a Matrix2D
class. At the beginning I was using constructor as folows,
My code:
Matrix2D(float a,float b, float c,float d)
{
a_=a;
....
}
However, I have just realized that it would be a lot better if I could use multi dimensional array [2][2]
. That's where the problem lies,
How do I write constructor for array ?
class Matrix
{
float matrix[2][2];
public:
Matrix2D(float a,float b,float c, float d)
{
matrix[2][2]={a,b,c,d} // not valid
}
}
Just to let you know, I don't ask for a complete code. I just need someone to put me on a right track.
For C++11 you can do:
Matrix(float a,float b,float c, float d) :
matrix{{a,b},{c,d}}
{
}
There's no clean alternative for C++03.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With