I was watching a video and saw this code:
class Dog {
public:
Dog() : age(3), name("dummy") {}
void setAge(const int &a) { age = a; }
private:
int age;
std::string name;
};
I was curious about the function signature for setAge
because I've never used const
as a function parameter. I've looked at several related answers, but none that seemed to answer my question.
In such an elementary example, it's hard to see the benefit of passing a const
reference to a function.
Is there any reason why you'd want to make the reference a const
? The only application I could think of is in embedded programming when making a copy of a variable could waste precious space.
Are there any simple examples, perhaps, where the impact is seen easily of passing a const
reference?