6

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.

2
  • 2
    BTW, the class name and constructor name need to match. Commented Oct 1, 2012 at 19:40
  • thx, its just typo, they match in a source code Commented Oct 2, 2012 at 7:12

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You need to take off the parentheses, i.e. matrix{{a,b},{c,d}} because it's an array.
LWS uses gcc 4.7.2
ok, I have tried it and apparently vs 2010 doesnt support this feature, so I will have to go with " matrix[0][0]=a; " and so on.. Thx for support
2
matrix[0][0] = a; // initialize one element

and so on.

1 Comment

Thx for the answer. That's the way I'll do it
1

matrix[0][0] = value you want to matrix [n][n] = value you want but count up in a loop so the matrix can be dynamic in size or you can reuse your code later.

for(int ii(0); ii < first dimension size; ++ii)
{
   for(int ll(0); ii < second dimension size; ++ll)
   {
     matrix[ii][ll] = value you want;
   }
}

this will make your code more extensible and more useful outside of this application and maybe it's not useful or maybe it is.

Comments

0

If it will be a matrix 2X2, then you can pass a float array and then loop through it.

for example

for(int x = 0;x<4;x++)
{
    matrix[0][x] = myarray[x];
}

7 Comments

@LuchianGrigore how is that invalid?
@coolbartek If matrix is declared as float matrix[2][2] then matrix[0][3] is obviously invalid.
it looks fine for me, an two-dimensional array is actually a normal array, where you can access the elements by [0][x]. An array[x][y] is the same as an array[0][x*y]
@coolbartek: Don't you mean that array[y][x] is the same as array[0][x+y*width]?
@Luchian, are you forgetting that a[i] is the same as *(a + i) for built-in types?
|
0

Luchian's version is best if you have a C++11 compiler. Here's one that works for all versions of C++:

struct matrix_holder { float matrix[2][2]; };

class Matrix : matrix_holder
{
    static matrix_holder pack(float a,float b,float c, float d)
    {
        matrix_holder h = { {{a, b}, {c, d}} };
        return h;
    }

public:
    Matrix(float a,float b,float c, float d) : matrix_holder(pack(a,b,c,d))
    {
    }
};

The optimizer will inline away the helper.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.