#include<bits/stdc++.h>
using namespace std;
class TEST{
private:
struct Node{
int val = 114;
}root;
public:
inline Node Get(){
return root;
}
}a;
int main(){
auto nd = a.Get();
nd.val = 514;
cout<<nd.val<<endl;
return 0;
}
如下代码可以编译通过(在本地和洛谷 IDE 环境下皆可),为什么?
如果将 auto 换为 TEST::Node 会发生 'struct TEST::Node' is private within this context 的错误,这是当然,因为它被声明为了 private,但是使用 auto 就可以了?很奇怪诶。
通过制造错误 nd = 514 可以获得 no match for 'operator=' (operand types are 'TEST::Node' and 'int') 的信息,可以发现 nd 依然是 TEST::Node,为什么它可以被使用了?不是很懂,求助各位巨佬