在 C++ 中,我可以从结构派生一个类吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/577465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:02:40  来源:igfitidea点击:

in C++, can I derive a class from a struct

c++data-structures

提问by SmacL

The question says it all really. Am I allowed derive a class from a struct, or should I create a class that embeds my struct and defines copy constructors and an = operator to move between the two?

问题说明了一切。我是否允许从结构派生一个类,还是应该创建一个嵌入我的结构并定义复制构造函数和 = 运算符以在两者之间移动的类?

回答by Alex B

In C++ structis (almost) synonymous to a class(except of different default access level), so yes, you can.

在 C++struct中(几乎)与 a 同义class(除了不同的默认访问级别),所以是的,你可以。

struct A {
// fields are public by default
};

class B: public A {
// fields are private by default
};

I'm not familiar with MFC, but it looks like an attempt to maintain both C and C++ APIs.

我不熟悉 MFC,但它看起来像是试图同时维护 C 和 C++ API。

回答by Johannes Schaub - litb

Of course you are. What is different regarding the inheritance is that if you define a class, it will inherit private from other classes/structs. If you define a struct and derive it from a class or struct, it will be a public inheritance by default:

你当然是。继承的不同之处在于,如果您定义一个类,它将从其他类/结构继承私有。如果你定义了一个结构体并从一个类或结构体派生出来,默认情况下它将是一个公共继承:

// the following two are equivalent except that one time class is 
// used and the other time struct
struct f : g { };
class f : public g { };

If you wonder how you should wrap C structs - i would embed them as a member into the class. Because the C structs were not designed to be inherited from (think of the neither protected nor virtual destructor in the C structs - they can't have one). They are simple aggregates that collect data. And putting them as members uses them as such (as in "a point has a pair of an x and an y coordinate").

如果您想知道应该如何包装 C 结构 - 我会将它们作为成员嵌入到类中。因为 C 结构不是为了继承而设计的(想想 C 结构中既不是受保护的也不是虚拟的析构函数——它们不能有)。它们是收集数据的简单聚合。并将它们作为成员使用它们(例如“一个点具有一对 x 和一个 y 坐标”)。

C structs also expose members that possibly should not be exposed in the class. Containment allows precise control over what is exposed and what not by get/set functions and you can still let it give you a copy or reference to the contained naked C struct object.

C 结构还公开了可能不应在类中公开的成员。包含允许通过 get/set 函数精确控制公开的内容和不公开的内容,您仍然可以让它为您提供对包含的裸 C 结构对象的副本或引用。

回答by Stephen C. Steel

Yes. you can derive a class from a struct. In C++, a struct is simply a class where the default access is public rather than private. Deriving a class from a struct that only adds non-virual member functions and/or static functions is a useful technique to provide a C++ interface while maintaining compatability with a C style API.

是的。您可以从结构派生类。在 C++ 中,结构只是一个类,其中默认访问是公共的而不是私有的。从仅添加非虚拟成员函数和/或静态函数的结构派生类是一种有用的技术,可提供 C++ 接口,同时保持与 C 风格 API 的兼容性。

This is exactly the approach used by MFC for many of the C structs (contrary to what you state in your question).

这正是 MFC 用于许多 C 结构的方法(与您在问题中陈述的相反)。

For example, the CRect class is publicly derived from struct tagRECT (the more commonly used name RECT is a typededf for struct tagRECT). Because struct tagRECT defines all the data members, and CRect adds only non-virtual member functions, the memory layout of CRects and RECTs are identical - you can use a CRect as an argument for any function expected a RECT and vice-versa.

例如,CRect 类是从 struct tagRECT 公开派生的(更常用的名称 RECT 是 struct tagRECT 的 typededf)。因为 struct tagRECT 定义了所有数据成员,而 CRect 只添加了非虚拟成员函数,所以 CRect 和 RECT 的内存布局是相同的——您可以使用 CRect 作为任何需要 RECT 的函数的参数,反之亦然。