Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ class constructor for array

Tags:

c++

arrays

class

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.

like image 310
Tomas Avatar asked Oct 01 '12 19:10

Tomas


1 Answers

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.

like image 88
Luchian Grigore Avatar answered Sep 30 '22 13:09

Luchian Grigore