Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 与参数组合的各种符号(*、&;等)之间有什么区别?_C++_Syntax_Notation - Fatal编程技术网

C++ 与参数组合的各种符号(*、&;等)之间有什么区别?

C++ 与参数组合的各种符号(*、&;等)之间有什么区别?,c++,syntax,notation,C++,Syntax,Notation,可能重复: 我知道对你们中的许多人来说,这似乎是一个难以置信的基本问题,但我真的不可能找到一个好的、彻底的解释,尽管我在谷歌上搜索得很好。我确信答案就在那里,所以我的搜索词一定很糟糕 C++中,使用各种符号和组合来标记参数(以及这些参数的参数)。它们的确切含义是什么 例:void func(int*var)和void func(int**var)之间有什么区别?那么int&var呢 同样的问题代表返回类型和参数。与int*func(int-var)相比,int&func(int-var)意味

可能重复:

我知道对你们中的许多人来说,这似乎是一个难以置信的基本问题,但我真的不可能找到一个好的、彻底的解释,尽管我在谷歌上搜索得很好。我确信答案就在那里,所以我的搜索词一定很糟糕

C++中,使用各种符号和组合来标记参数(以及这些参数的参数)。它们的确切含义是什么

例:
void func(int*var)
void func(int**var)
之间有什么区别?那么
int&var

同样的问题代表返回类型和参数。与
int*func(int-var)
相比,
int&func(int-var)
意味着什么?在参数中,
y=func(*x)
y=func(&x)
有何不同

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

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

因此需要澄清的是:我完全理解指针是如何工作的。我没有领会,而且奇怪地无法找到答案的是,例如“void func(int&var)”的含义。对于赋值语句,“&”运算符将位于右侧,如“int*x=&y;”,但是在上面的例子中,&运算符实际上位于左侧。换句话说,它是在l值上运行,而不是在r值上运行。这显然不能有相同的含义


我希望我现在讲得更有意义了?

你所问的是所谓的指针(
*
)和引用(
&
),我认为是这样。

符号和*分别用于表示引用和指针类型

int
仅表示“int”类型,
int*
表示“指向int的指针”,
int&
表示“引用int”

指针是用于存储变量地址的变量。 引用具有其基类型的语法,但具有指向该类型的指针的语义。这意味着您不需要为了更改值而取消对它的引用

举例来说,以下两个代码块在语义上是等价的:

int* a = &value;
*a = 0;
以及:

使用指针或引用作为参数类型的主要原因是为了避免复制对象,并能够更改传递的参数的值。这两种方法都有效,因为当您通过引用传递时,只复制地址,从而使您能够访问“传递”给函数的相同内存位置


相反,如果未使用引用或指针类型,则会生成参数的完整副本,函数中提供的就是该副本。

要理解这一点,首先需要理解指针和引用。假设您已经知道指针和引用是什么,我将简单地解释您所询问的类型声明语法

在C语言中,有人说“声明跟随使用”。这意味着声明变量的语法类似于使用变量:通常在声明中,在类似表达式的后面会有一个基本类型,如
int
float
。例如,在
int*y
中,基本类型为
int
,表达式的外观类似于
*y
。此后,该表达式计算为具有给定基类型的值

所以
int*y
意味着后面的表达式
*y
int
。这意味着
y
必须是指向int的指针。函数参数和整个函数声明也是如此:

int *foo(int **bar);
在上面的
int**bar
中说
**bar
是一个int,这意味着
*bar
是指向int的指针,
bar
是指向int的指针。它还声明
*foo(arg)
将是一个int(给定的
arg
,这意味着
foo(arg)
产生一个指向int的指针。因此整个函数声明显示“foo是一个函数,它获取指向int的指针,并返回指向int的指针。”

C++添加了引用的概念,并在这个过程中稍微弄乱了C风格的声明。因为使用运算符
&
的地址获取变量的地址必须产生指针,所以C在声明中对
&
没有任何用处<代码> INT/X/<代码>将意味着>X/Cuth>是int,这意味着 X/Cux>是席类型的地址导致INT.I的一种类型,因此,由于该语法未被使用,C++将其用于完全不同的用途。 < C++ >代码> int和x <代码>意味着<代码> x>代码>是int的引用。使用变量不涉及任何操作符“引用”引用,所以引用声明符与操作符地址冲突并不重要。同一符号在两种语境中的含义完全不同,在允许使用另一种含义的语境中,不需要使用一种含义

因此
char&foo(int&a)
声明一个函数,该函数获取对int的引用并返回对char的引用
func(&x)
是一个表达式,它获取
x
的地址并将其传递给
func


一,。事实上,在声明函数的原始C语法中,“声明遵循使用”甚至得到了更严格的遵循。例如,您将函数声明为
intfoo(a,b)
,参数类型声明为e
int *foo(int **bar);
(1)
    <return type> <function name> <parameters> 
    void           func            (int *var) 

    <parameter> here int *var is a pointer to integer, ie it can point to 
    an array or any buffer that should be handled with integer pointer 
    arithmetic. In simple terms , var holds the address of the respective 
    **actual parameter.**

    eg: int arr[10]; 
        func(arr);

        int a = 33;
        func(&a);

        Here, &a means we are explicitly passing address of the the variable 'a'. 
 (2) 
     int m = 0; 
     int &var = m;

     Here var means reference, ie it another alias name for variable 'm' , 
     so any change 'var' makes will change the contents of variable 'm'.

     var = 2; /* will change the actual contents of 'm' */ 

     This simple example will not make sense , unless you understand the context. 
     Reference are usually use to pass parameter to function, so that changes made by 
     the function to the passed variable is visible at the caller.

     int swap(int &m, int &n) {
         tmp = m;
         m = n; 
         n = tmp; 
     }   

     void main( void ) {

        int x = 1, y = 2;
        swap(x, y);
        /* x = 2, y =1 */   

     }

(3) 
     'int& func(int var)' mean, as compared to 'int* func(int var)'?

      int& func(int var) means the function returns a reference; 
      int* func(int var) means the function returns a pointer / address; 

      Both of the them has its context; 


      int& setNext() {

            return next;   
      } 

      setNext() = previous; 


      where as 

      int* setNext() {

           return &next; 
      } 

      int *inptr;
      inptr = setNext(); 
      *inptr = previous;

      In the previous two lines, 

      int *inptr <- integer pointer declaration; 
      *inptr <- means we are accessing contents of the address pointed by inptr; 
      ie we are actually referring to 'next' variable. 

      The actual use is context specific. It can't be generalized.  

(4) 
    how does 'y = func(*x)' differ from 'y = func(&x)'?                           

     y = func(&x) is already explained. 

     y = func(*x) , well i'm not sure if you actually meant *x.


     int swap(int *m, int *n) {
         tmp = *m;
         *m = *n; 
         *n = tmp; 
     }