Returns a float array in C ++

Currently, I have a 4x4 matrix class in C ++, and I save each value as a float:

Matrix4d::Matrix4d(const float& m00, const float& m01, const float& m02, const float& m03, const float& m10, const float& m11, const float& m12, const float& m13, const float& m20, const float& m21, const float& m22, const float& m23, const float& m30, const float& m31, const float& m32, const float& m33) { _m00 = m00; _m01 = m01; _m02 = m02; _m03 = m03; _m10 = m10; _m11 = m11; _m12 = m12; _m13 = m13; _m20 = m20; _m21 = m21; _m22 = m22; _m23 = m23; _m30 = m30; _m31 = m31; _m32 = m32; _m33 = m33; } 

My question is, how can I return a floating point array of this data? I have no problem creating an array in a class, for example:

 float arrayToReturn[16] = { m00, m01, m02, m03, ... m33 }; 

However, I cannot return this value from the class. I read about returning a pointer to an array, but it was out of luck.

+4
source share
5 answers

This will work if your internal array looks like float array[4][4] :

 float** Matrix4d::getMatrix(); 

If your inner array was one-dimensional:

 float* Matrix4d::getMatrix(); 

But both cases expose the inner workings of your class to the outside world, which makes your code less secure and difficult to maintain.

It’s better to create a copy constructor, an operator () and an assignment operator for your Matrix4d class and just pass this. You will be less likely to have runtime errors due to poor memory management or data corruption.

Your statement () will look like this:

 float& operator()( unsigned int xIndex, unsigned int yIndex ) { //return the right attribute } 

You would call it that to set the values:

 aMatrix(0,0) = 2.0; 

or is it for extracting:

 float attributeCopy = aMatrix(0,0); 

He works both ways.

EDIT: Forgot that the [] operator accepted only one argument. Changed operator to operator () aka functional operator.

+2
source
  • Do not skip floats using the const link, pass them by value.

  • I assume you want to return an array so that you can index? Then do not return an array from your matrix class. Instead, overload the [] operator or something else.

  • In addition, I would not use 16 member variables except one array. Makes indexing a lot easier.

Here is how I will probably do it:

 class Matrix4d { float matrix[4][4]; public: Matrix4d(float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13, float m20, float m21, float m22, float m23, float m30, float m31, float m32, float m33) { matrix[0][0] = m00; matrix[0][1] = m01; matrix[0][2] = m02; matrix[0][3] = m03; matrix[1][0] = m10; matrix[1][1] = m11; matrix[1][2] = m12; matrix[1][3] = m13; matrix[2][0] = m20; matrix[2][1] = m21; matrix[2][2] = m22; matrix[2][3] = m23; matrix[3][0] = m30; matrix[3][1] = m31; matrix[3][2] = m32; matrix[3][3] = m33; } float* operator[](int i) { return matrix[i]; } const float* operator[](int i) const { return matrix[i]; } }; int main() { Matrix4d test(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 16); test[3][2] = 15; } 
+4
source

You can use union to describe your class.

 union { struct { float _m00; float _m01; ... }; float _m[16]; }; 

Then you can return _m.

getting columns can also be useful:

 union { struct { float _m00; float _m01; ... }; float _m[4*4]; float _cols[4][4]; }; 
+2
source

Three options that I can think of.

The first returns std :: vector, not an array. The user can always get a pointer to the internal array using & v [0].

 std::vector<float> Matric4d::getData() { std::vector<float> d(16); d[0]=_m00; ... return d; } 

The second returns an array of boost ::. (I think there is an array tr1 :: from from the upcoming C ++ 0x standard if your compiler supports something like that). Again it is easy to get to the internal array.

The third is the most hacker one, but it might be better if you need speed. If you save your floats contiguously, you can simply return the pointer to the first record. (Note that you need to be careful with the details of your class, otherwise you will get β€œundefined behavior.” However, many / most / all compilers will still β€œdo the right thing.”) Oh, and you need to be careful, as the pointer will valid only when the matrix still exists.

 float * Matrix4d::getData() { return &_m00; } 
+1
source

Just enter the address of the first item.

 inline float* asArray { return &_m00 ; } 

You can consider any structure as an array .

 Matrix4d matrix ; // init with values.. functionThatExpectsArrayOfFloat( &matrix._m00 ) ; 
+1
source

Source: https://habr.com/ru/post/1302368/


All Articles