Reputation: 1
I have 2 classes and I would like to return a reference to a private member object.
class BB{};
class B
{
std::unique_ptr<BB> b;
public:
const std::unique_ptr<BB>& getBB(){return b;}
};
int main()
{
B b;
std::unique_ptr<BB> x=b.getBB();
}
Undoubtedly, the error occurs in main at x=b.GetBB()
that says
...can't be referenced. It's a deleted function.
Upvotes: 0
Views: 170
Reputation: 55887
You are trying to copy initialize unique_ptr
, that is not allowed, since unique_ptr
has deleted copy constructor. Try
const std::unique_ptr<BB>& x = b.getBB();
Upvotes: 3