Home > Mobile >  Transfer ownership of a derived class unique_ptr to its abstract base class unique_ptr
Transfer ownership of a derived class unique_ptr to its abstract base class unique_ptr

Time:12-24

I want to transfer ownership of a derived class unique_ptr to its abstract base class unique_ptr in a polymorphic situation. How to go about?

class Fruit {
public:
    virtual void print() = 0;
};

class Apple: public Fruit {
public:
    string name;
    virtual  void print()  { cout << " Apple name is " << name << endl; }
    Apple(string name): name(name) {}
};


int main()
{
    unique_ptr<Apple> apple = make_unique<Apple>("Rose");
    unique_ptr<Fruit> fruit = dynamic_cast<unique_ptr<Fruit>>(apple); // don't work
    // want to transfer ownership of apple to fruit

    unique_ptr<Apple> new_apple = dynamic_cast<unique_ptr<Apple>>(fruit); // get back the ownership to the new apple
    return 0;
}

CodePudding user response:

To transfer ownership of a derived class managed by a derived class unique_ptr to a base class unique_ptr, you can use move semantics.

    std::unique_ptr<Derived> foo = std::make_unique<Derived>();
    std::unique_ptr<Base> bar = std::move(foo);

To return ownership to a derived unique_ptr, you need to get a little messier:

    std::unique_ptr<Derived> foo = std::make_unique<Derived>();
    std::unique_ptr<Base> bar = std::move(foo);

    std::unique_ptr<Derived> biz(static_cast<Derived*>(bar.release()));

If you're unsure of the actual type of the pointer, then a dynamic cast can be used to check that it's correct. Note that we use std::unique_ptr<Base>::get() in the conditional, since we're not sure we want to release ownership yet. If this passes, then we can call std::unique_ptr<Base>::release().

    std::unique_ptr<Derived> foo = std::make_unique<Derived>();
    std::unique_ptr<Base> bar = std::move(foo);

    // dynamic cast if we're unsure that it is castable
    if (dynamic_cast<Derived*>(bar.get())) {
        foo.reset(static_cast<Derived*>(bar.release()));
    }

see it in action

CodePudding user response:

try move assignment

int main()
{
    unique_ptr<Apple> apple = make_unique<Apple>("Rose");
    unique_ptr<Fruit> fruit = std::move(apple);
    fruit->print();
    // want to transfer ownership of apple to fruit
    return 0;
}
  • Related