这是一个面试问题.我不是C++专家,所以我需要一些帮助来找到这个问题的答案(我首先想要理解这个问题......这是一个有效的问题吗?)
题:
假设我有一个派生自A类的B类,我想重用一些但不是A的所有方法.我如何有选择地限制对超类的方法的访问?
谢谢!
Ale*_* C. 10
我认为
A
A
应从B
对象访问哪些方法.该using
指令解决了您的问题.例:
class A
{
public: // or protected for that matter
void foo();
void bar();
};
class B : private A // or protected, depending on whether
// you want subclasses of B to expose
// some methods from A themselves
{
public:
using A::foo;
};
Run Code Online (Sandbox Code Playgroud)
foo
从课堂上可以使用B
,但不是bar
.但作为一个警告,请注意using A::foo
将暴露所有重载foo
.