its an assignment operator.
so you can say
Obstacle x, y;
... stuff
x = y;
c++ allows you to overload the operators. The inputs they take are rigid (you can't make operator = (int x, double d, aclass c) for example. But you can make it do just about anything you want as long as the format for the parameters is correct, it does not have to do assignment (yours does). I have a ! logical not operator that stands for transpose for my matrix class, for an example of making it do something else.
the compiler can sometimes make the assignment operator for you, so it is often redundant. I am not sure if you really, really NEED it here or not. Google the rule of big 3 as well.
the compiler can sometimes make the assignment operator for you, so it is often redundant. I am not sure if you really, really NEED it here or not.
It looks like it's doing the exact same thing as the compiler generated copy assignment operator would have done, except that it prevents the move constructor and move assignment operator from being automatically generated.
thanks for the responses.
So I am overloading the "=" operator for the class obstacle.
What does the syntax mean? Obstacle & Obstacle::operator= (const Obstacle & other)
The "&" is the address operator right? So the operator is not defined for the actual obstacle but for obstacle pointers?
"const Obstacle &other" is a pointer to a constant obstacle? What if my obstacle is not constant?
When used as a unary operator, then, yes, it's the address operator.
However, it couldn't possibly make sense for it to be an operator here. It's being used as part of the signature of a method definition, specificly as part of declaring a type. When used in a declaration, it indicates that the type of the thing being declared is a reference:
When you pass an object as a const reference it just means that you promise that you don't change it in the function. It does not need to be constant anywhere else.
And in this particular case it means also that you cannot return it.
Ah so it was "passing by reference". I almost forgot about that, because the code I am working with now almost always passed pointers to an object...
So passing "const Obastacle& other" means I am working with the actual object (unlike a copy as in passing by value), but due to the "const", I promise that the object is not changed.
However, I never encountered the reference in the function name. Obstacle & Obstacle::operator=
"Obstacle::operator=" is the technical name of the function?
And "Obstacle&" means the function returns an obstacle reference?
So passing "const Obastacle& other" means I am working with the actual object (unlike a copy as in passing by value), but due to the "const", I promise that the object is not changed.
Correct. It's largely to improve performance, as you don't need to copy objects. It also avoids any side-effects of invoking the copy constructor, if there are any.
And "Obstacle&" means the function returns an obstacle reference?
Correct. It's necessary, to allow you to chain assignments, e.g.
a = b = c; // Where a, b and c are all of type Obstacle
//assume we have defined obstacle a...
obstacle b;
obstacle b=a
The compiler calls "=" as a function of obstacle "b". "a" is the argument of that function and is passed by constant reference, meaning "a" will not be changed (as in usual assignment).
The function copies the properties of "a" to "b" and then returns a pointer to "b".
So "b" is a pointer to "b"? Ok, I don't get it :D
Why am I even returning a pointer, if the function signature expects just a reference?
A “typical C++ programmer” writes int* p; and explains it as “p is a pointer to an int:” emphasizing type. Indeed the type of p is int*. A C++ mindset prefers that emphasis and sees it as important for using the more advanced parts of C++ well.
This is a personal preference thing, but isocpp does give a reason for doing it that way.