Forward declaration as struct vs class(前向声明为结构 vs 类)
问题描述
我在一个类的 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
推荐答案
struct
和 class
就前向声明而言是完全可以互换的.即使对于定义,它们也只影响对象成员的默认访问说明符,其他一切都是等价的.您总是定义对象的类".
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
这篇关于前向声明为结构 vs 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:前向声明为结构 vs 类


- 如何对自定义类的向量使用std::find()? 2022-11-07
- 近似搜索的工作原理 2021-01-01
- C++ 协变模板 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 从python回调到c++的选项 2022-11-16
- Stroustrup 的 Simple_window.h 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01