C ++标准关于继承成员的地址 [英] C++ Standard On The Address of Inherited Members

查看:126
本文介绍了C ++标准关于继承成员的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准是否说明继承成员地址的任何内容?例如,如果我继承一个int成员或非虚方法,它是否说它的地址或虚拟成员:如果我不覆盖它,如果我做?构造函数,如果我使用以前的构造函数?运算符,重载运算符,模板成员?

Does the C++ standard say anything on the address of inherited members? For example if I inherit an int member or a non - virtual method, does it say anything about its address, or a virtual member: if I dont overide it, if I do? The constructor, if I use a previous constructor? Operators, overloaded operators, template members? Does it say anything about these things?

推荐答案

标准,第1.8节是关于C ++对象模型。

Standard, section 1.8 is about the C++ object model.

它不多说:对象是一个内存区域(但它可能包含未使用的区域用于对齐的目的),可以包含子对象(成员子对象或基类子对象或数组子对象)。从完整对象的定义,您可以推断子对象包含在其对象的内存区域中。它说,两个不同的对象既不是位字段也不是基类子对象应有不同的地址。

It doesn't say much: the object is a region of memory (but it may contain unused zones for alignment purpose) and can contain subobjects (member subobjects or base class subobjects, or array subobjects). From the definition of a complete object you can infer that subobjects are included in the memory region of their object. And it says that two distinct objects that are neither bitfields nor base class subobjects shall have distinct adresses.

第9.2 / 15节提供了关于对象中的地址顺序的一些附加信息:

Section 9.2/15 gives some additional information about the order of the addresess within an object :


具有相同访问
控制的(非联合)类的非静态数据成员被分配,以便以后的成员在类对象中具有更高的
地址。未指定具有不同访问控制的非静态
数据成员的分配顺序。
实现一致性要求可能导致两个相邻成员
不能立即分配;因此可能需要
来管理虚拟函数和
虚拟基类的空间。

Nonstatic data members of a (non-union) class with the same access control are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified. Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions and virtual base classes.

当所有联合成员都是以相同类型序列开始的结构时,关于联合的句子。然后允许检查公共部分,从中可以推断出它们必须具有相同的地址。

There are a couple of sentences about unions when all the union members are structs starting with the same sequence of types. Then it is allowed to "inspect the common parts", from which you can deduce that they must have the same address.

最后,我在9.2 / 21找到了最后一个:

Finally, I've found a last one in 9.2/21:


指向使用reinterpret_cast适当转换的标准布局结构体对象的指针指向其初始成员(或者如果
成员是位字段,则指向它所在的单元)和
反之。

A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa.

基本上就是这样。你看到这里有很多实现定义的东西,关于每个对象的确切布局。

And basically, that's it. You see there is a lot of implementation defined stuff here, about the exact layout of each objects.

不幸的是,你甚至不能说基地类子对象的地址和它的派生对象:也可以有多个继承。所以标准不使用地址假设:它更像状态:如果一个基类是可访问的,可以隐式地将指向一个派生类的指针转换为指向该基类的指针

Unfortunately, you can't even say much about the address of a base class subobject and it's derived object: there could be multiple inheritance as well. So the standard doesn't use address assumptions: it rathers states things like : "If a base class is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class"

这篇关于C ++标准关于继承成员的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆