c语言对象数组初始化,如何在C对象中初始化数组

阅读

How to initialize an array in C之后,特别是:

Don’t overlook the obvious solution, though:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

我试过这样的事情:

#include

class Something {

private:

int myArray[10];

public:

Something() {

myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

}

int ShowThingy(int what) {

return myArray[what];

}

~Something() {}

};

int main () {

Something Thing;

std::cerr << Thing.ShowThingy(3);

}

我得到:

..\src\Something.cpp: In constructor 'Something::Something()':

..\src\Something.cpp:10:48: error: cannot convert '' to 'int' in assignment

在这种情况下显而易见的并不是那么明显.我真的希望我的阵列的启动也更加动态.

我累了:

private:

int * myArray;

public:

Something() {

myArray = new int [10];

myArray = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

}

对我来说这看起来很时髦,对编译器来说也是如此:

..\src\Something.cpp: In constructor 'Something::Something()':

..\src\Something.cpp:11:44: error: cannot convert '' to 'int*' in assignment

这也行不通:

private:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

有:

..\src\Something.cpp:6:20: error: a brace-enclosed initializer is not allowed here before '{' token

..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers

..\src\Something.cpp:6:51: error: 'constexpr' needed for in-class initialization of static data member 'myArray' of non-integral type

我一直做得很好,学习什么不起作用,但不是很好学习什么工作.

那么,我如何在类中使用数组的初始化列表{value,value,value}?

我一直试图弄清楚如何做一段时间并且非常困难,我有很多这样的列表我需要为我的应用程序制作.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值