gpt4 book ai didi

c++ - 使用 `class` 关键字后跟未声明的标识符

转载 作者:行者123 更新时间:2023-12-02 10:18:08 24 4
gpt4 key购买 nike

以下 hpp 和 cpp 文件是我正在使用的一个大型程序的摘录。它将编译 g++ -std=c++17 -pedantic -Wall -Wextra .

// a.hpp

#include <memory>

class A
{
std::unique_ptr<class A_impl> my;
};
//a.cpp

#include "a.hpp"

int main()
{}

但我不明白有关唯一指针的行的语法。

问题:

  1. <class A_impl> 的语法是什么? ?这(将 class 放在未声明的标识符之前)叫什么?它是否在 A_impl 上进行“前向声明”或者是什么?我还没有说过任何关于标识符A_impl的事情。编译器怎么会同意呢?

  2. 如果这可能与任何“设计模式”有关,请帮我识别它。

请指出正确的方向。

最佳答案

Is it doing a "forward declaration" on A_impl or what?

正是如此。可能令人困惑的是它使用 elaborated type specifier在模板参数中这样做。 Differences to a "normal" forward declaration

I haven't said anything about the identifier type A_impl. How come the compiler is okay with that?

std::unique_ptr 可以用不完整的类型实例化 - 就像原始指针一样。

If this happens to be possibly related to any "design pattern", how can I identify it?

The PIMPL - "pointer to implementation" idiom.

关于c++ - 使用 `class` 关键字后跟未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59609097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com