C++ Composition & confusion

Hey there,

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.


Thanks (and sorry for the newbie question)
Birthday& bo is a reference to an object instead of a "copy" of an object

This means you can modify the Birthday you pass in and it doesn't allocate any additional memory for a copy.
Last edited on
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.

Thanks, I understand now!
Topic archived. No new replies allowed.