[英]Why can I not perform a sizeof against a static char[] of another class?
Why does the following code generate a compile error? 为什么以下代码会生成编译错误?
Edit: My original code wasn't clear - I've split the code up into separate files... 编辑:我的原始代码不清楚 - 我已将代码拆分为单独的文件...
First.h First.h
class First
{
public:
static const char* TEST[];
public:
First();
};
First.cpp First.cpp
const char* First::TEST[] = {"1234", "5678"};
First::First()
{
uint32_t len = sizeof(TEST); // fine
}
Determining the size within the First
class seems fine, however... 确定First
类中的大小似乎很好,但是......
Second.h Second.h
class Second
{
public:
Second();
};
Second.cpp Second.cpp
#include "First.h"
Second::Second()
{
uint32_t len = sizeof(First::TEST); // error
uint32_t elements = (sizeof(First::TEST) / sizeof(First::TEST[0])); // error
}
I get the following error: 'const char *[]': illegal sizeof operand
我收到以下错误: 'const char *[]': illegal sizeof operand
sizeof only works on complete types. sizeof仅适用于完整类型。 const char* TEST[]
is not a complete type until it is defined in First.cpp. const char* TEST[]
在First.cpp中定义之前不是完整类型。
sizeof(char*[10]) == sizeof(char*) * 10 == 40
sizeof(short[10]) == sizeof(short) * 10 == 20
// a class Foo will be declared
class Foo;
sizeof(Foo) == //we don't know yet
// an array bar will be defined.
int bar[];
sizeof(bar) == sizeof(int) * ? == //we don't know yet.
// actually define bar
int bar[/*compiler please fill this in*/] = { 1, 2, 3 };
sizeof(bar) == sizeof(int) * 3 == 12
// note bar is exactly the right size
// an array baz is defined.
int baz[4];
sizeof(baz) == sizeof(int) * 4 == 16
// initialize baz
int baz[4] = { 1, 2, 3 };
sizeof(bar) == sizeof(int) * 4 == 16
// note baz is still 4 big, the compiler doesn't control its size
To get this to work as you wish, you can: 要使其按照您的意愿工作,您可以:
First::TEST
array to its declaration ( static const char* TEST[2];
) 将First::TEST
数组的大小添加到其声明中( static const char* TEST[2];
) First::TEST
. 添加一个返回sizeof First::TEST
大小的新静态方法。 The method cannot be inline, it would have to be defined in First.cpp. 该方法不能内联,必须在First.cpp中定义。 Primarily because the compilation of First.cpp and Second.cpp are independent of each other. 主要是因为First.cpp和Second.cpp的编译是相互独立的。
sizeof() in Second is (generally) resolved at compile time, when only the array's declaration is known, and space for the static sizeof()in second(通常)在编译时解析,只有数组的声明是已知的,而静态的空间 hasn't been allocated 尚未分配 cannot be calculated. 无法计算。 See http://en.wikipedia.org/wiki/Sizeof#sizeof_and_incomplete_types 请参阅http://en.wikipedia.org/wiki/Sizeof#sizeof_and_incomplete_types
It gets clearer when you expand Second.cpp. 扩展Second.cpp时会更清楚。 This is the whole thing that is being compiled (1 compilation unit): 这是正在编译的整个事情(1个编译单元):
class First
{
public:
static const char* TEST[];
public:
First();
};
class Second
{
public:
Second();
Second::Second()
{
uint32_t len = sizeof(First::TEST); // error
uint32_t elements = (sizeof(First::TEST) / sizeof(First::TEST[0])); // error
}
}
If you look here, First::TEST clearly has no size, and sizeof(FIRST::TEST) is meaningless. 如果你看这里,First :: TEST显然没有大小,而sizeof(FIRST :: TEST)毫无意义。
Why not just have a method that returns the length of TEST? 为什么不只是有一个返回TEST长度的方法?
For me even the first sizeof(TEST)
fails to compile as TEST has not been declared with a size (that is only resolved at link time). 对我来说,即使第一个sizeof(TEST)
无法编译,因为TEST尚未使用大小声明(仅在链接时解析)。
If I change TEST[]
to, say, TEST[10]
, then both cases compile. 如果我将TEST[]
更改为TEST[10]
,则两种情况都会编译。
Both results are what I would expect. 两个结果都是我所期望的。
Which compiler are you using? 你使用的是哪个编译器?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.