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

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -