pure-specifier on function-definition

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

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?

回答1:

Ok, I've just learned something. A pure virtual function must be declared as follows:

 class Abstract  { public:    virtual void pure_virtual() = 0; }; 

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.

The details are here.



回答2:

C++ Standard, 10.4/2:

a function declaration cannot provide both a pure-specifier and a definition



回答3:

This syntax:

virtual void Process() = 0 {}; 

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.



回答4:

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?



回答5:

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] :

member-declaration:
attribute-specifier-seqoptdecl-specifier-seqoptmember-declarator-listopt;
function-definition

member-declarator-list:
member-declarator
member-declarator-list , member-declarator

member-declarator:
declarator virt-specifier-seqoptpure-specifieropt
declarator brace-or-equal-initializeropt
identifieroptattribute-specifier-seqopt: constant-expression

The grammar of function-definition does not include a pure-specifier, [dcl.fct.def.general]:

function-definition:
attribute-specifier-seqoptdecl-specifier-seqoptdeclarator virt-specifier-seqoptfunction-body



回答6:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!