I'm attempting to create an accessor for a class member variable using smart pointers. Here's the code:
class MyResource
{
};
class MyClass
{
public:
std::unique_ptr<MyResource> getResource();
private:
std::unique_ptr<MyResource> resource;
};
std::unique_ptr<MyResource> MyClass::getResource()
{
return this->resource;
}
The error I get trying to compile this:
cannot access private member declared in class 'std::unique_ptr<_Ty>'
Adding .get
to this->resource
of course doesn't work because the return type changes.
Should I not be using a unique_ptr here? Is this just a syntax issue? Am I totally on the wrong track?
my background with smart pointers: I've been using plain-old-pointers for a couple of years now in part because I can't find a solid explanation of when to use which types of smart pointers and how to go about using them. I'm tired of making excuses, so I'm just diving in. I think I understand what smart pointers are and why to use them, but I understand very little of the details. At the moment I'm totally lost in the endless Q&A about smart pointers.