When doing some composition tutorials, I came across the following:
1 2 3 4 5 6 7 8 9 10 11
class People {
public:
People(Birthday &bo)
: dateOfBirth(bo) {
}
private:
Birthday dateOfBirth;
};
and...
1 2 3 4 5 6 7 8 9 10 11
class People {
public:
People(Birthday bo)
: dateOfBirth(bo) {
}
private:
Birthday dateOfBirth;
};
What would the difference be between the &bo in the first set of code and the bo (without the &) in the second set of code?
I assume this has something to do with the &bo being the memory address of the bo object (or something on those lines), but I have not been able to find a conclusive answer online.
The first is passing bo by reference (probably should be a const as well), the second is passing bo by value. When you pass by value the compiler makes a copy of the variable passing by reference avoids the copy and allows the function to change the contents of the parameter.