I just can't seem to find a simple explanation or example of the keyword operator=
in C++.
e.g
#include <iostream>
using namespace std;
class A {};
class B {
public:
// conversion from A (constructor):
B (const A& x) {}
// conversion from A (assignment):
B& operator= (const A& x) {return *this;}
// conversion to A (type-cast operator)
operator A() {return A();}
};
int main ()
{
A foo;
B bar = foo; // calls constructor
bar = foo; // calls assignment
foo = bar; // calls type-cast operator
return 0;
}
What is operator=
? I see mentions of it in overloading operators, but the majority of the online sources do not explain the keyword operator
they just use it in their examples.
Could any one care to explain it?
I would also like to know why does the keyword this
have a *
in front of it? What is the difference between this->
and *this
?