AnT*_*AnT 89
您无法调用纯虚函数的假设绝对不正确.当一个函数被声明为纯虚拟时,它只是意味着无法通过虚拟调度机制动态调用此函数.然而,这非常相同的功能,可以很容易地被称为静态,非虚拟,直接(不包括虚拟调度).
在C++语言中,当在调用中使用函数的限定名称时,即在调用中指定的函数名具有<class name>::<function name>表单时,将执行对虚函数的非虚拟调用.
例如
struct S
{
virtual void foo() = 0;
};
void S::foo()
{
// body for pure virtual function `S::foo`
}
struct D : S
{
void foo()
{
S::foo();
// Non-virtual call to `S::foo` from derived class
this->S::foo();
// Alternative syntax to perform the same non-virtual call
// to `S::foo` from derived class
}
};
int main()
{
D d;
d.S::foo();
// Another non-virtual call to `S::foo`
}
Run Code Online (Sandbox Code Playgroud)
对于大多数纯虚函数,你是对的.但是,对于纯虚拟析构函数,定义相应的析构函数实现非常重要:
| 归档时间: |
|
| 查看次数: |
30061 次 |
| 最近记录: |