When you get the instance of crew by reference, its private members are still protected. Passing by reference is just to specify that you want the ACTUAL variable you passed, not a copy of it.
Now, with references you can do something like this...
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class Foo
{
public:
int & getBar() { return my_int; }
private:
int my_int;
};
int main()
{
Foo foo;
int & int_reference = foo.getBar();
// Uh oh, we now have unlimited access to a private member of foo now
}
|
That kind of code you definitely want to avoid. Never return a reference or pointer to a private member from a public function, unless you can use const correctness, which in the above example there isn't. You could fix that by doing...
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class Foo
{
public:
const int & getBar() { return my_int; }
private:
int my_int;
};
int main()
{
Foo foo;
int & int_reference = foo.getBar();
// Wrong, int_reference isn't declared to be a const reference, so this won't even compile
}
|
If you return a const reference/pointer, then it's read only and your referenced data can't be addled with. If it's a class/struct, then only const qualified functions can be used, which in your case, you didn't const qualify any of your methods. You can apply that to your getters, i.e...
1 2 3 4 5
|
class Crew
{
public:
int GetStrength() const { return Strength; }
...
|
Now your GetStrength() method guarentees that nothign can be mutated with
GetStrength
,
so if you have
Crew
as a const argument, you can still use
GetStrength
.