Why to use references rather than pointers when there is no compile time type checking on references?
Imagine that you are creating an object of type Person
that needs to hold a reference/pointer to their House
(pretty random) - so the person object would hold the house reference/pointer as a data member. Imagine this was done with references:
class Person{
public:
Person(House& myHouse) : house(myHouse) { }
private:
House& house;
};
The issue I see though is if you forget to include the & in the data member declaration that thing you thought was a reference becomes a copy. Surely it would be safer to pass and save a pointer so this error would be flagged up at compile time?
I'm not making a point to use one or the other - I'm asking why are references used over pointers to save data members?