gpt4 book ai didi

c++ - 使用自动与显式指定私有(private)结构类型时的行为差异

转载 作者:太空狗 更新时间:2023-10-29 23:13:43 25 4
gpt4 key购买 nike

<分区>

考虑以下代码片段

class BST
{
struct Node
{
int data_;
Node * left_, * right;
} * root_ = nullptr;

public:

Node * getRoot()
{
return root_;
}
};

int main() {
BST bst;
auto root1 = bst.getRoot(); // Compiles fine
BST::Node * root2 = bst.getRoot(); // Gives error
}

当获取值并分配给使用 auto 推导其类型的变量时,代码可以正常编译。当明确指定类型时,它会失败。为什么会有这种行为差异?我预计两者都会失败。

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