在c++中使用私有继承时是否可能隐藏重载方法?
is it possible to hide an overloaded method while using private inheritence in c++
class Foo
{
public:
int fn()
{
return 1;
}
int fn(int i)
{
return i; //2nd fn()
}
};
class Bar:Foo
{
public :
Foo::fn;
};
int main(int argc, char** argv)
{
Bar b;
cout<<b.fn(2)<<endl;
}
不可能在具体类"Bar"中隐藏fn(int)
不,您不能从名称空间中"隐藏"名称。这与名称有关,因此包含了所有可能的同名重载。
同样,没有办法unusing
一个名称/命名空间。
这种现象导致了一个鲜为人知的ADL陷阱,库作者应该始终注意这个陷阱。
p。在示例代码中,当然你可以直接去掉/*using*/ Foo::fn;
line…但我猜这不是你的实际代码....
只是使基类private
, protected
(使用class
时默认为private
,到目前为止,ok),不使用,但在派生类中重写函数
class Bar: private Foo
{
public:
int fn() {return Foo::fn();}
};
这只会使int fn()
在Bar和not int fn(int)
中可见。当然,编译器会大声嚷嚷,fn不是虚函数,但你仍然覆盖它,但只要它只调用基类中的一个,它都是一样的。
相关文章:
- 在设计 SDK 时,我是否应该在 C++ 头文件中完全隐藏内部类?
- 是否可以在 c++ 中创建一个文件并为其提供属性以将其隐藏?(在 Ubuntu 上)
- c++ 内联友元函数是否会导致命名空间之间的名称隐藏?
- 这种获取模板参数包中最后一个元素的方法是否有隐藏的开销?
- 是否有任何简单的方法(黑客)揭露前向声明隐藏的标头
- QScopedPointer 是否隐藏了多重继承
- 是否可以隐藏像BOOST_LOG_TRIVIAL(lvl)这样的宏
- 是否可以从其他线程隐藏qt小部件窗口
- 静态函数是否隐藏具有相同名称的非静态函数
- 是否可以在OpenCV 3.1.0中指定SVM隐藏层中的节点数
- 是否可以隐藏模板类的实现
- 如果是模板类,是否可以隐藏库依赖项?
- 是否应隐藏所有数据成员
- 是否可以覆盖(隐藏)非虚拟方法,但仍然从子类显式调用它
- 使用Qt时是否可以隐藏或打包*.qss文件
- 如果内部类具有与外部类同名的函数,那么它是否在 C++ 和 Java 中隐藏了覆盖或重载
- 如何检测自动隐藏的任务栏是否可见
- 隐藏第三方命名空间是否危险
- 更改方法以添加隐藏的this指针是否会破坏二进制兼容性
- SFINAEd-out 函数是否隐藏了从基类显式导入的重载