I'm fairly new to programming in C++ and I was wondering something:
whenever I see operator overloading in C++ it's done like this:
#ifndef STONE_H
#define STONE_H
class Stone {
private:
int weight;
public:
.......
Stone operator+(const Stone& s) {
Stone stone;
stone.weight = this->weight + s.weight;
return stone;
}
.......
}
#endif
But when the "+" operator is called, it creates an object "stone", and returns a copy of this. This can't be good for performance when dealing with huge objects?
Wouldn't it be better to use dynamic memory for this as in the example below:
Stone * operator+(const Stone& s) {
Stone * stone = new Stone;
stone->weight = this->weight + s.weight;
return stone;
}
Or am I seeing this wrong?
Thanks in advance