简体   繁体   English

C ++中的结构继承与类继承

[英]Struct inheritance vs class inheritance in C++

I just discovered from this Q/A that structs are inheritable in C++ but, is it a good practice, or is it preferable to use classes? 我刚刚从这个Q / A中发现结构可以在C ++中继承,但是,这是一种好习惯还是使用类呢? In which cases is preferable and in which ones is not? 在哪种情况下更可取?

I have never needed this, but now I have a bunch of messages of different types, but same longitude. 我从不需要这个,但是现在我收到了一堆不同类型但相同经度的消息。 I got them in binary in a char array, and I just copy them with memcpy to the struct to fill its fields (I don't know if it is even possible to do it with std::copy). 我将它们以二进制形式放在char数组中,然后将它们与memcpy复制到结构中以填充其字段(我不知道是否有可能使用std :: copy做到这一点)。

I guess it would be great to be able to inherit every struct from a base struct with common headers, that is why I searched for this. 我想能够从具有公共标头的基本结构中继承每个结构是很棒的,这就是为什么我要搜索此结构。 So a second question would be: if I do this with classes, is it possible to do a memcpy (or std:copy) from a buffer to a class? 因此,第二个问题是:如果我对类进行此操作,是否可以从缓冲区到类进行memcpy(或std:copy)?

Whether you can use a bitwise copy or not has nothing to do with the struct or class tag and only depends on whether said struct or class is_trivially_copiable . 是否可以使用按位复制与structclass标签无关,仅取决于所述structclass is_trivially_copiable Whether they are is defined in the Standard (9/6 [class]) and it basically boils down to not having to declare any other special member methods than constructors . 它们是否在Standard(9/6 [class])中定义,并且基本上可以归结为不必声明除构造方法之外的任何其他特殊成员方法

The bitwise copy is then allowed by the Standard in 3.9/2 [basic.types] 然后,按标准3.9 / 2 [basic.types]允许按位复制

For any object (other than a base-class subobject) of trivially copyable type T , whether or not the object holds a valid value of type T , the underlying bytes (1.7) making up the object can be copied into an array of char or unsigned char . 对于平凡可复制类型T任何对象(基类子对象除外),无论该对象是否持有类型T的有效值,组成该对象的基础字节(1.7)都可以复制到char或array中。 unsigned char If the content of the array of char or unsigned char is copied back into the object, the object shall subsequently hold its original value. 如果将charunsigned char数组的内容复制回该对象,则该对象随后应保留其原始值。 [ Example: [ 示例:

 #define N sizeof(T) char buf[N]; T obj; // obj initialized to its original value std::memcpy(buf, &obj, N); // between these two calls to std::memcpy, // `obj` might be modified std::memcpy(&obj, buf, N); // at this point, each subobject of `obj` // of scalar type holds its original value 

—end example ] —末例 ]

Note: a bitwise copy of padding bytes will lead to reports in Valgrind. 注意:填充字节的按位复制将导致Valgrind中的报告。

Using std::copy to the same effect: 使用std::copy具有相同的效果:

char const* b = reinterpret_cast<char const*>(&obj);
std::copy(b, b + N, buf);

The only difference between struct and class is the default access modifier to its members. structclass之间的唯一区别是对其成员的默认访问修饰符。 In struct it's public and in class it's private (until stated otherwise). struct它是public ,在class它是private (除非另有说明)。 Besides that struct and class are identical in C++. 此外,C ++中的structclass相同。 Sometimes structs are prefered for PDO (Plain Data Objects) over classes for readability but that's really up to a coding convention. 有时,出于可读性考虑,相对于类而言,PDO(普通数据对象)更喜欢使用结构,但这实际上取决于编码约定。

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

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