简体   繁体   English

C ++ Primer练习2.27 [第5版]。

[英]C++ Primer exercise 2.27 [5th ed.]

I am doing exercise 2.27 from C++ primer 5th edition and I am confused in this question: 我正在从C ++入门手册第5版进行练习2.27,对此问题感到困惑:

Exercise: Which of the following initializations are legal? 练习:以下哪些初始化是合法的? Explain why. 解释为什么。

(c) const int i = -1, &r = 0; (c)const int i = -1,&r = 0;

I came to conclusion that r is illegal because this will be same as below: 我得出的结论是r非法,因为这将与以下内容相同:

const int i = -1;
int &r = 0;

But this github repo suggest that (c) is same as below: 但是这个github回购建议(c)如下:

const int i = -1;
const int &r = 0;

So, it contradicts to my answer, please provide me the correct answer. 因此,它与我的答案相矛盾,请提供正确的答案。

PS: I am begineer in C++ language. PS:我是C ++语言的初学者。

The type specifier ( int ) with the qualifier ( const ) belong to all declarators in the declaration 类型限定符( int )和限定符( const )属于声明中的所有声明符

const int i = -1, &r = 0;

Thus declarators i and &r have the type specifier and qualifier const int . 因此,声明符i&r具有类型说明符和限定符const int Moreover you may not write for example 而且你可能不写例如

int &r = 0;

because a temporary object (in this case expression 0) may not be bound to a non-constant reference. 因为临时对象(在这种情况下为表达式0)可能不会绑定到非恒定引用。

However you could write 但是你可以写

int &&r = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM