I never use pointers in my C++ code. Am I coding C++ wrong?

It's hard to tell whether you code wrong or not...

When you use the standard library you can actually avoid pointers to a certain degree. They are handling them it for you. An iterator has many traits of a pointer.

It is certainly a good idea to avoid pointer when they are not necessary. Howver you can use pointer to determine the existence of data. For instance when you are looking in a vector for e.g. login data you may return a pointer where a nullptr tells that the data does not exists.

By the way: In Java you are basically use permanently pointer. Almost all non privmitve object including string are pointer.
You are going about it the right way, strongly favouring std::vector<>, std::string etc. over smart pointers and the like. They manage their internal resources for you; they do use pointers internally. The general recommendation is: use pointers only in situations where one sees a clear need for using them.
I never use pointers in my C++ code. Am I coding C++ wrong?


No. But you should understand them as there is an awful lot of current C++ code that does use pointers. As others have pointed out, if you don't understand them you could create massive problems - even with code that seems to work but in reality isn't right.
closed account (z05DSL3A)
Butler2653 wrote:
it is not like I don't know how to use pointers but I just never found a moment where I think a pointer would be useful.
closed account (z05DSL3A)
Oh so he's a university student and therefore deserves your contempt, how could he know enough about the dreaded pointer?
Butler2653, as coder777 mentioned, Java already uses pointers; it just doesn't call them that. When you pass an object to a function, you are essentially passing a handle to that object, which is just a pointer -- the handle is copied, but you can still access the stuff it points to from within the function.

But as far as C++ goes, one thing I would suggest practicing is using the pre-made functionality inside the <algorithm> header. These algorithms are mainly made using iterators (which are like pointers in a sense) to decouple them from the specifics of the container itself. So it's good to know how these algorithms work, because a lot of code can be re-written and simplified by using these functions correctly.
https://www.cplusplus.com/reference/algorithm/
closed account (z05DSL3A)
superfluous wrote:
Rage much?

No, but I do a good line in sarcasm.
superfluous wrote:
No doubt they've got the ability to learn, but i for one would avoid working with them in their current state like the plague.

No rage here, but I'm with Grey Wolf - that sentence was unnecessarily snide. It was a hostile thing to post, and adds nothing of value to the discussion.
Last edited on
@Butler2653,

I'm with you, I try to write code that avoids using raw pointers as much as possible. When using a pointer makes the code easier to construct and understand I opt for a smart pointer if at all possible.

I don't avoid using iterators. Even though they are "pointer-like" they are a part of the C++ standard library for a reason.

Keep on writing code!
@OP, do you mean that you never use raw pointers? Or that you never use anything that behaves like a pointer, or wraps one, like a smart pointer, or an iterator?

Cause, while there are still valid uses for raw pointer, it's pretty rare to use them in modern C++. The same is not true of smart pointers and iterators; there are plenty of valid uses for them still.
Last edited on
I write a fair amount of, what I fondly believe is 'fairly modern C++'. In the few places where pointers are needed, I tend to find far more uses for non-owning raw pointers than for smart pointers.
iterators still feel like holding a hammer and seeing nails everywhere to me. I need them and use them because the built in tools require me to do so. None of those tools do anything that could not be done without this device, though. It feels like eggheaded theorycrafted design to me. Probably, there is just some area of coding that I haven't been involved with where the things are a godsend, but so far, they feel like a cryptic wrapper for a simple pointer or worse, just an integer.
Topic archived. No new replies allowed.