【发布时间】:2011-02-12 15:28:50
【问题描述】:
对于下面的 C++ 代码片段:
class Foo {
int a[]; // no error
};
int a[]; // error: storage size of 'a' isn't known
void bar() {
int a[]; // error: storage size of 'a' isn't known
}
为什么成员变量也不会导致错误?这个成员变量是什么意思?
我通过 CodeBlocks 8.02 使用 gcc 版本 3.4.5(mingw-vista special)。
在 Visual Studio Express 2008 - Microsoft(R) C/C++ Optimizing Compiler 15.00.30729.01 for 80x86 上,我收到以下消息:
class Foo {
int a[]; // warning C4200: nonstandard extension used : zero-sized array in struct/union - Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
};
int a[];
void bar() {
int a[]; // error C2133: 'a' : unknown size
}
现在,这也需要一些解释。
【问题讨论】:
-
标题中提到了“静态数组”。您的问题中这些“静态数组”在哪里?我没有看到对任何“静态数组”的单一引用。
-
我的意思是静态分配的数组。如果您认为有必要,请随时编辑问题。谢谢。
标签: c++ class compiler-construction static-array