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.
std::shared_ptr<Foo> foo = std::make_shared(Foo(10));?get()when you need the raw pointer, e.g. to send it to a legacy function that expects one.