虚函数本质上是否应该有定义? [英] Should a virtual function essentially have a definition?

查看:139
本文介绍了虚函数本质上是否应该有定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为虚拟函数定义是否必不可少?



请考虑以下示例程序:

  #include< iostream> 

使用命名空间std;

班级基础
{
public:
void virtual virtualfunc();
};

派生的类:公共基础
{
public:
void virtualfunc()
{
cout<< 派生类中的vf;
}
};

int main()
{
衍生d;
返回0;
}

这会产生链接错误:


在函数 base :: base() ::中未定义对 vtable的引用


我在基类中没有虚拟函数的定义。为什么即使我没有显式调用虚函数也会发生此错误?



我发现的有趣之处在于,如果不实例化类派生的,链接错误不再存在。为什么是这样?实例化与上述链接错误有什么关系?

解决方案

ISO C ++标准规定,该类的所有虚方法


参考:


C ++ 03标准:10.3虚拟函数[class.virtual]


必须定义一个类中声明的虚拟函数,或在该类中声明为纯函数(10.4),或同时定义两者。但不需要诊断(3.2)。


因此,您应该使函数成为纯虚函数,或者为其提供定义。


如果使用的是gcc,如果不遵循此标准规范,可能会遇到一些奇怪的错误。 gcc常见问题解答 也将其记录在文档中:


ISO C ++标准规定,必须定义非纯虚拟类的所有虚拟方法,但对于违反此规则的情况不需要任何诊断 [class.virtual] / 8 。基于此假设,GCC将只在隐式定义的构造函数,赋值运算符,析构函数和转换单元中定义其第一个此类非内联方法的类的虚拟表中发出该类。


<因此,如果您未能定义此特定方法,则链接器可能会抱怨缺乏明显不相关的符号的定义。不幸的是,为了改善此错误消息,可能有必要更改链接器,而这并非总是如此。


解决方案是确保所有虚拟方法不纯被定义。请注意,即使已将析构函数声明为纯虚拟 [class.dtor] / 7



Is it essential to have a definition for a virtual function?

Consider this sample program below:

#include <iostream>

using namespace std;

class base
{
   public:
      void virtual virtualfunc();
};

class derived : public base
{
   public:
   void virtualfunc()
   {
      cout << "vf in derived class\n";
   }
};

int main()
{
   derived d;
   return 0;
}

This gives the link-error:

In function base::base():: undefined reference to vtable for base

I do not have the definition for the virtual function in base class. Why is this error occurring even though I have not explicitly invoked the virtual function?

The interesting thing which I find is that if I do not instantiate an object of class derived, the link error is no longer there. Why is this? What has instantiation got to do with the above link error?

解决方案

The ISO C++ Standard specifies that all virtual methods of a class that are not pure-virtual must be defined.

Reference:

C++03 Standard: 10.3 Virtual functions [class.virtual]

A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both; but no diagnostic is required (3.2).

So either you should make the function pure virtual or provide a definition for it.

If you are using gcc, You might get some weird errors if you fail to follow this standard specification. The gcc faq doccuments it as well:

The ISO C++ Standard specifies that all virtual methods of a class that are not pure-virtual must be defined, but does not require any diagnostic for violations of this rule [class.virtual]/8. Based on this assumption, GCC will only emit the implicitly defined constructors, the assignment operator, the destructor and the virtual table of a class in the translation unit that defines its first such non-inline method.

Therefore, if you fail to define this particular method, the linker may complain about the lack of definitions for apparently unrelated symbols. Unfortunately, in order to improve this error message, it might be necessary to change the linker, and this can't always be done.

The solution is to ensure that all virtual methods that are not pure are defined. Note that a destructor must be defined even if it is declared pure-virtual [class.dtor]/7.

这篇关于虚函数本质上是否应该有定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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