您可以在C ++的同一行上声明多个引用变量吗? [英] Can you declare multiple reference variables on the same line in C++?

查看:42
本文介绍了您可以在C ++的同一行上声明多个引用变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即这些法律声明是

int x = 1, y = z = 2;
int& a = x, b = c = y;

,预期结果为 a x 的别名,而 b c y ?

with the intended result that a is an alias for x, whileb and c are aliases for y?

我之所以问是因为我在此处

I ask simply because I read here

将变量声明为引用而不是常规变量只需在< strong"类型

Declaring a variable as a reference rather than a normal variable simply entails appending an ampersand to the type name

这使我犹豫是否可以通过在变量名称之前放置& 来创建多个参考变量是否合法.

which lead me to hesitate whether it was legal to create multiple reference variables by placing & before the variable name.

推荐答案

int x = 1, y = z = 2;--incorrect
int& a = x, b = c = y;--incorrect

语句应如下所示:

int x = 1, y =2,z = 2;
int&q=x,&b=y,&c=y;

c ++中的所有赋值和初始化语句应为以下类型:

All assignment and initialization statements in c++ should be of the following type:

lvalue=rvalue;

此处,左值必须始终是一个为其分配了临时值/另一个变量的变量.rvalue可以是另一个变量,也可以是计算为(4 + 5)之类的临时变量的表达式.

here lvalue must always be a variable to which a temporary value/another variable is assigned. rvalue can be another variable or an expression that evaluates to a temporary variable like (4+5).

这篇关于您可以在C ++的同一行上声明多个引用变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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