前向声明为结构 vs 类

2021-12-23 00:00:00 c struct forward-declaration c++

我在一个类的 C++ 前向声明中发现了一个错误,它被错误地声明为 struct Book 而不是 class Book.我认为 Book 曾经是一个结构体,后来变成了一个类,但前向声明仍然存在.

I found a mistake in a C++ forward declaration of a class, which was wrongly declared as struct Book instead of class Book. I think Book used to be a struct, got changed to a class but the forward declarations remained.

Book.h:

class Book {
    ...
};

库.h:

struct Book;

class Library {
    std::vector<Book*> books;
};

没有编译器警告,生成的程序似乎运行良好.这让我很好奇:理论上,这会导致问题吗?它只是一个指针,是的,但是例如,如果类有虚方法、多重继承,指针会不同吗?

There were no compiler warnings and the resulting program seemed to work fine. It made me curious: in theory, can this ever cause a problem? It's just a pointer, yes, but for example, if the class had virtual methods, multiple inheritance, could the pointers be different?

我知道关于默认公共/私有的类/结构之间的差异,但我特别询问了前向声明和交换它们的可能后果.

I know the differences between class/struct regarding default public/private but I'm asking specifically about the forward declarations and possible consequences of swapping them.

UPDATE 较新版本的 Xcode 现在会在不匹配时发出此警告:

UPDATE newer versions of Xcode now give this warning when there's a mismatch:

Struct 'Book' was previously declared as a class; this is valid, but may result in linker errors under the Microsoft C++ ABI

推荐答案

structclass 就前向声明而言是完全可以互换的.即使对于定义,它们也只影响对象成员的默认访问说明符,其他一切都是等价的.您总是定义对象的类".

struct and class are completely interchangeable as far as forward declarations are concerned. Even for definitions, they only affect the default access specifier of the objects members, everything else is equivalent. You always define "classes" of objects.

必须在 class 上使用 struct 的唯一地方是为 c 绑定.

The only place where struct must be used over class, is when forward declaring opaque data for c bindings.

关于您的

我知道关于默认公共/私有的类/结构之间的差异,但我特别询问了前向声明和交换它们的可能后果.

I know the differences between class/struct regarding default public/private but I'm asking specifically about the forward declarations and possible consequences of swapping them.

Visual C++ 产生警告 C4099.它做到了,因为其功能的名称修饰包含您使用的关键字.因此,程序可能无法正确链接.因此,在使用 VC++ 时,完全符合标准的代码可能无法链接(Microsoft 的一个笨蛋举动,AFAIC).

Visual C++ produces warning C4099. It does it, because the name decoration for its functions incorporates the keyword you used. Therefore, programs may fail to link properly. So perfectly standard compliant code may not link when using VC++ (A bonehead move on Microsoft's part, AFAIC).

有关此警告的讨论,以及如果您受到纪律处分可以忽略它的原因,请参见 这里

A discussion of this warning, as well as why it can be ignored if you are disciplined, can be found here

相关文章