C ++语法“ A :: B:A {};”的含义是什么意思 [英] What does C++ syntax “A::B:A {};” mean

查看:172
本文介绍了C ++语法“ A :: B:A {};”的含义是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++语法 struct A :: B:A {}; 是什么意思? C ++标准中描述的名称定义(或访问)在哪里?

What does C++ syntax struct A::B:A {}; mean? Where is this name definition (or access) described in the C++ standard?

#include <iostream>

struct B;

struct A {
    struct B;
};

struct A::B:A {
};

int main() {
    A::B::A::B b;
    std::cout<<"Sizeof A::B::A::B is " << sizeof(A::B::A::B)<<std::endl;
    return 0;
}


推荐答案

此定义

struct A {
    struct B;
};

定义结构体 A 并声明嵌套结构 B 1 B 的完全限定名称为 A :: B ,您可以说 B A 的命名空间内。然后这样:

Defines a struct A with a declaration of a nested struct B1. The fully qualified name of B is A::B, you could say B is inside the "namespace" of A. Then this:

struct A::B : A { // Note I added spaces
};

A :: B 的定义,而单个则指定它是源自 A

Is the definition of A::B, and the single : specifies that it is derived from A.

现在,有趣的部分是 A :: B :: A :: B 。让我们对其进行剖析:

Now, the interesting part is A::B::A::B. Let's dissect it:


  1. A :: B 命名嵌套结构。

  2. A :: B :: A 在<内访问注入的类名 A code> B 。注入是由于继承。

  3. A :: B :: A :: B 将嵌套结构命名为<$ c $再次在 A 中使用c> B 。

  1. A::B names the nested structure.
  2. A::B::A accesses the injected class name A inside B. The injection is due to the inheritance.
  3. A::B::A::B names the nested structure B in A again.

并且您可以继续进行无穷大,或者至少直到编译器达到其翻译限制 2 为止。

And you can continue ad-infinitum, or at least until your compiler meets its translation limit2.

有趣的智力练习,但避免实际代码中的瘟疫。

A fun intellectual exercise, but avoid like the plague in actual code.

[class.qual] / 1 解释查找的工作原理


如果<一个 qualified-id 的em> nested-name-specifier 提名一个类,在 nested-name-specifier 之后指定的
名称在
类别的范围内([class.member.lookup]),以下情况
除外。名称应代表该
类或其基类之一(Clause [class.derived])的一个或多个成员。

If the nested-name-specifier of a qualified-id nominates a class, the name specified after the nested-name-specifier is looked up in the scope of the class ([class.member.lookup]), except for the cases listed below. The name shall represent one or more members of that class or of one of its base classes (Clause [class.derived]).

上面的文本使我们可以命名基类,因为 [class] / 2

And the text above allows us to name the base class because [class]/2


类名也插入到类本身的作用域中;
,称为 injected-class-name 。为了进行
检查,将 injected-class-name 视为公开的
成员名称。

The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.

上面的内容清楚地表明,以 A :: 开头的完全限定名称允许您指定成员或基类。由于 A 没有基数,因此只能指定 A :: B (成员类型)。但是 A :: B 也可以提名一个班级。因此,我们也可以使用 A :: B :: 指定 的基数或成员,从而可以命名 A :: B :: A 。现在冲洗并重复。

The above clearly says that starting a fully qualified name with A:: allows you to specify a member or a base class. Since A has no bases, you can only specify A::B (a "member type"). But A::B also nominates a class. So we may specify a base or member of that as well with A::B::, which allows us to name A::B::A. Now rinse and repeat.


1 -请注意,这是一个完全是其他 B 。与全局结构B 完全无关。

2 -根据 [implimits] /2.36

1 - Note it's a completely other B. Not at all related to the global struct B.
2 - A recommended minimum of 256 according to [implimits]/2.36

这篇关于C ++语法“ A :: B:A {};”的含义是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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