C++ 继承 - 无法访问的基础?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4847100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ inheritance - inaccessible base?
提问by bandai
I seem to be unable to use a base class as a function parameter, have I messed up my inheritance?
我似乎无法使用基类作为函数参数,我是否搞砸了我的继承?
I have the following in my main:
我的主要内容如下:
int some_ftn(Foo *f) { /* some code */ };
Bar b;
some_ftn(&b);
And the class Bar inheriting from Foo in such a way:
类 Bar 以这种方式从 Foo 继承:
class Bar : Foo
{
public:
Bar();
//snip
private:
//snip
};
Should this not work? I don't seem to be able to make that call in my main function
这不应该工作吗?我似乎无法在我的主函数中进行调用
回答by Andrew Noyes
You have to do this:
你必须这样做:
class Bar : public Foo
{
// ...
}
The default inheritance type of a classin C++ is private, so any publicand protectedmembers from the base class are limited to private. structinheritance on the other hand is publicby default.
classC++中 a 的默认继承类型是private,因此基类中的任何public和protected成员都限于private。struct另一方面,继承是public默认的。
回答by Jim Buck
By default, inheritance is private. You have to explicitly use public:
默认情况下,继承是私有的。您必须明确使用public:
class Bar : public Foo
class Bar : public Foo

