Really simple question:
I'm kinda new to smart pointers in C++. I think I got the ownership stuff, but I have no idea how to access what they're actually pointing to. When I try to use the member functions/variables of the object I just get the functions of the unique_ptr class, which is not what I want.
I can see three ways of doing that: operator->
, operator*
, get()
.
Here is a 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With