C ++: constructor / initializer for an array?

I am familiar with C ++ constructors and initializers:

class Foo { int x; public: Foo(int _x) : x(_x) {} }; Foo foo1(37); Foo foo2(104); 

My question is that I have to implement a class that has a member, which is a 3x6 array. How do I do something similar to the above?

 class Bar { int coeff[3][6]; public: // what do I do for a constructor???? }; 

edit: for a simple array, I would do the following: I just don't know how to do this for the class:

  static int myCoeffs[3][6] = {{ 1, 2, 3, 4, 5, 6}, { 7, 8, 9, 10, 11, 12}, { 13, 14, 15, 16, 17, 18}}; 

edit 2: For various reasons (for example, it is an embedded system with restrictions) I do not need to use Boost, so if it offers a solution, I cannot use it.


UPDATE I am not tied to the initializer. It is normal to do this in the constructor body, and it should not be inline either. I'm just looking for the right way to instantiate a class that needs an array of coefficients, without spoiling the pointer or anything else.

+6
c ++ arrays constructor
source share
6 answers

You can not. In C ++ 03, you cannot initialize an array in the ctor initialization list. However, you can do this in the body of the constructor (technically, this is no longer initialization).

it

 struct x { int a[4]; x():a({1,2,3,4}) //illegal { a[0] = 1; etc. } }; 

Edit: after editing the question here is a way to do it

 #include <algorithm> struct x { int arr[3][4]; x(int (&arg)[3][4]) { std::copy(&arg[0][0], &arg[0][0]+3*4, &arr[0][0]); } }; 
+4
source share

I don't know if that sounds too obvious, but why not just copy the values?

 class Foo { static const int X = 3; static const int Y = 6; int mCoeff[X][Y]; void setCoeff(int coeff[X][Y]) { for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { mCoeff[i][j] = coeff[i][j]; } } } public: Foo(int coeff[X][Y]) { setCoeff(coeff); } }; 

Hope this helps. Best wishes.

+2
source share

You cannot (generally) pass an arbitrary multidimensional array of functions to C or C ++, as in Java (I mention Java, because where your question history tells you that your experience lies).

There really is no way to support anything like array initializers for your class in the current standard, although there are some plans to change this in the next version, C ++ 0x.

If you want this to have the flexibility of, say, Java arrays (where clients can resize the specified array, you have several options. The first and best would be to use std::vector<> , which implements a dynamic array for you, but you can be limited to this built-in platform, you can also implement things yourself as a one-dimensional array and accept a block of dynamic memory.

 class Foo { int *theArray_; unsigned int rows_; unsigned int cols_; public: Foo(int *theArray, int rows, int cols) : theArray_(theArray) , rows_(rows) , cols_(cols) { } void UseTheArray() { //Access array[5][6] theArray[5*cols+6]; } }; 

Please note that in this case, the caller is responsible for transferring memory. Also consider using std::auto_ptr above instead of a raw pointer to find out who is responsible for destroying a block of memory when destroying an object.

Also note that you should not use the name _x - names beginning with an underscore contain a whole bunch of rules that you probably want to avoid thinking about.

EDIT: This is part of my original answer; it is not right:

<y> A multidimensional array in C ++ is just one block of memory. It is possible to accept a multidimensional array, similar to this only in one case - where you know in advance the exact dimensions of the array in the class. Then you can write something like:

 class Foo { int coeff[3][6]; public: Foo(int _x[3][6]) : coeff(_x) {} }; 

Please note that the size of the array is fixed and cannot be changed by clients of your class. C>

+1
source share

In C ++ 03, since you cannot initialize the array in the initialization list, you can write a generic function template to fill the array with the given array, as shown in the following example

 template<typename T, size_t N> void array_fill(T (&dest)[N], T (&src)[N]) { for ( size_t i = 0 ; i < N ; i++ ) dest[i] = src[i]; } struct A { int a[5]; A(int (&i)[5]) { array_fill(a, i); } void print() { for ( int i = 0 ; i < 5 ; i++ ) cout << a[i] << " "; } }; int main() { int a[5] = {1,2,3,4,5}; A obj(a); obj.print(); return 0; } 

Output:

 1 2 3 4 5 

Running at ideon: http://ideone.com/pFcrv

Well, there is already std::copy that you can use.

+1
source share

If you need to go this way, I would just go for a for loop, iterate over the elements of a two-dimensional array and initialize them. Or, if you're going for speed, something like memset will do the trick.

 memset(&coeff,0,3*6*sizeof(int)) 
-one
source share

You can do it as follows:

 #include <algorithm> class Bar { int coeff[3][6]; public: // what do I do for a constructor???? Bar() { int myCoeffs[3][6] = { { 1, 2, 3, 4, 5, 6}, { 7, 8, 9, 10, 11, 12}, { 13, 14, 15, 16, 17, 18} }; std::swap(coeff, myCoeffs); } }; 
-one
source share

All Articles