简体   繁体   English

为什么我不能对另一个类的静态char []执行sizeof?

[英]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: 要使其按照您的意愿工作,您可以:

  • add the size of the First::TEST array to its declaration ( static const char* TEST[2]; ) First::TEST数组的大小添加到其声明中( static const char* TEST[2];
  • add a new static method that returns the sizeof 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.

相关问题 C ++为什么我可以在类定义中初始化静态const char而不是静态const double? - C++ Why can I initialize a static const char but not a static const double in a class definition? 如何获得char *的真实长度? strlen和sizeof(target)/ sizeof(target *)之间有区别吗? - How can I get the real length of char*? Different between strlen and sizeof(target)/sizeof(target*)? 为什么我可以对static * cast使用void *但不能对char *使用 - Why can I use static_cast With void* but not With char* 为什么我不能 static_cast char* 到 std::byte*? - Why I can't static_cast char* to std::byte*? 为什么sizeof派生类是8? - Why is sizeof Derived Class is 8? 为什么我不能在类的另一个函数的声明中使用静态constexpr的结果? - Why can't I use the result of a static constexpr in the declaration of another function in a class? 为什么我不能在char *和unsigned char *之间进行static_cast? - Why can't I static_cast between char * and unsigned char *? 为什么sizeof()返回带有2个字节的此类的4 - Why is the sizeof() returning 4 for this class with 2 bytes 为什么在对同一个字符数组使用 sizeof 时会得到不同的结果? - Why I am getting varied results when using sizeof for same char array? How can I call a non-static member function of a class from a static member function of another class? - How can I call a non-static member function of a class from a static member function of another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM