c++ - 虚拟继承的优势

标签 c++ inheritance c++11 virtual-inheritance

C++98/C++03 标准和 C++0x future 标准对 dominance in virtual inheritance 的确切规则是什么? ?

我不只是要求特定的段落,尽管我也在要求(我猜是在第 10 节的某个地方)。

我还询问了标准语的后果,标准语解释得很清楚。

最佳答案

我认为这是您正在寻找的语言。在 C++03 ISO 规范的 §10.2/2 中,我们有以下内容:

The following steps define the result of name lookup in a class scope, C. First, every declaration for the name in the class and in each of its base class sub-objects is considered. A member name f in one sub-object B hides a member name f in a sub-object A if A is a base class sub-object of B. Any declarations that are so hidden are eliminated from consideration. Each of these declarations that was introduced by a using-declaration is considered to be from each sub-object of C that is of the type containing the declaration designated by the using-declaration. If the resulting set of declarations are not all from sub-objects of the same type, or the set has a nonstatic member and includes members from distinct sub-objects, there is an ambiguity and the program is ill-formed. Otherwise that set is the result of the lookup.



在较高级别,这意味着当您尝试查找名称时,它会在所有基类和类本身中查找该名称的声明。然后逐个类进行,如果其中一个基对象具有该名称,它将隐藏在该对象的任何基类中引入的所有名称。

这里的一个重要细节是这一行:

Any declarations that are so hidden are eliminated from consideration.



重要的是,这表示如果隐藏了某些东西 通过任何事情 ,它被认为是隐藏的并被移除。因此,例如,如果我这样做:
                            class D {
                            public:
                                void f();
                            }

   class B: virtual public D {        class C: virtual public D {
   public:                            public:
        void f();                         /* empty */
   };                                 };

                       class A: public B, public C {
                       public:
                           void doSomething() {
                                f(); // <--- This line
                           }
                       };

在指示的行上,调用 f()解决如下。首先,我们添加 B::fD::f到可以考虑的名称集。 D::f不隐藏任何东西,因为 D没有基类。然而,B::f隐藏 D::f ,所以即使 D::f可以从 A 联系到没有看到B::f ,它被认为是隐藏的并从可以命名为 f 的对象集中删除。 .由于只有 B::f剩下的,这就是所谓的。 ISO 规范提到 (§10.2/7)

When virtual base classes are used, a hidden declaration can be reached along a path through the sub-object lattice that does not pass through the hiding declaration. This is not an ambiguity. [...]



我认为这是因为上述规则。

在 C++11 中(根据草案规范 N3242),规则比以前更加明确,并给出了实际算法来计算名称的含义。这是一步一步的语言。

我们从 §10.2/3 开始:

The lookup set for f in C, called S(f, C), consists of two component sets: the declaration set, a set of members named f; and the subobject set, a set of subobjects where declarations of these members (possibly including using-declarations) were found. In the declaration set, using-declarations are replaced by the members they designate, and type declarations (including injected-class-names) are replaced by the types they designate. S(f, C) is calculated as follows:



在这种情况下,C指查找发生的范围。换句话说,集合 S(f, C)意思是“当我尝试在类范围 f 中查找 C 时可见的声明是什么?”为了回答这个问题,规范定义了一个算法来确定这一点。第一步如下:(§10.2/4)

If C contains a declaration of the name f, the declaration set contains every declaration of f declared in C that satisfies the requirements of the language construct in which the lookup occurs. [...] If the resulting declaration set is not empty, the subobject set contains C itself, and calculation is complete.



换句话说,如果类本身有一个叫做 f 的东西在其中声明,那么声明集就是名为 f 的东西的集合。在该类中定义(或使用 using 声明导入)。但是,如果我们找不到名为 f 的任何内容,或者如果所有内容都命名为 f是错误的类型(例如,当我们想要一个类型时的函数声明),然后我们继续下一步:(第 10.2/5 节)

Otherwise (i.e., C does not contain a declaration of f or the resulting declaration set is empty), S(f, C) is initially empty. If C has base classes, calculate the lookup set for f in each direct base class subobject Bi, and merge each such lookup set S(f, Bi) in turn into S(f, C).



换句话说,我们将查看基类,计算名称在这些基类中可以引用的内容,然后将所有内容合并在一起。进行合并的实际方式在下一步中指定。这真的很棘手(它分为三个部分),所以这里是逐一的。这是原始措辞:(第 10.2/6 节)

The following steps define the result of merging lookup set S(f, Bi) into the intermediate S(f, C):

  • If each of the subobject members of S(f, Bi) is a base class subobject of at least one of the subobject members of S(f, C), or if S(f, Bi) is empty, S(f, C) is unchanged and the merge is complete. Conversely, if each of the subobject members of S(f, C) is a base class subobject of at least one of the subobject members of S(f, Bi), or if S(f, C) is empty, the new S(f, C) is a copy of S(f, Bi ).

  • Otherwise, if the declaration sets of S(f, Bi) and S(f, C) differ, the merge is ambiguous: the new S(f, C) is a lookup set with an invalid declaration set and the union of the subobject sets. In subsequent merges, an invalid declaration set is considered different from any other.

  • Otherwise, the new S(f, C) is a lookup set with the shared set of declarations and the union of the subobject sets.



好的,让我们一次把它拆开。这里的第一条规则有两个部分。第一部分说,如果你试图将一个空的声明集合并到整个集合中,你根本什么都不做。那讲得通。它还说,如果你试图合并一些东西,它是迄今为止已经合并的所有东西的基类,那么你根本什么都不做。这很重要,因为这意味着如果你隐藏了一些东西,你不想通过重新合并它而意外地重新引入它。

第一条规则的第二部分说,如果你要合并的东西派生自到目前为止已经合并的所有东西,你用你为派生类型计算的数据替换你到目前为止计算的集合.这实质上是说,如果您将许多看起来没有联系的类合并在一起,然后合并到一个统一所有这些类的类中,则丢弃旧数据并仅使用您已经计算过的派生类型的数据.

现在让我们来看看第二条规则。这花了我一段时间才理解,所以我可能有这个错误,但我认为它是说如果你在两个不同的基类中进行查找并返回不同的东西,那么名称是不明确的,你应该报告一些东西是如果您此时尝试查找名称,则错误。

最后一条规则是,如果我们不在这两种特殊情况中的任何一种,都没有错,您应该将它们组合起来。

呼……太难了!让我们看看当我们追踪上面的菱形继承(钻石问题)时会发生什么。我们要查找名称 fA 开始.自 A没有定义 f ,我们计算查找值 fB 开始和 fC 开始.让我们看看发生了什么。当计算什么的值时 f表示在 B ,我们看到 B::f已定义,因此我们停止查找。查找的值(value)fB是集合 ( B::f , B } 。要查找 fC 中的含义,我们查看 C 并看到它没有定义 f ,因此我们再次递归查找来自 D 的值。在 D 中查找会产生 { D::f , D },当我们将所有内容合并在一起时,我们发现规则 1 的后半部分适用(因为在子对象集是 D 的基础),因此 C 的最终值由 { D::f , D } 给出。

最后,我们需要将 B 的值合并在一起。和 C .这会尝试合并 { D::f , D } 和 { B::f , B }.这就是它变得有趣的地方。假设我们按此顺序合并。合并{ D::f , D } 并且空集产生 { D::f , D }.当我们现在合并 { B::f , B },那么因为DB 的基础,在规则一的后半部分,我们覆盖了旧的集合并以 { B::f 结束, B }.因此,查找 ff的版本在 B .

另一方面,如果我们以相反的顺序合并,我们从 { B::f 开始, B } 并尝试合并 { D::f , D }.但由于 DB 的基础,我们就忽略它,留下{ B::f , B }.我们得出了相同的结果。很酷吧?我很惊讶这效果这么好!

所以你有它 - 旧规则真的(ish)直截了当,而新规则不可能复杂,但无论如何还是设法解决了。

希望这可以帮助!

关于c++ - 虚拟继承的优势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7210860/

相关文章:

kotlin - 在 Kotlin 中重写子类中的变量

c++ - boost asio transfer_exactly 读取 0 字节

c++ - 插入适用于 set 但不适用于 unordered_set

pointers - C++ 11-一直使用nullptr吗?

c++ - 编译器是否调整 int 大小?

C++ winsock 错误

c++ - 位操作(清除 n 位)

java - 在 Java 中作弊单继承?

java - JavaFX 应用程序的子类化

c++ - 在主函数 c++ 中使用类时出现两个错误