jeudi 17 septembre 2020

Move unique_ptr ownership from one class to another

I want to create a unique pointer in one class, class A, then pass on the ownership to another class, class B. Am I ok doing such a thing?

The code below gives me error in getC: error: call to deleted constructor of 'std::unique_ptr<C>

What am I doing wrong?

class A {
...
  void func(shared_ptr<B> Bptr) {
   A_pass = make_unique<C>();
   Bptr->setPass(move(A_pass));
  }
  unique_ptr<C> getC()
  {
    return A_pass;
  }
 unique_ptr<C> A_pass;
};

class B {
...
 void setPass(unique_ptr<C> pass_ptr){
  B_pass = move(pass_ptr);
 }
 unique_ptr<C> B_pass;
}

edit: update the question

Aucun commentaire:

Enregistrer un commentaire