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

Programmer Humor

19443 readers
37 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.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 4 points 5 months ago* (last edited 5 months 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).