我正在尝试C++中的继承的简单示例。但我无法理解它。当我试图获得从类A
继承的类B
的受保护成员时,它表示A::baz
受保护。公共/受保护/私有继承问题
#include <iostream>
class A {
public:
int foo;
int bar;
protected:
int baz;
int buzz;
private:
int privfoo;
int privbar;
};
class B : protected A {}; // protected members go to class B, right?
int main() {
B b;
b.baz; // here is the error [A::baz is protected]
}
我似乎无法找到我做错了什么。我试过将class B : protected A
更改为: public A
,但它仍然不起作用。
它很少使用公共继承以外的任何东西。 – 2012-08-16 18:04:00
http://www.parashift.com/c++-faq/access-rules-with-priv-inherit.html – Derek 2012-08-16 18:05:45