函数定义上的纯说明符
在 GCC 上编译时,我收到错误:函数定义上的纯说明符,但当我使用 VS2005 编译相同的代码时却没有。
class Dummy {
//error: pure-specifier on function-definition, VS2005 compiles
virtual void Process() = 0 {};
};
但是,当这个纯虚函数的定义不是内联时,它会起作用:
class Dummy
{
virtual void Process() = 0;
};
void Dummy::Process()
{} //compiles on both GCC and 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,我刚刚学到了一些东西。纯虚函数必须声明如下:
它可以有一个函数体,尽管在声明点包含它是非法的。这意味着要拥有主体,纯虚函数必须在类外部定义。请注意,即使它有主体,该函数仍然必须被从
Abstract
派生的任何具体类覆盖。如果需要,他们可以选择显式调用Abstract::pure_virtual()
。详细信息位于此处。
Ok, I've just learned something. A pure virtual function must be declared as follows:
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 callAbstract::pure_virtual()
explicitly if they need to.The details are here.
C++ 标准,10.4/2:
C++ Standard, 10.4/2:
此语法:
不是合法的 C++,但受 VC++ 支持。我一直不清楚标准为何不允许这样做。你的第二个例子是合法的。
This syntax:
is not legal C++, but is supported by VC++. Exactly why the Standard disallows this has never been obvious to me. Your second example is legal.
根据定义,C++ 中的纯虚函数在声明中没有定义。
您的第二个代码块并没有避免编译器问题。它按照预期的方式实现了纯虚函数。
要问的问题是,如果您打算有一个默认实现,为什么需要将其声明为纯虚拟?
Pure virtual functions in C++ by definition have no definition in the declaration.
You second code block is not avoiding the compiler issue. It is implementing a pure virtual function the way it was intended.
The question to ask is, why do you need to declare it pure virtual if you intend to have a default implementation?
这在语法上是不允许的 - 可以包含纯说明符的声明符,即成员声明符,仅出现在不是定义的声明中。 [类.mem]
:
函数定义的语法不包含纯说明符, [dcl.fct.def.一般]:
This is gramatically disallowed - the declarator that can include pure-specifiers, i.e. the member-declarator, only appears in declarations that aren't definitions. [class.mem]
:
The grammar of function-definition does not include a pure-specifier, [dcl.fct.def.general]:
您当然可以为纯虚函数提供主体。该函数将由该抽象类 vtable 指向。否则,相同的槽将指向特定于编译器的陷阱函数,例如 GCC 的 __cxa_pure_virtual。当然,标准中对此没有任何内容。
You can certainly provide a body for pure virtual function. That function will be pointed to by that abstract class vtable. Otherwise the same slot will point to compiler-specific trap function like
__cxa_pure_virtual
for GCC. There's of course nothing about this in the standard.