c++ - Accessing object unique_ptr is pointing to -
really simple question:
i'm kinda new smart pointers in c++. think got ownership stuff, have no idea how access they're pointing to. when try use member functions/variables of object functions of unique_ptr class, not want.
i can see 3 ways of doing that: operator->
, operator*
, get()
.
here running code example: ideone it
#include <iostream> #include <memory> struct foo { foo(std::string v) : value(v) {} void bar() { std::cout << "hello, " << value << "!" << std::endl; } std::string value; }; int main() { std::unique_ptr<foo> fooptr = std::make_unique<foo>("world"); fooptr->bar(); fooptr.get()->bar(); (*fooptr).bar(); return 0; }
Comments
Post a Comment