C++ const int*& var 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1257507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:20:29  来源:igfitidea点击:

What does this mean const int*& var?

c++

提问by seg.server.fault

I saw someone using this

我看到有人用这个

void methodA(const int*& var);

in one answer, but couldn't understand what the argument means.

在一个答案中,但无法理解该论点的含义。

AFAIK:

AFAIK:

  • const int var=> const value which can't be changed

  • const int* var=> pointer to int which is const i.e *var can't be changed but var can be changed

  • const int& var=> reference to const int i.e value of var can't be changed

  • const int var=> 无法更改的常量值

  • const int* var=> 指向 int 的指针,即 const 即 *var 不能更改但 var 可以更改

  • const int& var=> 对 const int 的引用,即无法更改 var 的值

What does const int*& varmean, and is const int& *varalso possible?

什么const int*& var意思,const int& *var也有可能?

Can you please give some example as well, like what can be done and what can't be done with it?

您能否也举一些例子,比如什么可以做,什么不能做?

UPDATE :

更新 :

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,

我不确定我的想法是否正确,但我开始认为引用是作为参数传递的变量的别名,所以 const int * p; methodA(p) => 这里我们将 p 作为 const int * 传递,但我们不知道这是按值传递还是什么,直到我们看到方法 A 的定义,

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,

所以如果methodA就像这个methodA(const int * & p2) ==>这里p2是p的另一个名字,即p和p2从现在开始是一样的,如果methodA(const int * p2) ==>这里p2被传递因为值即 p2 只是此方法的本地值,

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 ?

如果我想错了,请纠正我?如果是,我可能需要更多地研究这个?你能指出一些不错的参考资料吗?

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

更新 2 如果像我这样的初学者想了解更多关于这件事的信息,您可以使用c++decl / cdecl program,我刚刚从这里发现它非常有用

$ 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

But as every one here pointed, first example isnt legal in C++.

但正如这里的每个人所指出的,第一个例子在 C++ 中是不合法的。

Thanks

谢谢

回答by GManNickG

It is a reference to a pointer to an int that is const.

它是对指向 int 的指针的引用,该指针是 const。

There is another post somewhat related, actually, here. My answer gives a sorta of general algorithm to figuring these things out.

还有另一篇有点相关的帖子,实际上,在这里。我的回答提供了一种解决这些问题的通用算法。

This: const int& *varhas no meaning, because you cannot have a pointer to reference.

This:const int& *var没有意义,因为你不能有一个指向引用的指针。

If the const's and pointers are getting in the way, remember you can typedef these things:

如果 const 和指针妨碍了,请记住您可以键入以下内容:

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

Might make it easier to read.

可能会更容易阅读。



If you need more help on C++, read this. More specifically, references.

如果您需要有关 C++ 的更多帮助,请阅读此。更具体地说,参考资料

References as variables do nottake 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)
}

So it's actually passing the address of ion the stack, so takes sizeof(int*)space on the stack. But don't start thinking about references as pointers. They are notthe same.

所以它实际上是i在堆栈上传递的地址,所以sizeof(int*)在堆栈上占用空间。但是不要开始将引用视为指针。它们一样。

回答by Stephen Nutt

Some folks find it easier reading this from right to left. So

有些人发现从右到左阅读更容易。所以

const int*&

常量整数*&

is a reference to a pointer to an integer that is const.

是对指向常量的指针的引用。

As you know, references cannot be changed, only what they refer to can be changed. So the reference will refer to just one pointer to an integer that is const. Since the pointer is not const - the integer is const - you can change the pointer to point to a different integer.

如您所知,引用无法更改,只能更改引用的内容。因此,引用将只引用一个指向常量的指针。由于指针不是 const - 整数是 const - 您可以将指针更改为指向不同的整数。

Compare this to

将此与

int* const &

int* const &

This is a reference to a constant pointer to an integer. Again the reference is immutable, and in this case it is a reference to a constant pointer. What you can change in this case is the integer value since there was no const either side of the int keyword.

这是对指向整数的常量指针的引用。同样,引用是不可变的,在这种情况下,它是对常量指针的引用。在这种情况下,您可以更改的是整数值,因为 int 关键字的两侧都没有 const。

Just to add confusion, const int and int const are the same. However int const * and int * const are very different. The first is a pointer to a constant integer, so the pointer is mutable. The second is a constant pointer to an integer, so the integer is mutable.

只是为了增加混淆, const int 和 int const 是相同的。然而 int const * 和 int * const 是非常不同的。第一个是指向常量整数的指针,因此该指针是可变的。第二个是指向整数的常量指针,因此整数是可变的。

Hope this helps!

希望这可以帮助!

回答by Martin Liversage

It is a reference to a const pointer, i.e. a pointer where you cannot modify the data pointed to. As the reference is used as an argument to a method the method is able to modify the pointer to let it point to something else (still something that cannot be modified).

它是对 const 指针的引用,即不能修改指向的数据的指针。由于引用用作方法的参数,因此该方法能够修改指针以使其指向其他内容(仍然是无法修改的内容)。

With regards to your update:

关于你的更新:

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

所以如果methodA就像这个methodA(const int * & p2) ==>这里p2是p的另一个名字,即p和p2从现在开始是一样的,如果methodA(const int * p2) ==>这里p2被传递作为值,即 p2 只是此方法的本地值

Yes, you are correct.

是的,你是对的。

回答by Michael Burr

In your example, varis a refernce to a pointer to const char.

在您的示例中,var是对指向 const char 的指针的引用。

Since it's a reference, a change to the parameter inside methodA()will be reflected in the argument that is passed to methodA():

由于它是一个引用,对内部参数的更改methodA()将反映在传递给 的参数中methodA()

void methodA( const char*& var)
{
    static const char newdata[] = {'a', 'b', 'c', '##代码##'};

    printf( "var points to %s\n", var);

    var = newdata;
}


int main()
{
    const char * p = "123";

    printf( "p points to: %s\n", p);      // prints "p points to: 123"
    methodA( p);
    printf( "now p points to: %s\n", p);  // prints "now p points to: abc"
}