为什么“覆盖”是在C ++ 11结束? [英] Why "override" is at the end in C++11?

查看:169
本文介绍了为什么“覆盖”是在C ++ 11结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看为什么在C ++ 11中,他们必须在方法结束时添加 override 关键字,而不是开始 virtual 。我没有看到在方法的声明中能够写 virtual override 的兴趣。 / p>

有没有技术上的原因,委员会没有选择简单地能够写 override 而不是<$


$ b

谢谢!

解决方案

添加关键字控制覆盖的提案( override / final ),paper N3151 ,让我们对这个选择有一些了解(强调我的):


将这样的虚拟控制关键字放在
结尾的声明中,以便它们不会与eg冲突。



[...]



不敏感,正常的关键字,因为这些字是保留的,所以关键字放在
的位置不太重要。我们可以把
放在声明的开头或结尾。



在讨论属性时,Francis Glassborow指出
的声明正在变得拥挤。如果我们把
虚拟控制关键字放在开头,我们可以像下面这样的例子




  struct B 
{
virtual volatile const unsigned long int f()
volatile const noexcept;
void f(int g);
};

struct D:B
{
virtual hides_name virtual_override final_overrider volatile const unsigned long int f()
volatile const noexcept;
};




将新关键字放到最后可以缓解情况
有些:




  struct B 
{
virtual volatile const unsigned long int f()
volatile const noexcept;
void f(int g);
};

struct D:B
{
virtual volatile const unsigned long int f()
hides_name virtual_override final_overrider volatile const noexcept;
};




有些人认为这些控制关键字
同虚拟的地方。如上所述,该地点已经拥挤。







注意:



C ++ 11标准在第2.11 / 2节[lex.name]中定义了上下文相关关键字:


表3中的标识符在出现在
特定上下文中时有特殊的含义。当在语法中引用时,这些标识符
被明确地使用,而不是使用标识符语法
生产。除非另有说明,关于给定标识符的
是否具有特殊含义的任何歧义都被解析为将
令牌解释为常规标识符。



表3:



最终覆写



I'm trying to see the reason why, in C++11, they had to add the override keyword at the end of the method instead of the beginning like virtual. I don't see the interest of being able to write both virtual and override in the declaration of a method.

Is there a technical reason why the committee didn't choose to simply be able to write override instead of virtual when it was needed?

Thanks!

解决方案

The proposal for the addition of the keywords controlling override (override/final) , paper N3151 , gives us some insight about this choice (emphasis mine) :

It is preferable to put such virtual control keywords at the end of the declaration so that they don't clash with eg. return types at the beginning of declarations.

[...]

For context-insensitive, normal keywords, it's less important where the keywords are placed because the words are reserved. We could put them at the beginning of declarations or at the end.

During the discussion of attributes, Francis Glassborow pointed out that the beginning of declarations is becoming crowded. If we put the virtual control keywords at the beginning, we can end up with examples like the one below:

struct B
{
   virtual volatile const unsigned long int f()
      volatile const noexcept;
   void f(int g);
};

struct D : B
{
   virtual hides_name virtual_override final_overrider volatile const unsigned long int f()
      volatile const noexcept;
};

Putting the new keywords at the end at least alleviates the situation somewhat:

struct B
{
   virtual volatile const unsigned long int f()
      volatile const noexcept;
   void f(int g);
};

struct D : B
{
   virtual volatile const unsigned long int f()
      hides_name virtual_override final_overrider volatile const noexcept;
};

There are people who think these control keywords should be in the same place with virtual. As mentioned, that place is already crowded.


Note:

The C++ 11 Standard defines context sensitive keywords in section § 2.11 / 2 [lex.name] :

The identifiers in Table 3 have a special meaning when appearing in a certain context. When referred to in the grammar, these identifiers are used explicitly rather than using the identifier grammar production. Unless otherwise specified, any ambiguity as to whether a given identifier has a special meaning is resolved to interpret the token as a regular identifier.

Table3:

final override

这篇关于为什么“覆盖”是在C ++ 11结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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