本文介绍了派生类比基类有更多的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想就这个派生类的编程风格提出一个问题:
I would like to ask a question about programming style in this case of derived class:
class A
{
public:
virtual foo1()=0;
}
class B: public A
{
public:
virtual foo1();
virtual foo2();
}
class C: public A
{
public:
virtual foo1();
}
int main() {
B mB();
C mC();
mB.foo2() //OK!
mC.foo2() // obviously, it is not correct
return 0;}
$ b b
因此,一个派生类是否应该有比抽象基类少或相等的公共方法?
Therefore, should a derived class have less or equal public methods than the abstract base class?
如果派生类需要更多方法,应该是私有的吗?
If the derived classes require more methods, should these be private?
推荐答案
这个类结构没有问题。一个派生类有比父类更多的方法没有错误 - 这是很常见的。 mC.foo2();
行错了,这不是类的错误。
There is nothing wrong with this class structure. There is nothing wrong with a derived class having more methods than the parent class-- it's quite commonplace. The line mC.foo2();
is just wrong, and that is not the fault of the classes.
这篇关于派生类比基类有更多的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!