确定构造函数初始化数组大小 [英] Determine array size in constructor initializer

查看:257
本文介绍了确定构造函数初始化数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code下面我想阵列时类的构造函数被调用被定义为大小为x的数组。我该怎么做?

  Class类
{
上市:
  int数组[];
  类(INT X):??? {}
}


解决方案

您不能初始化非const维数组不能在编译时(至少在当前的C ++标准计算的大小,据我所知)。

我推荐使用的std ::矢量< INT>将代替阵列。它提供了数组的语法为大部分的操作。

In the code below I would like array to be defined as an array of size x when the Class constructor is called. How can I do that?

class Class
{
public:
  int array[];
  Class(int x) : ??? { }
}

解决方案

You can't initialize the size of an array with a non-const dimension that can't be calculated at compile time (at least not in current C++ standard, AFAIK).

I recommend using std::vector<int> instead of array. It provides array like syntax for most of the operations.

这篇关于确定构造函数初始化数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆