加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 是现代g编译器解决的vtable错误的未定义引用?

发布时间:2020-12-16 09:56:37 所属栏目:百科 来源:网络整理
导读:根据 this虚函数必须定义,否则链接器抱怨报告错误“未定义引用vtable”,但为什么 ideone编译器没有为以下代码提供任何错误? #include iostreamusing namespace std;class Test{ public: Test() { cout"test() is calledn"; } virtual void test();};int ma
根据 this虚函数必须定义,否则链接器抱怨&报告错误“未定义引用vtable”,但为什么 ideone编译器没有为以下代码提供任何错误?

#include <iostream>
using namespace std;
class Test
{
    public:
    Test()
    {
        cout<<"test() is calledn";
    }
    virtual void test();
};
int main() {
    Test t;
    // your code goes here
    return 0;
}

解决方法

您没有正确阅读文档.相关段落的第一句话说:

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.

因此,预计您可能不会收到错误,尤其是因为您实际上并未调用test()(尽管构造函数的输出中存在谎言).

实际上,只有在下列情况下,您才有可能获得此诊断:

>你调用一个你没有定义的虚函数
>您实例化一个您没有定义其虚拟析构函数的对象

但不要搞错:你的程序有不确定的行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读