各种符号(*,&等)与参数的区别是什么? [英] What are the distinctions between the various symbols (*,&, etc) combined with parameters?

查看:253
本文介绍了各种符号(*,&等)与参数的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

c ++ * vs&在函数声明中

我知道这可能看起来像一个令人难以置信的基本问题很多人,但我真的有一个不可能的时间找到一个良好,彻底的解释,尽管我最好的谷歌。我确定答案在那里,所以我的搜索字词必须是可怕的。

I know that this probably seems like an incredibly elementary question to many of you, but I have genuinely had an impossible time finding a good, thorough explanation, despite all my best Googling. I'm certain that the answer is out there, and so my search terms must be terrible.

在C ++中,各种符号及其组合用于标记参数(以及这些参数的参数)。

In C++, a variety of symbols and combinations thereof are used to mark parameters (as well as arguments to those parameters). What, exactly, are their meanings?

例如: void func(int * var)之间的区别是什么?和 void func(int ** var) int& var

Ex: What is the difference between void func(int *var) and void func(int **var)? What about int &var?

同样的问题代表返回类型以及参数。 int&与 int * func(int var)相比,func(int var)在参数中, y = func(* x)如何与 y = func(& x)不同?

The same question stands for return types, as well as arguments. What does int& func(int var) mean, as compared to int* func(int var)? And in arguments, how does y = func(*x) differ from y = func(&x)?

如果只有你能指出正确的方向,我非常乐意阅读关于这个主题的大量文章。此外,我非常熟悉一般的编程概念:OO,泛型/模板等,只是不是在C / C ++中使用的符号。

I am more than happy to read enormous volumes on the subject if only you could point me in the right direction. Also, I'm extremely familiar with general programming concepts: OO, generics/templates, etc., just not the notation used in C/C++.

编辑:似乎我可能给人的印象是我不知道什么指针是。我不知道这是怎样的。)

It seems I may have given the impression that I do not know what pointers are. I wonder how that could be :)

所以为了澄清:我完全理解指针的工作原理。我不是掌握,并且奇怪地无法找到答案,是什么意思,例如'void func(int& var)'。在赋值语句的情况下,'&'运算符将在右侧,如在'int * x =&y;'中,但在上面,'&'运算符实际上在左手侧。换句话说,它运行在l值上,而不是r值上。这显然不能有同样的意义。

So for clarification: I understand perfectly how pointers work. What I am not grasping, and am weirdly unable to find answers to, is the meaning of, for example 'void func(int &var)'. In the case of an assignment statement, the '&' operator would be on the right hand side, as in 'int* x = &y;', but in the above, the '&' operator is effectively on the left hand side. In other words, it is operating on the l-value, rather than the r-value. This clearly cannot have the same meaning.

我希望我现在更有意义了。

I hope that I'm making more sense now?

推荐答案

要理解这个,你首先需要理解指针和引用。我将简单解释你要求的类型声明语法,假设你已经知道什么指针和引用。

To understand this you'll first need to understand pointers and references. I'll simply explain the type declaration syntax you're asking about assuming you already know what pointers and references are.

在C中,类型声明遵循一个' '风格。这意味着通常在一个声明中,你将有一个基类型和一个表达式,将评估该基类型。因此, int * y 声明在另一个表达式中使用的 * y 将是一个int。这意味着 y 是一个指向int的指针。这同样适用于函数参数,事实上对于整个函数声明:

In C, type declarations follow a 'declaration follows use' style. That means that generally in a declaration you'll have a base type and an expression which will evaluate to that base type. Therefore int *y declares that *y used in another expression will be an int. That implies that y is a pointer to an int. The same holds true for function parameters, and in fact for whole function declarations:

int *foo(int **bar);

在上述 int ** bar ** bar 是一个int,意味着 * bar 是指向int的指针, bar 是指向int的指针。它还声明 * foo(arg)将是一个int(适当类型的给定 arg ), foo(arg)导致指向int的指针。因此,整个函数声明读取foo是一个函数,指向指向int的指针,指向int的指针。

In the above int **bar says **bar is an int, implying *bar is a pointer to an int, and bar is a pointer to a pointer to an int. It also declares that *foo(arg) will be an int (given arg of the appropriate type), implying that foo(arg) results in a pointer to an int.¹ So the whole function declaration reads "foo is a function taking a pointer to a pointer to an int, and returning a pointer to an int."

C ++添加了引用的概念,并在过程中混淆了C风格的声明。因为使用操作符& 的地址取得变量的地址必须产生一个指针,C对于& 在声明中; int& x 意味着& x 是一个int,意味着 code>是一种类型,其中采用该类型的地址导致int。因此,因为这种语法未被使用,C ++将其用于完全不同的目的。

C++ adds the concept of references, and messes the C style declarations up a little bit in the process. Because taking the address of a variable using the address-of operator & must result in a pointer, C doesn't have any use for & in declarations; int &x would mean &x is an int, implying that x is some type where taking the address of that type results in an int.² So because this syntax is unused, C++ appropriates it for a completely different purpose.

In C ++ int& x 意味着 x 是对int的引用。使用变量不涉及任何操作符来解引用引用,因此引用声明符符号与操作符的地址冲突是没有关系的。相同的符号意味着在两个上下文中完全不同的东西,并且在允许另一个的上下文中不需要使用一个含义。

In C++ int &x means that x is a reference to an int. Using the variable does not involve any operator to 'dereference' the reference, so it doesn't matter that the reference declarator symbol clashes with the address-of operator. The same symbol means completely different things in the two contexts, and there is never a need to use one meaning in the context where the other is allowed.

因此 char& foo(int& a)声明一个引用int并返回对char的引用的函数。 func(& x)是以 x 的地址并将其传递给 func

So char &foo(int &a) declares a function taking a reference to an int and returning a reference to a char. func(&x) is an expression taking the address of x and passing it to func.

1。事实上,在原来的C语法中声明函数的声明后跟use'被更严格地遵循。例如,你要声明一个函数为 int foo(a,b),并且在其他地方声明参数的类型,这样声明看起来就像一个use,没有额外的类型名。

1. In fact in the original C syntax for declaring functions 'declarations follow use' was even more strictly followed. For example you'd declare a function as int foo(a,b) and the types of parameters were declared elsewhere, so that the declaration would look exactly like a use, without the extra typenames.

2。当然 int *& x; 可能有意义,因为 *& x 可能是一个int,实际上并不这样做。

2. Of course int *&x; could make sense in that *&x could be an int, but C doesn't actually do that.

这篇关于各种符号(*,&等)与参数的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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