相关疑难解决方法(0)

为什么有注入的类名?

最近,我看到了一个奇怪的C++特性:注入类名.

class X { };
X x1;
class X::X x2; // class X::X is equal to X
class X::X::X x3; // ...and so on...
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚为什么这个功能是必要的.有没有需要此功能的练习?

我听说旧C++中不存在这个功能.然后,什么时候介绍?C++ 03?C++ 11?

c++

142
推荐指数
1
解决办法
6321
查看次数

当 Injected-Class-Name 发生时会发生什么?(C++)

根据https://en.cppreference.com/w/cpp/language/injected-class-name

在类作用域中,当前类的名称被视为公共成员名称;这称为注入类名。名称的声明点紧跟在类定义的左大括号之后。

int X;
struct X {
    void f() {
        X* p; // OK. X refers to the injected-class-name
        ::X* q; // Error: name lookup finds a variable name, which hides the struct name
    }
};
Run Code Online (Sandbox Code Playgroud)

那么代码中到底发生了什么?是X* p变成了X::X* p

c++ scope class name-lookup injected-class-name

4
推荐指数
1
解决办法
96
查看次数

标签 统计

c++ ×2

class ×1

injected-class-name ×1

name-lookup ×1

scope ×1