Monday, 26 June 2017

Use of the '&' symbol in C++




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.


Answer




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


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...