一种在c ++中隐藏特定函数的方法 [英] a way in c++ to hide a specific function
本文介绍了一种在c ++中隐藏特定函数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个继承 struct A:public B
,我想从B隐藏个别函数,这是可能的吗?
我知道在A声明中使用
>
解决方案
这里有一个问题:这将是对Liskov替换原则的直接违反,即 A
不会再作为
B
。
您希望重用 B
实现,解决方案就是这样:
class A
{
public:
void foo(){return b.foo(); }
void bar(){return b.bar(); }
// ...
private:
B b;
};
不要滥用继承,改用组合 b $ b
i have an inheritance struct A : public B
, i want to hide individual functions from B, is this possible?
i know the opposite is possible using using BMethod
in the A declaration.
cheers
解决方案
There is an issue here: this would be a direct violation of the Liskov Substitution Principle, namely A
would not act as a B
any longer.
If you wish to reuse B
implementation, the solution is simply to do so:
class A
{
public:
void foo() { return b.foo(); }
void bar() { return b.bar(); }
// ...
private:
B b;
};
Don't abuse inheritance, use composition instead
这篇关于一种在c ++中隐藏特定函数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文