<分区>
考虑以下代码片段
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 推导其类型的变量时,代码可以正常编译。当明确指定类型时,它会失败。为什么会有这种行为差异?我预计两者都会失败。