简体   繁体   English

结构和类在C ++中是否真的相同?

[英]Are structures and classes really equivalent in C++?

Assuming that struct is similar to class except for the default member privacy, why the following code does not compile? 假设structclass相似,除了默认的成员隐私,为什么下面的代码不能编译?

#define class struct
#include <iostream>


int main()
{
     return 0;
}

Update 更新

In file included from /usr/include/c++/7/bits/stl_algobase.h:61:0,
                 from /usr/include/c++/7/vector:60,
                 from main.cpp:5:
/usr/include/c++/7/bits/cpp_type_traits.h:86:18: error: ‘struct std::_Sp’ is not a valid type for a template non-type parameter
   template<class _Sp, class _Tp>
                  ^~~
compilation terminated due to -Wfatal-errors.

The behaviour of your code is undefined : The C++ standard does not permit the redefinition of keywords. 代码的行为未定义 :C ++标准不允许重新定义关键字。

Perhaps the specific failure is due to template<class T> being valid syntax but template<struct T> is not? 也许特定的失败是由于template<class T>是有效的语法而template<struct T>不是? The preprocessor step seems to have ruined the implementation of <iostream> on your platform. 预处理器步骤似乎破坏了平台上<iostream>的实现。

(A class and struct are the same in all respects aside from the default access of member variables and functions - including inheritance, as you point out. (除了成员变量和函数的默认访问权限外, classstruct在所有方面都是相同的 - 包括继承,正如您所指出的那样。

The C++ standard allows you to forward declare as a struct and implement as a class and vice-versa, although some compilers; C ++标准允许您将声明转发为struct并实现为class ,反之亦然,尽管有些编译器; older versions of MSVC for example; 例如旧版本的MSVC; disallow that.) 不允许这样做。)

A class and a struct are equivalent (except for the default privacy) classstruct是等价的(默认隐私除外)

but grammar doesn't allow struct in template: 但语法不允许在模板中使用struct

 template <struct S> // Invalid
 /*..*/

whereas

 template <class C> // valid
 /*..*/

or 要么

 template <typename T> // valid
 /*..*/

The behavior is identical when defining members. 定义成员时,行为是相同的。

However, class and struct are not synonymous in every context. 但是, classstruct在每个上下文中都不是同义词。

template<class T> is a synonym for template <typename T> . template<class T>template <typename T>的同义词。 Your #define would turn it into template <struct T> , which is not allowed. 你的#define会把它变成template <struct T> ,这是不允许的。 I presume this is why you're getting errors on templates in the standard headers. 我认为这就是为什么你在标准标题中的模板上出现错误的原因。

In addition to other answers, 除了其他答案,

To hack into a private visibility scope, the least intrusive way is probably to copy the header to your code base (making sure it goes first in the -I path), and make one of your classes a friend of the third party API class that needs hacking. 要破解私有可见范围,最不具侵入性的方法可能是将标题复制到您的代码库(确保它首先在-I路径中),并使您的一个类成为第三方API类的朋友需要黑客攻击。 This would be my tool to temporarily patching a suboptimal third party API for production usage. 这将是我临时修补次优第三方API以供生产使用的工具。

Another, a more "cowboy" way is to #define private public for some limited scope. 另一种更“牛仔”的方式是#define private public在一些有限的范围内。 This one I would not do in a production code base, probably. 这个我不会在生产代码库中做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM