我正在阅读这个Stack Overflow问题,并在该问题的代码中添加了一个构造函数,如下所示:
class Foo {
struct Bar {
int i;
Bar(int a = 5) :i(a) {};
};
public:
Bar Baz() { return Bar(); }
};
int main() {
Foo f;
// Foo::Bar b = f.Baz(); // error
auto b = f.Baz(); // ok
std::cout <<"b.i="<< b.i<<endl;
return 0;
}
代码输出
b.i=5
。在那个问题中,它得出结论私有名称不可访问,但类型是可访问的。所一般来说,类型和名称之间有什么区别?再举两个具体情况。
What's the difference between the following two declarations? Why can I get the output
b.i=5
fromauto b = f.Baz();
?Foo::Bar b = f.Baz(); auto b = f.Baz();
If I add
typedef Bar B;
in the public part ofFoo
, what is the difference between the following?Foo::Bar b = f.Baz(); Foo::B b = f.Baz();