与参数组合的各种符号(*、&等)有什么区别?
可能的重复:
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& 有什么作用?func(int var)
和 int* 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
或 float
这样的基本类型,其后跟一些看起来像表达式的东西.例如,在 int *y
中,基本类型是 int
并且表达式相似是 *y
.此后,该表达式的计算结果为具有给定基类型的值.
In C, it is said that 'declaration follows use.' That means the syntax for declaring a variable mimics using the variable: generally in a declaration you'll have a base type like int
or float
followed something that looks like an expression. For example in int *y
the base type is int
and the expression look-alike is *y
. Thereafter that expression evaluates to a value with the given base type.
所以 int *y
意味着后面的表达式 *y
是一个 int
.这意味着 y
必须是一个指向 int 的指针.这同样适用于函数参数,实际上也适用于整个函数声明:
So int *y
means that later an expression *y
is an int
. That implies that y
must be 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 的指针中.1 所以整个函数声明读作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.1 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,暗示 x
是某种类型,其中获取该类型的地址会导致int.2 所以因为这个语法没有被使用,C++ 将它用于一个完全不同的目的.
C++ adds the concept of references, and messes 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.2 So because this syntax is unused, C++ appropriates it for a completely different purpose.
在 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 语法中,更严格地遵循声明跟随使用".例如,你将一个函数声明为 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,但 C 实际上并没有这样做.
2. Of course int *&x;
could make sense in that *&x
could be an int, but C doesn't actually do that.
相关文章