C++ 变量数组作为类成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10459735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ variable array as class member
提问by user1376415
I have a small C++ program defined below:
我有一个小的 C++ 程序定义如下:
class Test
{
public:
int a;
int b[a];
};
When compiling, it produces an error:
编译的时候会报错:
testClass.C:7:5: error: invalid use of non-static data member ‘Test::a'
testClass.C:8:7: error: from this location
testClass.C:8:8: error: array bound is not an integer constant before ‘]' token
How do I learn about what the error message means, and how do I fix it?
我如何了解错误消息的含义,以及如何修复它?
回答by demi
You cannot use an array with undefined size in compile time. There are two ways: define a
as static const int a = 100;
and forget about dynamic size or use std::vector
, which is safer than the manual memory management:
您不能在编译时使用未定义大小的数组。有两种方法:定义a
为static const int a = 100;
并忘记动态大小或使用std::vector
,这比手动内存管理更安全:
class Test
{
public:
Test(int Num)
: a(Num)
, b(Num) // Here Num items are allocated
{
}
int a;
std::vector<int> b;
};
回答by Nicol Bolas
Unless you dynamically allocate them (such as with new[]
), arrays in C++ musthave a compile-time constant as a size. And Test::a
is not a compile-time constant; it's a member variable.
除非您动态分配它们(例如使用new[]
),否则C++ 中的数组必须具有编译时常量作为大小。并且Test::a
不是编译时常量;它是一个成员变量。
回答by Ahmed Jolani
Let's go through the complete reason behind this, first of all when you declare a class initially, any member variable has not been allocated any memory, so public or private methods have no value. That was a quick tip for you, now for your problem:
让我们来看看这背后的完整原因,首先,当您最初声明一个类时,任何成员变量都没有分配任何内存,因此公共或私有方法没有价值。这对您来说是一个快速提示,现在可以解决您的问题:
You can't do that outside the class since any array size should be known before the compilation because any statically declared array resides in the function's stack frame, and the compiler needs to know how much memory to allocate exactly. The only segment in memory that is resized by the programmer is the heap. So, whenever you want to have a dynamically allocated array size, you need to declare it to reside in the heap, and you do that like so:
您不能在类之外这样做,因为在编译之前应该知道任何数组大小,因为任何静态声明的数组都驻留在函数的堆栈帧中,并且编译器需要知道要准确分配多少内存。内存中唯一由程序员调整大小的段是堆。因此,每当您想要动态分配的数组大小时,您都需要声明它驻留在堆中,您可以这样做:
int a;
cin >> a;
int * b = new int[a];
That's the correct way to declare an array with an unknown size (size determined during the runtime), to integrate this with your class here is how you do it, and recall that any class private or public attributes have no memory - They are just declarations that should not contain any initialization somewhere else in the member methods or outside the class - this is because they are public, as in your case - and of course after declaring an instance of the class e.g Test t. Anyway, here is how you do it within the class:
这是声明具有未知大小(在运行时确定的大小)的数组的正确方法,在这里将它与您的类集成是您如何做到的,并回想一下任何类的私有或公共属性都没有内存 - 它们只是声明不应该在成员方法或类之外的其他地方包含任何初始化 - 这是因为它们是公共的,就像你的情况一样 - 当然在声明类的实例之后,例如 Test t。无论如何,这是您在课堂上的做法:
class Test
{
public:
int a;
int * b;
Test(int Ia=1) {
a = Ia;
b = new int[a];
}
~Test() { delete b; }
};