23

I realize this is probably a dumb question but I couldn't find the answer anywhere... What is the purpose of the '&' symbol in C++ functions? Such as

vec2& operator+(vec2& left, const vec2& right)
{
    return left.add(right);
}

I'm following a youtube series that's a little over my head, but I'm doing fine because all the code is there. However that 'and' symbol keeps popping up and I'd really like to know what it is... Does it have something to do with classes?

Here's exactly what I'm watching: https://www.youtube.com/watch?v=-peYVLeK0WU Guy from a channel called "TheChernoProject" making a simple game engine. Also, this is Visual Studio 2013 C++, if that changes anything.

L. F.
  • 19,445
  • 8
  • 48
  • 82
Gector
  • 379
  • 1
  • 2
  • 8
  • Lookup reference parameters. – πάντα ῥεῖ Jan 24 '16 at 06:20
  • 1
    http://en.cppreference.com/w/cpp/language/reference – Ivan Aksamentov - Drop Jan 24 '16 at 06:23
  • 3
    [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Ivan Aksamentov - Drop Jan 24 '16 at 06:23
  • 5
    It's probably not a good idea trying to learn c++ from videos or by asking very basic stuff at Stack Overflow. – πάντα ῥεῖ Jan 24 '16 at 06:27
  • Hey, first question on this site, thanks for the feedback and I'm sorry this isn't a reverent question. – Gector Jan 24 '16 at 06:29
  • 3
    @GectorWhitlox: Book recommendation: [Programming Principles and Practice Using C++, Bjarne Stroustrup](http://www.amazon.com/Programming-Principles-Practice-Using-Edition/dp/0321992784). It helps you learn C++. More importantly, it gives you insight as to *why* C++ works the way it does, in the context of "real world" problems. – paulsm4 Jan 24 '16 at 06:32
  • @πάνταῥεῖ I learned my first three languages by starting with videos until I finally understood what I was writing. Infact "TheCherno" is the one that showed me Java. ;) –  Sep 18 '16 at 10:17

3 Answers3

21

"&" 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

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
5

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(i.e a copy is made) however when you pass by reference any changes you make to value change the original value.

5

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).

fnc12
  • 2,241
  • 1
  • 21
  • 27