简体   繁体   English

class 中的前向声明(不是嵌套类)

[英]Forward declaration within a class (not a nested class)

I came across this odd forward declaration (or something) where instead of having a normal:我遇到了这个奇怪的前向声明(或其他东西),而不是正常的:

class A;
class B{
    A* var;
}

You can do你可以做

class B{
   class A* var;
}

Now this might be an elaborated type specifier, but I'm not sure about that.现在这可能是一个详细的类型说明符,但我不确定。 I've tried it myself and I had no issues, even made my code a lot more cleaner, but I'm afraid it may cause scoping issues that I'm currently unaware of.我自己尝试过,没有遇到任何问题,甚至让我的代码更简洁,但我担心它可能会导致我目前不知道的范围问题。

Does anyone have an insight on this?有没有人对此有所了解? is this a valid forward deceleration?这是有效的向前减速吗?

example: https://youtu.be/EIptJ0YrYg0?t=412示例: https://youtu.be/EIptJ0YrYg0?t=412

Note that the doc says:请注意,文档说:

Note that a new class name may also be introduced by an elaborated type specifier which appears as part of another declaration, but only if name lookup can't find a previously declared class with the same name.请注意,新的 class 名称也可能由作为另一个声明的一部分出现的详细类型说明符引入,但前提是名称查找无法找到先前声明的具有相同名称的 class。

Thus, if class B is enclosed in a namespace and no previous forward declaration of A is performed, then it refers to n2::A that will have to be defined.因此,如果 class B包含在命名空间中并且之前没有执行A的前向声明,则它引用必须定义的n2::A

Now this might be an elaborated type specifier, but I'm not sure about that.现在这可能是一个详细的类型说明符,但我不确定。

It is.这是。 In fact, class A;事实上, class A; is a declaration to uses an elaborate class specifier.是使用精心设计的 class 说明符的声明。

but I'm afraid it may cause scoping issues that I'm currently unaware of但恐怕这可能会导致我目前不知道的范围界定问题

The only scoping related point you should be aware of is that if the elaborate type specifier (that is not a declaration by itself) is the first time class is referenced, the behavior is the same as if you introduced a forward declaration to the nearest namespace or block scope. So for instance您应该注意的唯一与作用域相关的点是,如果详细类型说明符(本身不是声明)是第一次引用 class,则其行为与将前向声明引入到最近的命名空间相同或阻止 scope。例如

namespace N {
  class foo {
     class bar {
       void func(class baz*);
     };
  };
};

... is the same as ... 是相同的

namespace N {
  class baz;
  class foo {
     class bar {
       void func(baz*);
     };
  };
};

Yes, it's a valid forward declaration.是的,这是一个有效的前向声明。 It's called an elaborate type specifier.它被称为精心设计的类型说明符。

To quote from elaborated type specifier from cppreference.引用来自 cppreference 的详细类型说明符

If the name lookup does not find a previously declared type name, the elaborated-type-specifier is introduced by class, struct, or union (ie not by enum), and class-name is an unqualified identifier, then the elaborated-type-specifier is a class declaration of the class-name.如果名称查找未找到先前声明的类型名称,则 elaborated-type-specifier 由 class、struct 或 union(即不是由 enum)引入,并且 class-name 是不合格的标识符,则 elaborated-type-说明符是类名的 class 声明。

It can also be used to refer to an already declared class even if it's name is hidden by non-type declaration.它也可以用来引用一个已经声明的 class,即使它的名字被非类型声明隐藏了。

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

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