Originally Posted by
Elysia
But very inefficient.
How is it inefficient? Do you mean in this comparison?
Code:
object *p = get_first_object();
// use p
p = get_second_object();
// use p
vs
Code:
object &r1 = get_first_object();
// use r1
object &r2 = get_second_object();
// use r2
Because any compiler with even a primitive flow analysis will just reuse the same storage space for the second reference if r1 isn't used anymore after r2 is assigned. And if it is, it still works, whereas the pointer reassign snippet just misbehaves.
Never has been for me. Since I use OO a lot, I need classes that keeps track of other classes, and that means pointers.
I still don't see the connection to OO.
And in any case, that's just one case usage scenario. Where pointers can be used for many more.
That's exactly their weakness. Or rather, that's what makes them a low-level component that should be used to build high-level components (smart pointers, containers) that are then used in high-level code.
...But I tire of this. Everyone has their own way of coding and we should stick to that.
If we always stick to the way we already do it, how do we get better? The nice thing about this forum is that you can discuss the relative merit of various approaches.
There's really no good or bad way.
There's no universal best way. There definitely are good and bad ways. For example, not using RAII and using endless try...catch blocks in an attempt to make code exception-safe would be a bad way. (Not that I'm accusing you of doing that. It's just an example that springs to mind whenever I read about raw pointers.)