函数定义上的纯说明符

发布于 2024-09-03 09:22:51 字数 438 浏览 8 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冰火雁神 2024-09-10 09:22:51

好吧,我刚刚学到了一些东西。纯虚函数必须声明如下:


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

它可以有一个函数体,尽管在声明点包含它是非法的。这意味着要拥有主体,纯虚函数必须在类外部定义。请注意,即使它有主体,该函数仍然必须被从 Abstract 派生的任何具体类覆盖。如果需要,他们可以选择显式调用 Abstract::pure_virtual()

详细信息位于此处

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.

爱的那么颓废 2024-09-10 09:22:51

C++ 标准,10.4/2:

函数声明不能​​同时提供纯说明符和定义

C++ Standard, 10.4/2:

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

美人骨 2024-09-10 09:22:51

此语法:

virtual void Process() = 0 {};

不是合法的 C++,但受 VC++ 支持。我一直不清楚标准为何不允许这样做。你的第二个例子是合法的。

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.

许久 2024-09-10 09:22:51

根据定义,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?

荆棘i 2024-09-10 09:22:51

这在语法上是不允许的 - 可以包含纯说明符的声明符,即成员声明符,仅出现在不是定义的声明中。 [类.mem]

成员声明
        属性说明符序列opt
decl-specifier-seqopt 成员声明符列表opt ;
         函数定义

        [...]

成员声明符列表
        成员声明符
         成员声明符列表
, 成员声明符

成员声明符
       声明符
virt-specifier-seq
opt pure-specifieropt

       声明符大括号或等于初始化器opt
        标识符opt 属性说明符序列opt : 常量表达式

函数定义的语法不包含纯说明符, [dcl.fct.def.一般]:

函数定义
     属性说明符序列opt
decl-specifier-seqopt 声明符 virt-specifier-seqopt 函数体 >

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-seqopt
decl-specifier-seqopt member-declarator-listopt ;
         function-definition

         [...]

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

member-declarator:
         declarator
virt-specifier-seq
opt pure-specifieropt

         declarator brace-or-equal-initializeropt
         identifieropt attribute-specifier-seqopt : constant-expression

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

function-definition:
     attribute-specifier-seqopt
decl-specifier-seqopt declarator virt-specifier-seqopt function-body

瑕疵 2024-09-10 09:22:51

您当然可以为纯虚函数提供主体。该函数将由该抽象类 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文