submitted 1 month ago by [email protected] to c/[email protected]
you are viewing a single comment's thread
view the rest of the comments
[-] [email protected] 4 points 1 month ago* (last edited 1 month ago)

Smart pointers model ownership. Instead of (maybe) a comment telling you if you have to free/delete a returned pointer this information is encoded into the type itself. But that's not all, this special type even handles the whole deleting part once it goes away.

Since they model ownership you should only use them when ownership should be expressed. Namely something that returns a pointer to a newly allocated thing should be a std::unique_ptr because the Callie has ownership of that memory. If they want to share it (multiple ownership of the object) there's a cheap conversion to std::shared_ptr.

How about a function that takes in an object cause it wants to look at it? Well that doesn't have anything to do with ownership so make it a raw pointer, or better yet a reference to avoid nullability.

What about when you've got a member function that wants to return a pointer to some memory the object owns? You guessed it baby raw pointer (or again reference if it'll never be null).

this post was submitted on 15 May 2024
587 points (97.0% liked)

Programmer Humor

18039 readers
446 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.


founded 1 year ago