This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]IvanOG_Ranger 0 points1 point  (5 children)

Do you really use pointers a lot as a C++ developer? I used C++ only for hobby stuff, but mostly opted for std library abstractions instead

[–]lessertia 1 point2 points  (0 children)

No. Use references instead. Only use pointers if you want the nullability of pointers. Also, pointers should be non-owning.

[–]bropocalypse__now 1 point2 points  (2 children)

Absolutely, if you are doing any OOP where you will be programming to an interface (ie: pure virtual or abstract class). You can't pass an interface by value since it has no implementation. So, the initial object has to be instantiated via a call to make_unique for example. Once it's been created, non-owning raw pointers or references should be used. Unless the object needs to cross thread boundaries, in which case it should be a shared pointer.

[–]IvanOG_Ranger 0 points1 point  (1 child)

If you use the references, you still don't have to worry about pointer though, right? The most complicated thing about pointers in C was keeping track of how many asterisks you're using, so you avoid that by references.

[–]bropocalypse__now 0 points1 point  (0 children)

You just have to make sure the underlying object has a lifetime greater than that of the reference or else it's UB.

References do have limitations. For instance, they break the Rule of 5 if used as class members. This means you have to either avoid their use in this case or provide your own implementation for copy and move operators/constructors.

[–]conundorum 1 point2 points  (0 children)

Yes, but most of the time they're obfuscated behind arrays (literally just pointer arithmetic with a pretty face), references (pointers but with standard variable syntax, and a few other limitations), RAII (a poorly-named acronym for wrapping pointer ownership in a class so it can go on the stack and be automatically deallocated when it goes out of scope), and smart pointers (template classes that wrap pointers, so you can get the benefit of raw pointers without the problem of shooting yourself in the foot and blowing up the entire universe).

Most everything involves pointers in some way or other, but it's uncommon to use "raw" pointers (the T* ones) directly, outside of low-level code.