589
Perpetual Motion finally achieved!
(lemmy.zip)
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.
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 tostd::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).