初始化非静态成员数组
C++ Initializing Non-Static Member Array
我正在编辑一些旧的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};
我想让所有这些数组成为下面定义的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
}
我想在Robot()构造函数中初始化这六个数组的元素。除了逐个赋值元素之外,还有别的方法吗?
如果您的要求确实允许,那么您可以将这5个数组作为类的static
数据成员,并在.cpp文件中定义时初始化它们,如下所示:
class Robot
{
static int posLShd[5];
//...
};
int Robot::posLShd[5] = {250, 330, 512, 600, 680}; // in .cpp file
如果不可能,那么像往常一样用不同的名称声明这些数组,并在构造函数中使用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);
}
};
c++ 11
数组初始化现在变得微不足道了:
class Robot
{
private:
int posLShd[5];
...
public:
Robot() : posLShd{0, 1, 2, 3, 4}, ...
{}
};
您可以将其设置为静态,或者使用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};
将另一种方法(并且没有告诉您将数组数据成员设置为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())
{ }
除了逐个分配每个元素之外,还有什么方法可以做到这一点吗?
如果你想用一些默认值填充数组的所有元素,可以使用std::fill
。
#include <algorithm>
// ...
Robot::Robot()
{
std::fill(posLShd, posLShd+5, 13 ) ; // 13 as the default value
// Similarly work on with other arrays too.
}
如果数组的每个元素都需要用不同的值填充,那么在每个索引处赋值是唯一的选择。
将全局变量保留在代码中,然后使用memcpy()初始化局部数组,将全局数组的内容复制到局部数组中。
这与现在的问题只有轻微的关系,但这是一个特殊的情况,它完全解决了重复的问题。
0初始化是c++语言中数组的一种特殊情况。如果初始化列表比数组短,则将其余元素初始化为零。例如,重复问题的要求是对类的所有成员进行零初始化,包括构造函数中最后一个数组的所有元素:
class myprogram {
public:
myprogram ();
private:
double aa,bb,cc;
double G_[2000];
};
对于构造函数定义来说这就足够了:
myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0), G_{0.} {}
,因为G_
的第一个元素被显式初始化为值0.
,所有其他元素都被初始化为0。
// class definition with incomplete static member could be in a header file
Class Robot {
static const int posLShd[5];
....
// this needs to be placed in a single translation unit only
const int Robot::posLShd[5] = {250, 330, 512, 600, 680};
不完全是这样,尽管我同意stefaanv的评论——如果它们之前是全局的,那么将它们设置为静态会让你得到"简单的赋值",并且它们看起来好像是常量静态的。
如果这些值是您偶尔更改的,您可以考虑在创建类时从外部文件中读取它们,这样可以避免重新编译。
您还可以考虑使用std::vector来代替固定数组,因为它提供了一些特性。
我错过了什么吗?下面的代码可以工作。只需声明成员并立即初始化即可。
#include <iostream>
class Robot {
public:
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};
public:
Robot() {}
~Robot() {}
};
int main () {
Robot obj;
for (int i = 0;i < 5;i++) {
std::cout << obj.posRArm[i] << std::endl;
}
}
- 为什么我无法访问指向数组中成员函数的指针?
- 如何将结构/联合数组的成员传递到函数中
- 使用带有参数包的数组的成员数组初始化类
- 有没有办法给数组的成员指定不同的名称?
- 使用包含结构的数组的成员函数
- C++:释放动态数组(结构成员)和指向此结构的指针的方法
- C 用数组作为成员创建对象
- 用另一个 constexpr 数组对成员数组进行大括号初始化
- 来自数组的成员和比特菲尔德的结构
- 如何创建指向数组类成员的指针?
- 将数组的成员设置为零
- "Empty"数组\向量成员 C++
- 功能指针数组(包括成员功能)投掷模板专业化错误
- 另一个结构内部的结构数组的成员变量没有通过引用传递
- 正确转换 2D 数组的成员变量
- 常量数组类成员初始化
- 为具有对象或对象数组作为成员的类运行不同的代码
- C++ 如何指向数组的成员
- 将包含数组数据成员的结构保存到文件
- 数组类成员初始化(C++)