Use of the '&' symbol in C++

This question already has an answer here:

  • The Definitive C++ Book Guide and List 1 answer
  • What are the differences between a pointer variable and a reference variable in C++? 32 answers

  • "&" can mean several different things, depending on the context.

    The example you gave above is the C++ "reference operator":

    Need help understanding reference operator(C++) in specific functions

    The reference operator is specific to C++. "&" can also be used as the "address of" operator, used in both C and C++:

    What are the differences between a pointer variable and a reference variable in C++?

    Finally, "&" can also be the bitwise "AND" operator:

    http://www.cprogramming.com/tutorial/bitwise_operators.html


    It means pass by reference, the object that is passed can be manipluated directly. For example when you write public void TestMethod(obj value) any changes you make to value do not affect the original value(ie a copy is made) however when you pass by reference any changes you make to value change the original value.


    In the code you provided in your question & symbol acts for passing argument by reference in your operator+ function. If you remove it your code still will be able to compile and will act the same way (in your case) but arguments will be passed in your operator+ function by value (will be copied).

    链接地址: http://www.djcxy.com/p/6850.html

    上一篇: 如何在C ++中启动面向对象编程?

    下一篇: 在C ++中使用'&'符号