pure virtual function can has a function body

轉載自http://stackoverflow.com/questions/5481941/c-pure-virtual-function-have-body

 

Your assumption that pure virtual function cannot be called is absolutely incorrect. When a function is declared pure virtual, it simply means that this function cannot get called dynamically, through a virtual dispatch mechanism. Yet, this very same function can easily be called statically, without virtual dispatch.

In C++ language static call to a virtual function is performed when a qualified name of the function is used in the call, i.e. when the function name specified in the call has the <class name>::<function name> form.

For example

struct S { virtual void foo() = 0; }; void S::foo() { // body for the pure virtual function `S::foo` } struct D : S { void foo() { S::foo(); // static call to `S::foo` from derived class } }; int main() { D d; d.S::foo(); // another static call to `S::foo` }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值