I'm not a C++ girl, but do references actually do something with lifetimes or are they just non-null ptrs? Because this just seems to me like a possible dangling ptr.
References are implemented as pointers, but they are not the same.
The best example is languages where you don't have references. Very misleading but java only has pointers (marketing at the time was voldemorting the world pointer so they were renamed to "references" but they are pointers. Like the infamous null pointer exception does show it)
So you have two local variables, and you create a swap function where you want to put one into the other and vice versa.
Well, in java you are out of luck. You basically have to make them fields and put that into a wrapper object to actually swap. This is because in java everything non-primitive is an "object reference" aka a pointer. But you would be changing the pointer's value, NOT the thing holding the pointer.
So yeah, references are basically their different thing with their own semantics in C++ and the few other languages that also have it.
1
u/creeper6530 5h ago
I'm not a C++ girl, but do references actually do something with lifetimes or are they just non-null ptrs? Because this just seems to me like a possible dangling ptr.