2

This seems like a pretty dumb question, so please bear with me. I am using smart pointers in place of raw pointers in my programs. I am advised against using raw pointers or mixing the two as much as possible. I understand that as well. I am also aware that pointers should be used only went necessary.

class Foo{
  private: int val;
  public: 
    Foo(int v) :val(v){}
    int getvalue() const { return val; }
};

std::shared_ptr<Foo> foo = std::make_shared(Foo(10));

int v;

//Option I
v=foo->getvalue();

//Option II
v=foo.get()->getvalue();

I feel option I is more correct as option II utilizes raw pointer. But using raw pointer may not hurt here as I am not allocating or deallocating.

Often I get confused with these two options. Which one is preferable? Are they just same? Thanks.

3
  • 2
    "...as I am not allocating or deallocating..." what do you think this line does std::shared_ptr<Foo> foo = std::make_shared(Foo(10)); ? Commented Feb 16, 2020 at 13:42
  • They're the same. The first one is preferable because it's less typing. You use get() when you need the raw pointer, e.g. to send it to a legacy function that expects one. Commented Feb 16, 2020 at 13:42
  • @Ricard I don't mean that line but the option lines . Commented Feb 16, 2020 at 13:44

3 Answers 3

5

Raw pointers are not the enemy. We can't write any useful program in C++ without them in one shape or form. The problem is owning raw pointers. They don't have a clear ownership semantic associated with their type, and so it fall to the programmer to do the arduous task of managing the lifetime by hand. Don't fear passing a raw pointer around as a handle, if you need to.

Which brings us to the next point. Both methods you demonstrated rely on raw pointers. An overloaded operator-> must return a raw pointer (eventually) for us to apply member access on the object. In essence, the first version is equivalent to

v = foo.operator->() -> getvalue();

It operates exactly the same as the second form, just with syntactic sugar on top.

You should prefer the first form because it is shorter and readable. But it doesn't avoid raw pointers, that's impossible to avoid.

Sign up to request clarification or add additional context in comments.

Comments

4

There is no functional difference between

//Option I
v=foo->getvalue();

And

//Option II
v=foo.get()->getvalue();

But option II is more typing, so why?

Both constructs do exactly the same in this situation.

This has nothing to do with smart pointers vs raw pointers. The problem with raw pointers is purely with raw owning pointers. There's nothing wrong with raw non-ownining pointers.

Comments

1

Both option will probably get the same result in the assembled code. The first option foo->getvalue() is the prefered option, because this gives a better readability.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.