用于数组的c ++类构造函数

Tom*_*mas 6 c++ arrays class

我正在Matrix2D上课.一开始我使用构造函数作为folows,

我的代码:

Matrix2D(float a,float b, float c,float d)
{
    a_=a;
    ....
} 
Run Code Online (Sandbox Code Playgroud)

但是,我刚刚意识到,如果我可以使用多维,那将会好很多array [2][2].这就是问题所在,如何为数组编写构造函数?

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
    }
}
Run Code Online (Sandbox Code Playgroud)

只是为了让您知道,我不要求提供完整的代码.我只需要有人让我走上正轨.

Luc*_*ore 4

对于C++11你可以这样做:

Matrix(float a,float b,float c, float d) :
   matrix{{a,b},{c,d}}
{
}
Run Code Online (Sandbox Code Playgroud)

C++03没有干净的替代品。