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 类
基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01