In C, we can just assign one struct to another:
struct vector {
int x, y;
};
int main (void) {
struct vector v1 = {1, 1}, v2;
v2 = v1;
return 0;
}
The same goes for C++ objects:
class vector {
public:
int x, y;
vector (int a = 0, int b = 0) : x(a) , y(b){};
};
int main (void) {
vector v1(1, 1), v2;
v2 = v1;
return 0;
}
Other than classes that have pointers as member variables, and we don't want to assign memory address but values, or maybe assign object from different class - why do we want to overload the =
operator in the first place? What would be an example to a time where this is crucial? (And not the examples above?)