通过引用传递的这两种C ++语法之间有什么区别? [英] Whats the difference between these two C++ syntax for passing by reference?

查看:49
本文介绍了通过引用传递的这两种C ++语法之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习现代C ++,有时我看到引用传递的值与空格之间的书写略有不同.

I'm learning modern C++ and sometimes I see values passed by reference written slightly differently with a space.

int &i

有区别吗?

int& x


#include <iostream>
using namespace std;

void swapnum(int &i, int &j) {
  int temp = i;
  i = j;
  j = temp;
}

void f(const int& x) {
  int& y = const_cast<int&>(x);
  ++y;
}

推荐答案

不,间距完全没有区别.在您的示例中与众不同的是 const 关键字(此处不是问题).

No, the spacing makes no difference at all. What makes the difference in your example is the const keyword (which isn't the question here).

间距实际上并不重要,除了您的同事可能会抱怨造型不好.这些形式等效于编译器:

Spacing doesn't really matter, except your colleagues may complain about bad styling. These forms are equivalent to the compiler:

int &i;
int& i;
int&i;
int
&
i;

编译器将在进一步处理之前将代码解析为词法元素",而空间仅用作分隔符.

The compiler will parse the code into "lexical elements" before further processing, and space only serves as separators.

这篇关于通过引用传递的这两种C ++语法之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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