Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Initializing Non-Static Member Array

I am working on editing some old C++ code that uses global arrays defined like so:

int posLShd[5] = {250, 330, 512, 600, 680};
int posLArm[5] = {760, 635, 512, 320, 265};
int posRShd[5] = {765, 610, 512, 440, 380};
int posRArm[5] = {260, 385, 512, 690, 750};
int posNeck[5] = {615, 565, 512, 465, 415};
int posHead[5] = {655, 565, 512, 420, 370};

I want to make all of these arrays private members of the Robot class defined below. However, the C++ compiler does not let me initialize data members when I declare them.

class Robot
{
   private:
       int posLShd[5];
       int posLArm[5];
       int posRShd[5];
       int posRArm[5];
       int posNeck[5];
       int posHead[5];
   public:
       Robot();
       ~Robot();
};

Robot::Robot()
{
   // initialize arrays
}

I want to initialize the elements of these six arrays in the Robot() constructor. Is there any way to do this other than assigning each element one by one?

like image 437
Michael Hornfeck Avatar asked Apr 13 '11 03:04

Michael Hornfeck


People also ask

Can I initialize Non-static data member inside static block?

In the static method, the method can only access only static data members and static methods of another class or same class but cannot access non-static methods and variables.

Where do you initialize a non-static class member that is a reference?

Member initialization Non-static data members may be initialized in one of two ways: 1) In the member initializer list of the constructor.

What is Nsdmi?

Initialisation of Data members As you can see, the variables get their default value in the place of declaration. There's no need to set values inside a constructor. The feature is called non-static data member initialization, or NSDMI in short.

How do you initialize all type const data members?

To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon.


1 Answers

If your requirement really permits then you can make these 5 arrays as static data members of your class and initialize them while defining in .cpp file like below:

class Robot
{
  static int posLShd[5];
  //...
};
int Robot::posLShd[5] = {250, 330, 512, 600, 680}; // in .cpp file

If that is not possible then, declare this arrays as usual with different name and use memcpy() for data members inside your constructor.

Edit: For non static members, below template style can be used (for any type like int). For changing the size, simply overload number of elements likewise:

template<size_t SIZE, typename T, T _0, T _1, T _2, T _3, T _4>
struct Array
{
  Array (T (&a)[SIZE])
  {
    a[0] = _0;
    a[1] = _1;
    a[2] = _2;
    a[3] = _3;
    a[4] = _4;
  }
};

struct Robot
{
  int posLShd[5];
  int posLArm[5];
  Robot()
  {
    Array<5,int,250,330,512,600,680> o1(posLShd);
    Array<5,int,760,635,512,320,265> o2(posLArm);
  }
};

C++11

The array initialization has now become trivial:

class Robot
{
   private:
       int posLShd[5];
       ...
   public:
       Robot() : posLShd{0, 1, 2, 3, 4}, ...
       {}
};
like image 82
iammilind Avatar answered Oct 06 '22 08:10

iammilind