这意味着const int *& var?

编程入门 行业动态 更新时间:2024-10-08 20:39:42
本文介绍了这意味着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?

本文发布于:2023-08-07 22:43:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1322210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这意味着   const   int   amp   var

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!