10
nitwhiz
6y

C++ Warriors: Do you use smart pointers?

Yes/No/Depends.

Comments
  • 6
    Answer: Yes and No and Depends!
  • 2
    Yeah, it depends. I try to use smart pointers as sparingly as possible, but there are tons of cases where it's the best thing you can do.
    However, raw pointers are still cool for a lot of things.

    This may interest you: https://youtu.be/xnqTKD8uD64
  • 2
    Why pointers could be smart? Just
    *Googlin it
    wut?
    *Reading pages
    FRIGGG
    C++ is trying to imitate Java!
    Fellow comrades, THE END TIMES ARE UPON US!
    Jk, in fact, i've seen to much lack of delete[]; 's
  • 2
    Wouldn't get past code review if I didn't.
  • 1
    Thanks for the input!

    @RememberMe, definitely going to look into that.

    I myself don't really like the java-style giving stuff out of my hands. If i didn't get it wrong, shared pointers just free themselves, so the only thing you saved yourself from is a free-call for the cost of runtime, am i right?
    I liked the "do shit yourself or leave it" nature of C. C++ doesn't seem to go that way.^^
  • 1
    @hch11 lol, if you want more Java "imitation" there's a pretty good garbage collector implementation for C/C++ : the Boehm GC.
  • 2
    I tend to use mostly unique_ptr, because it adds very little overhead
  • 0
    @PAKA is there any reason besides prevention of memory leaks by forgetting something for enforcing this?
  • 2
    Pretty much always yes unless I'm holding a non owning pointer. In that case I just use a raw pointer or a reference if I want the class lifetime bound to what I'm pointing at
  • 0
    @stevemk14ebr and that's what i don't like about smart pointers. They automatically bring inconsistency to your code.
    The C subset uses raw pointers and I guess the lib functions, do so, too. (pure guess! Is that so?)

    Also it makes stuff unnecessarily complex, now we have one way that always works + 3 other ways + 1 deprecated way to point to an object in memory.
  • 1
    @daintycode Mostly "because tech lead said so"
Add Comment