放置“&"在类型之后 [英] putting a '&' after the type

查看:106
本文介绍了放置“&"在类型之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手.我只是在大学课程中从C转到C ++,遇到了一些以前从未在C中见过的东西.有时在类型之后,无论是在函数声明中还是在传递参数时,&都会紧跟在类型.例如,我们在我们的一个项目中使用了一个名为Customer的结构,并且某些函数通过Customer&传递.为什么&"号后面是类型,而不是前面?谢谢!

I am fairly new to programming. I am just moving on to C++ from C in my college courses, and I encountered something that I haven't seen before in C. Sometimes after the type, either in a function declaration or passing a parameter, a & immediately follows the type. For example, we use a struct called Customer in one of our projects, and some of the functions pass Customer&. Why is the ampersand after the type, as opposed to in front? Thanks!

推荐答案

C ++中的引用仅允许使用更简洁的方式执行以下代码:

References in C++ simply allow for a cleaner way to execute the following code:

int x = 16;
int* y = &x;
cout << *y;

哪个可以写成

int x = 16;
int& y = x;
cout << y;

在定义函数时,引用允许函数更改参数的值,而不会导致函数的使用者在所有字符之前加上&"号.例如

When defining functions, a reference allows a function to change the value of parameters without causing the user of the function to put an ampersand before everything. E.g.

void func( int& a )
{
    a = 5;
}

void main()
{
    int A = 10;
    func( A );
    cout << A; // Will output '5'
}

请谨慎对待这种类型的突变,因为程序员使用此类函数而不检查实现可能不会意识到函数正在更改参数的值,除非意图很明显. init_server(my_server)是一个显而易见的例子,但是to_json(my_struct)显然是一个不应该使用引用以任何方式更改结构的例子.

Be careful with this type of mutation, as a programmer using functions like this without checking the implementation might not realize that the function is changing the value of the parameters unless the intent is obvious. init_server(my_server) would be an example of a case where it's obvious, but to_json(my_struct) would clearly be an example where you should not be using a reference to change the struct in any way.

但是,引用最重要的用途之一就是像

But, one of the most important uses of references, would be function like

int sum_vector( const vector<int>& a ) {
   int sum = 0;
   for( int i = 0; i < a.size(); i++ ) {
      sum += a[i];
   }
   return sum;
}

如果您尝试使sum_vector接受一个向量,并且传递了一个带有1亿个条目的向量,那么它将必须将它们全部复制,直到永远.您可以使用一个指针,但是该函数的内部部分必须不断取消引用,并且必须使用sum_vector(&myvec)进行调用,这比sum_vector(myvec)更令人讨厌.这样,通过使用const引用,可以防止将整个向量非常低效地复制到函数主体中,同时保持语法整洁.使用const可以使您确信自己不会更改给定的向量.并且,它还向您的功能的用户保证您不会对其进行更改.同样,void to_json(const some_struct&)是更好的函数定义,因为它可以确保您不会更改用户的数据.

If you tried to make sum_vector take in a vector, and you passed in a vector with 100 million entries, then it would have to copy them all over, taking forever. You could take in a pointer, but then the internal parts of the function would have to constantly dereference, and it must called with sum_vector(&myvec), which is more annoying than sum_vector(myvec). In this way, using a const reference, you can prevent the highly inefficient copying of the whole vector into the function body, while keeping syntax neat. Using const lets you reassure yourself that you're not going to change the vector that you were given. And, it also assures the user of your function that you won't change it. Similarly, void to_json(const some_struct&) would be a better function definition as it ensures you won't change the user's data.

这篇关于放置“&amp;"在类型之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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