Friday, 9 September 2016

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...