您的位置:首页 > 其它

pure virtual function can has a function body

2017-04-19 18:48 239 查看
轉載自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`
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: