本文介绍了纯虚函数在C ++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 98中,空指针由文字 0 (或实际上任何值为零的常量表达式)表示。在C ++ 11中,我们更喜欢 nullptr 。但这不适用于纯虚函数:

  struct X 
{
virtual void foo )= nullptr;
};

为什么不起作用?这不会完全有道理吗?这只是一个监督吗?

因为语法 0 ,不会修改 或其他非终端匹配 nullptr



所有时间只有 0 工作。即使 0L 也会因为语法不符而导致错误。



编辑



Clang允许 = 0x0 = 0b0 = 00 (31.12.2013)。这是不正确的,应该在编译器中固定,当然。


In C++98, the null pointer was represented by the literal 0 (or in fact any constant expression whose value was zero). In C++11, we prefer nullptr instead. But this doesn't work for pure virtual functions:

struct X
{
    virtual void foo() = nullptr;
};

Why does this not work? Would it not make total sense? Is this simply an oversight? Will it be fixed?

解决方案

Because the syntax says 0, not expression or some other non-terminal matching nullptr.

For all the time only 0 has worked. Even 0L would be ill-formed because it does not match the syntax.

Edit

Clang allows = 0x0, = 0b0 and = 00 (31.12.2013). That is incorrect and should be fixed in the compiler, of course.

这篇关于纯虚函数在C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 06:36
查看更多