user3525475
user3525475

Reputation: 1

unique_ptr: So I can't reference to a deleted function anymore

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

Answers (1)

ForEveR
ForEveR

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

Related Questions