这意味着const int *& var? [英] What does this mean const int*& var?
问题描述
我看到有人在使用
void methodA(const int*& var);
一个答案,但不明白该参数的含义。
in one answer, but couldn't understand what the argument means.
AFAIK:
-
const int var
=>不能更改的const值
const int var
=> const value which can't be changed
const int * var
=>指向int,即const,即* var不能更改,但var可以更改
const int* var
=> pointer to int which is const i.e *var can't be changed but var can be changed
const int& var
=>对const int的引用,即var的值无法更改
const int& var
=> reference to const int i.e value of var can't be changed
const int *&是什么? var
的平均值,并且是 const int& * var
也可以吗?
What does const int*& var
mean, and is const int& *var
also possible?
您能否也举一些例子,例如可以做什么和不能做什么? ?
Can you please give some example as well, like what can be done and what can't be done with it?
更新:
我不确定我的想法是否正确,但我开始考虑参考作为作为参数传递的变量的别名,因此
const int * p;
methodA(p)=>在这里,我们将p作为const int *传递,但是直到我们看到方法A的定义,我们才知道这是按值传递还是传递值,
I am not sure if I am thinking right way, but I began to think reference as alias of the variable that was pass as argument, so const int * p; methodA(p) => here we are passing p as const int * but we dont know if this is pass by value or what until we see the definition of method A,
因此,如果methodA像这样
methodA(const int *& p2)==>这里p2是p的另一个名称,即p和p2从现在起在
上是相同的,如果methodA(const int * p2)==>此处p2作为值传递,即p2仅在此方法本地,
so if methodA is like this methodA(const int * & p2) ==> here p2 is another name to p, i.e. p and p2 are same from now on and if methodA(const int* p2) ==> here p2 is passed as value i.e p2 is just local to this method,
如果我想错了方法,请纠正我?如果是,我可能需要进一步研究?你能指点一些不错的参考吗?
please correct me if I am thinking wrong way ? If yes, I might need to study some more about this ? Can you please point some nice references ?
更新2
如果像我这样的初学者想了解更多关于这件事的信息,可以使用 c + + decl / cdecl程序,我刚刚从此处
UPDATE 2 If some beginner like me want to know more about this thing, you can use c++decl / cdecl program, which I just discovered to very useful from here
$ c++decl
Type `help' or `?' for help
c++decl> explain const int&* p
declare p as pointer to reference to const int
c++decl> explain const int*& p
declare p as reference to pointer to const int
的指针的引用,但是正如这里每个人所指出的,第一个示例在C ++中不是合法的。
But as every one here pointed, first example isnt legal in C++.
谢谢
推荐答案
还有另一篇文章有些相关,实际上是此处。我的答案给出了一种一般的算法来解决这些问题。
There is another post somewhat related, actually, here. My answer gives a sorta of general algorithm to figuring these things out.
此: const int& * var
没有意义,因为您没有指向引用的指针。
This: const int& *var
has no meaning, because you cannot have a pointer to reference.
如果const和指针妨碍了您的操作,请记住您可以键入以下内容:
If the const's and pointers are getting in the way, remember you can typedef these things:
typedef int* IntPointer;
typedef const IntPointer ConstIntPointer;
void foo(ConstIntPointer&); // pass by reference
void bar(const ConstIntPointer&); // pass by const reference
void baz(ConstIntPointer); // pass by value
可能会更易于阅读。
如果您需要有关C ++的更多帮助,请阅读此内容。更具体地说,引用。
If you need more help on C++, read this. More specifically, references.
参考因为变量 not 不占用空间:
References as variables do not take space:
int i; // takes sizeof(int)
int*pi = &i; // takes sizeof(int*)
int& ri = i; // takes no space.
// any operations done to ri
// are simply done to i
作为参数的引用使用指针来达到最终效果:
References as parameters use pointers to achieve the end effect:
void foo(int& i)
{
i = 12;
}
void foo_transformed(int *i)
{
*i = 12;
}
int main()
{
int i;
foo(i); // same as:
foo_transformed(&i); // to the compiler (only sort of)
}
因此,它实际上是在传递地址 i
在堆栈上,因此在堆栈上占用 sizeof(int *)
空间。但不要开始考虑将引用作为指针。它们不相同。
So it's actually passing the address of i
on the stack, so takes sizeof(int*)
space on the stack. But don't start thinking about references as pointers. They are not the same.
这篇关于这意味着const int *& var?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!