纯指定器对函数定义 [英] pure-specifier on function-definition

查看:398
本文介绍了纯指定器对函数定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GCC上编译时,我得到错误:函数定义上的纯说明符,但是当我使用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


$ b b

错误是什么意思?为什么我不能内联?是否合法逃避编译问题,如第二个代码示例所示?

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;
};

它可以有一个主体,但在声明时包含它是非法的。这意味着要有一个body,纯虚函数必须在类外定义。请注意,即使它有一个主体,该函数仍然必须被从 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆