它是一个法律符号:Foo& foo = Bar; [英] Is it a legal notation: Foo &foo = Bar;

查看:142
本文介绍了它是一个法律符号:Foo& foo = Bar;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  struct Foo {}; 
struct Bar:Foo {};

Foo& foo = Bar; // without()



我不知道,这是一个法律符号吗?如果是合法的,你能给一些细节吗?有什么,为什么是合法的?或者,这样的符号的起源是什么?



编辑:我无法编译此代码。但我遇到了一个像这样的代码,想知道是否允许这样的符号(可能只是我的编译器不支持这种符号)。我有一些不确定性,因为下面的符号是合法的: Foo * pFoo = new Bar;

解决方案


  1. 不能为引用/对象分配类名。

  2. 您不能绑定对临时(rvalue)的引用,因此以下内容也是非法的:



    Foo& foo = Bar();


  3. 可以将一个临时(rvalue)绑定到一个const引用,所以以下是合法的:



    const Foo& foo = Bar code>


C ++标准特别允许3。


struct Foo {};
struct Bar : Foo {};

Foo &foo = Bar; // without ()

I wonder, Is it a legal notation? And if it is legal, could you give some details? Something like, Why it's legal? Or, What is the origin of such a notation?

EDIT: I cannot compile this code. But I met a code like that and wanted to know whether such a notation is allowed (probably just my compiler doesn't support this notation). I'm having some uncertainty since the following notation is quite legal: Foo *pFoo = new Bar;

解决方案

  1. You cannot assign an class Name to a reference/object. It is neither syntactically valid nor does it make any sense.
  2. You cannot bind a reference to a temporary(rvalue), So following is illegal too:

    Foo &foo = Bar();

  3. You can bind a temporary(rvalue) to an const reference, So following is legal:

    const Foo &foo = Bar();

The C++ standard specifically allows the 3.

这篇关于它是一个法律符号:Foo& foo = Bar;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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