什么时候我应该在 C++ 中添加“类”这个词来创建指针?

Max*_*sky 3 c++ class

在什么情况下写可能无效 Type *p = nullptr;

而只有 class Type *p = nullptr;满足?

for*_*818 7

当类型Type被变量名称遮蔽时,您需要它:

class Type{};

int main() {
    int Type = 42;

    //Type * p = nullptr; // error: 'p' was not declared in this scope
    class Type* p = nullptr;
}
Run Code Online (Sandbox Code Playgroud)

但是,正如 Ayxan Haqverdili 所指出的那样,它::Type* p = nullptr;也能正常工作,并且还具有使用类型别名的额外好处。

  • @463035818 你可以做`::Type`。 (2认同)