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

发布于 2024-08-14 18:31:31 字数 837 浏览 10 评论 0原文

为什么下面的代码会产生编译错误?


编辑:我的原始代码不清楚 - 我已将代码分成单独的文件...


First.h

class First
{
public:
    static const char* TEST[];

public:
    First();
};

First.cpp

const char* First::TEST[] = {"1234", "5678"};

First::First()
{
    uint32_t len = sizeof(TEST); // fine
}

确定 First 类中的大小似乎没问题,但是...

Second.h

class Second
{
public:
    Second();
};

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
}

我收到以下错误: 'const char *[]':操作数大小非法

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

class First
{
public:
    static const char* TEST[];

public:
    First();
};

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...

Second.h

class Second
{
public:
    Second();
};

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∞梦里开花 2024-08-21 18:31:31

sizeof 仅适用于完整类型。 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

要使其按您希望的方式工作,您可以:

  • 将 First::TEST 数组的大小添加到其声明中 (static const char* TEST[2];)
  • 添加一个新的静态方法,返回 First::TEST 的大小。该方法不能是内联的,它必须在 First.cpp 中定义。

sizeof only works on complete types. const char* TEST[] is not a complete type until it is defined in 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];)
  • add a new static method that returns the sizeof First::TEST. The method cannot be inline, it would have to be defined in First.cpp.
邮友 2024-08-21 18:31:31

主要是因为First.cpp和Second.cpp的编译是相互独立的。

Second 中的 sizeof() (通常)在编译时解析,此时仅知道数组的声明,并且无法计算静态 尚未分配 的空间。请参阅http://en.wikipedia.org/wiki/Sizeof#sizeof_and_incomplete_types

Primarily because the compilation of First.cpp and Second.cpp are independent of each other.

sizeof() in Second is (generally) resolved at compile time, when only the array's declaration is known, and space for the static hasn't been allocated cannot be calculated. See http://en.wikipedia.org/wiki/Sizeof#sizeof_and_incomplete_types

攒眉千度 2024-08-21 18:31:31

当您展开 Second.cpp 时,它会变得更加清晰。这是正在编译的整个内容(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
    }
}

如果您看这里,First::TEST 显然没有大小,并且 sizeof(FIRST::TEST) 毫无意义。

为什么不直接有一个返回 TEST 长度的方法呢?

It gets clearer when you expand Second.cpp. This is the whole thing that is being compiled (1 compilation unit):

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.

Why not just have a method that returns the length of TEST?

怕倦 2024-08-21 18:31:31

对我来说,即使第一个 sizeof(TEST) 也无法编译,因为 TEST 尚未声明大小(仅在链接时解析)。

如果我将 TEST[] 更改为 TEST[10],则两种情况都会编译。

这两个结果都是我所期望的。

您使用哪个编译器?

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).

If I change TEST[] to, say, TEST[10], then both cases compile.

Both results are what I would expect.

Which compiler are you using?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文