Const References & Out Parameters

So this means references should always have a const before a reference right to avoid errors down the line?

Update: So they don’t to have a const always. I’ve just been wondering if I can forcibly change the reference address with a new pointer for learning purposes but it doesn’t let me, instead telling me it’s the type I assigned to it before.

Which I find confusing because isn’t a reference also a memory address? Same as a pointer so I should be able to do that.

Yes but you should think of them as an alias. Modifying a reference modifies the referent.

Well, no because that’s one the main points of references. They must be initialised and once initialised can’t be modified to refer to a different object. These two points is why they are safer to use than pointers; it ensures they aren’t null unless you beyond the rules of the language.

Privacy & Terms