函数定义上的纯说明符 [英] pure-specifier on function-definition
问题描述
在 GCC 上编译时,我收到 error: pure-specifier on function-definition,但在我使用 VS2005 编译相同的代码时却没有.
While compiling on GCC I get the error: pure-specifier on function-definition, but not when I compile the same code using VS2005.
class Dummy {
//error: pure-specifier on function-definition, VS2005 compiles
virtual void Process() = 0 {};
};
但是当这个纯虚函数的定义不是内联时,它是有效的:
But when the definition of this pure virtual function is not inline, it works:
class Dummy
{
virtual void Process() = 0;
};
void Dummy::Process()
{} //compiles on both GCC and VS2005
错误是什么意思?为什么我不能内联?如第二个代码示例所示,逃避编译问题是否合法?
What does the error means? Why cannot I do it inline? Is it legal to evade the compile issue as shown in the second code sample?
推荐答案
好的,我刚刚学到了一些东西.纯虚函数必须声明如下:
Ok, I've just learned something. A pure virtual function must be declared as follows:
class Abstract
{
public:
virtual void pure_virtual() = 0;
};
它可能有一个主体,尽管在声明点包含它是非法的.这意味着要拥有主体,必须在类之外定义纯虚函数.请注意,即使它有一个主体,该函数仍然必须被任何从 Abstract
派生的具体类覆盖.如果需要,他们可以选择显式调用 Abstract::pure_virtual()
.
It may have a body, although it is illegal to include it at the point of declaration. This means that to have a body the pure virtual function must be defined outside the class. Note that even if it has a body, the function must still be overridden by any concrete classes derived from Abstract
. They would just have an option to call Abstract::pure_virtual()
explicitly if they need to.
详情在这里.
这篇关于函数定义上的纯说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!