[英]How do I delete a pure virtual function inherited from base class?
Let's say I have a base class called Human
.假设我有一个名为
Human
的 base class。 Baby
and Adult
are sub-classes of Human
. Baby
和Adult
是Human
的子类。 Human
has a function run
as a pure virtual function and I don't want Baby
to inherit run
but if I don't override run
, Baby
would become an abstract class. Human
有一个 function 作为纯虚拟 function run
,我不希望Baby
继承run
但如果我不覆盖run
, Baby
将变成一个抽象的 class。
class Human{
public:
// Some human attributes.
virtual void run()=0;
};
class Adult: public Human{
public:
void run(){
// Adult running function
}
};
class Baby: public Human{
public:
void run()=delete; //error
// If I don't override `run` then Baby is marked as an abstract class
void crawl(){
// Baby crawling function.
}
};
Error if I mark run
with =delete
如果我用
=delete
标记run
时出错
prog.cpp:19:10: error: deleted function ‘virtual void Baby::run()’
void run()=delete;
^~~
prog.cpp:6:22: error: overriding non-deleted function ‘virtual void Human::run()’
virtual void run()=0;
^~~
Might be a bad example but what I want is to inherit everything except for run
in Baby
.可能是一个不好的例子,但我想要的是继承除
run
in Baby
之外的所有内容。 Is it possible?可能吗?
Disclaimer: I'm not implementing it anywhere.免责声明:我不会在任何地方实施它。 Just out of curiosity.
只是出于好奇。
This is not possible.这不可能。 Consider the following code:
考虑以下代码:
Human *CreateAPerson() {
return new Baby();
}
int main() {
Human *human = CreateAPerson();
human->run();
}
The compiler would have no way to know that human->run()
is illegal.编译器无法知道
human->run()
是非法的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.