C ++消歧:子对象和子类对象 [英] C++ Disambiguation: subobject and subclass object

查看:104
本文介绍了C ++消歧:子对象和子类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上标题是什么。我一直将Base对象称为子对象。这是正确的,子对象 == 超类对象也是正确的吗?首选哪一个?

Basically what the title says. I have been referring to Base objects as subobjects. Is that correct and would it also be correct that subobject == superclass object? Which one is preferred?

子类表示派生类和子类对象表示派生类'对象,对吗?

The subclass means the derived class and the subclass object means the derived class' object, right?

对我来说,混淆是子类对象!= subobject

The confusion for me is that subclass object != subobject.

如果其中任何一项是正确的,无论如何..

If any of this is right, anyway..

谢谢

推荐答案

C ++标准明确定义子对象是。也就是说,许多人在谈论C ++时并没有(精确地)使用标准语言。一个流行的例子是术语对象。从像Java这样的语言中,有些人倾向于仅在类的实例的意义上使用 object ,这不适用于 int 。在C ++标准中, int 是一个对象。

The C++ Standard has a clear definition of what a subobject is. That said, many people don't (precisely) use the language of the Standard when talking about C++. One popular example is the term object. From languages like Java, some people tend to use object only in the sense of instance of a class, which wouldn't apply to int. In the terms of the C++ Standard, an int is an object.

标准在[intro。 object] / 2:

What the Standard says in [intro.object]/2:


对象可以包含其他对象,称为子对象。子对象可以是成员子对象基类子对象或数组元素。不是任何其他对象的子对象的对象称为完整对象

Objects can contain other objects, called subobjects. A subobject can be a member subobject, a base class subobject, or an array element. An object that is not a subobject of any other object is called a complete object.

这是在(可能的)内存布局的上下文中理解。它没有完全指定,但很可能是这样的:

This is to be understand in the context of the (possible) memory layout. It isn't fully specified, but most likely looks like this:

class Foo { int i; };
class Bar : public Foo { int j; };

Bar 类型的对象可能看起来像这在内存中:

An object of type Bar could look like this in memory:


+-Bar-------------------+
|  +-Foo-----+          |
|  |  int i  |  int j;  |
|  +---------+          |
+-----------------------+

也就是说, Foo 的成员是 Bar 的成员,就像 Bar的直接成员一样。因此, Bar 的每个对象都包含一个 Foo 类型的对象。还要考虑

That is, the members of Foo are members of Bar like the direct members of Bar. Every object of Bar therefore "contains" an object of type Foo. Also consider

Bar b;
Bar* pBar = &b;
Foo* pFoo = &b;




+-Bar-------------------+
|  +-Foo-----+          |
|  |  int i  |  int j;  |
|  +---------+          |
+--^--------------------+
^  |pFoo
|pBar

允许 pFoo 指向 Foo 和 Foo 类型的子对象,需要一个整体和(内存方面)类型 Foo 在 Bar 类型的任何对象内。在编译时可能不知道哪一个是这种情况,但为 pFoo-> i = 5生成的代码; 必须适用于两者。

To allow pFoo to point to complete objects of type Foo and to subobjects of type Foo, there needs to be a whole and (memory-wise) independent object of type Foo inside any object of type Bar. It might not be known at compile-time which one is the case, but the code produced for pFoo->i = 5; has to work for both.

这一切都是在as-if规则下指定的,即它不一定是这样,但它必须以这种方式表现出来。此外,这不是实际的内存布局,但它是一种常见的实现。

This all is specified under the as-if rule, i.e. it doesn't have to be that way, but it has to observably behave that way. Also, it isn't required that this is the actual memory layout, but it is a common implementation.

在[ intro.object] / 4

In [intro.object]/4


如果完整对象,数据成员或数组元素属于类类型,则考虑其类型大多数派生类,以区别于任何基类子对象的类类型;最派生类类型或非类类型的对象称为大多数派生对象

If a complete object, a data member, or an array element is of class type, its type is considered the most derived class, to distinguish it from the class type of any base class subobject; an object of a most derived class type or of a non-class type is called a most derived object.

除了 supersede superset 之外,标准中没有使用 super 这个词。有基类派生类(以及〜对象)。

There's no use of the word super in the Standard other than supersede and superset. There's base class and derived class (as well as ~ object).

在C ++语言标准之外,术语超类用于引用基类,术语子类用于引用派生类。这是指OO概念和分类,就像 Species 是生物学中 Genus 的子类别。

Outside the C++ language Standard, the term superclass is used to refer to a base class, and the term subclass is used to refer to a derived class. This refers to the OO concepts and classification, much like a Species is a sub-category of a Genus in biology.

但是在这种分类中,没有内存布局,因此无需谈论子对象。有实例对象(在OO意义上),所以你可以谈论超类的对象子类的对象。这种混淆可能源于将子类的对象缩写为子对象,以及将OO语言与C ++标准语言混合。

But in this categorization, there's no memory layout, hence no need to talk about subobjects. There are instances or objects (in the OO sense) of classes, so you could talk about objects of a superclass and objects of a subclass. The confusion might stem from abbreviating this object of a subclass to subobject, and mixing language of OO with language from the C++ Standard.

这篇关于C ++消歧:子对象和子类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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