C++初始化非静态成员数组

Mic*_*eck 15 c++ arrays default-constructor

我正在编辑一些使用如下定义的全局数组的旧C++代码:

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

我想让所有这些数组成为下面定义的Robot类的私有成员.但是,当我声明数据成员时,C++编译器不允许我初始化数据成员.

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

我想在Robot()构造函数中初始化这六个数组的元素.除了逐个分配每个元素之外,还有什么方法可以做到这一点吗?

iam*_*ind 15

如果您的要求确实允许,那么您可以将这5个数组作为static类的数据成员,并在定义.cpp文件时初始化它们,如下所示:

class Robot
{
  static int posLShd[5];
  //...
};
int Robot::posLShd[5] = {250, 330, 512, 600, 680}; // in .cpp file
Run Code Online (Sandbox Code Playgroud)

如果那是不可能的话,像往常一样用不同的名称声明这个数组,并memcpy()用于构造函数中的数据成员.

编辑:对于非静态成员,template可以使用以下样式(对于任何类型int).要更改大小,请同样重载元素数量:

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

C++ 11

数组初始化现在变得微不足道了:

class Robot
{
   private:
       int posLShd[5];
       ...
   public:
       Robot() : posLShd{0, 1, 2, 3, 4}, ...
       {}
};
Run Code Online (Sandbox Code Playgroud)

  • 这个答案最好的部分是在最后,就像编辑过的老式答案经常出现的情况一样。 (3认同)

and*_*ini 12

您可以将其设置为静态,也可以使用C++ 0x中引入的新初始化

class Robot
{
private:
  int posLShd[5];
  static int posLArm[5];
  // ...
public:
  Robot() :
    posLShd{250, 330, 512, 600, 680} // only C++0x                                                                                     
  {}

  ~Robot();
};

int Robot::posLArm[5] = {760, 635, 512, 320, 265};
Run Code Online (Sandbox Code Playgroud)


ild*_*arn 5

要将另一种方法混入其中(并且不会static像大多数其他答案那样告诉您使数组数据成员- 我假设知道它们是否应该是static),这是零开销方法我使用:创建static成员函数并让它们返回std::array<>(或者boost::array<>如果您的编译器太旧而无法提供std::std::tr1::实现):

class Robot
{
    static std::array<int, 5> posLShd_impl() { std::array<int, 5> x = {{ 250, 330, 512, 600, 680 }}; return x; }
    static std::array<int, 5> posLArm_impl() { std::array<int, 5> x = {{ 760, 635, 512, 320, 265 }}; return x; }
    static std::array<int, 5> posRShd_impl() { std::array<int, 5> x = {{ 765, 610, 512, 440, 380 }}; return x; }
    static std::array<int, 5> posRArm_impl() { std::array<int, 5> x = {{ 260, 385, 512, 690, 750 }}; return x; }
    static std::array<int, 5> posNeck_impl() { std::array<int, 5> x = {{ 615, 565, 512, 465, 415 }}; return x; }
    static std::array<int, 5> posHead_impl() { std::array<int, 5> x = {{ 655, 565, 512, 420, 370 }}; return x; }

    std::array<int, 5> posLShd;
    std::array<int, 5> posLArm;
    std::array<int, 5> posRShd;
    std::array<int, 5> posRArm;
    std::array<int, 5> posNeck;
    std::array<int, 5> posHead;
public:
    Robot();
};

Robot::Robot()
  : posLShd(posLShd_impl()),
    posLArm(posLArm_impl()),
    posRAhd(posRAhd_impl()),
    posRArm(posRArm_impl()),
    posNeck(posNeck_impl()),
    posHead(posHead_impl())
{ }
Run Code Online (Sandbox Code Playgroud)

  • @weberc2:[NRVO](http://en.wikipedia.org/wiki/Return_value_optimization) 使这在实践中不是问题。 (2认同)