简体   繁体   English

将std :: unique_ptr转换为std :: unique_ptr转换为超类的正确方法是什么?

[英]What is the correct way to convert a std::unique_ptr to a std::unique_ptr to a superclass?

Suppose I have a class called foo which inherits from a class called bar . 假设我有一个名为foo的类,该类继承自名为bar的类。

I have a std::unique_ptr to an instance of foo and I want to pass it to a function that only takes std::unique_ptr<bar> . 我有一个std::unique_ptrfoo的实例,我想将其传递给只需要std::unique_ptr<bar>的函数。 How can I convert the pointer so it works in my function? 如何转换指针使其在函数中起作用?

You can convert a std::unique_ptr<foo> rvalue to an std::unique_ptr<bar> : 您可以将std::unique_ptr<foo>右值转换为std::unique_ptr<bar>

std::unique_ptr<foo> f(new foo);
std::unique_ptr<bar> b(std::move(f));

Obviously, the pointer will be owned by b and if b gets destroyed bar needs to have a virtual destructor. 显然,指针将归b ,如果b被破坏,则bar需要具有virtual析构函数。

Nothing special is required because of the inheritance. 由于继承,因此不需要任何特殊要求。 You need to use std::move to pass the unique_ptr to a function, but this is true even if the types match: 您需要使用std::move将unique_ptr传递给函数,但这是正确的,即使类型匹配:

#include <memory>

struct A {
};

struct B : A {
};

static void f(std::unique_ptr<A>)
{
}

int main(int,char**)
{
  std::unique_ptr<B> b_ptr(new B);
  f(std::move(b_ptr));
}

You may use this syntax: 您可以使用以下语法:

std::unique_ptr<parent> parentptr = std::unique_ptr<child>(childptr);

Or you may use std::move . 或者您可以使用std::move

The other option is to emit raw pointer, but you need to change a function: 另一个选择是发出原始指针,但是您需要更改一个函数:

void func(const parent* ptr)
{
  // actions...
}
func(*childptr);

Here is a good article about smart pointers and passing it to functions: http://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters . 这是一篇关于智能指针并将其传递给函数的好文章: http : //herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters

You can't, because it would violate the most basic unique_ptr rule: there has to be only one instance that holds a given pointer, and the unique_ptr has full ownership of it (when it goes out of scope, the pointee is deleted). 您不能这样做,因为它会违反最基本的unique_ptr规则:必须只有一个实例拥有给定的指针,并且unique_ptr拥有它的完全所有权(当它超出范围时,pointe被删除)。

unique_ptr<T> and unique_ptr<U> (where U : T ) aren't compatible, as you've seen. 如您所见, unique_ptr<T>unique_ptr<U> (其中U : T )不兼容。

For shared_ptr , for which you can have multiple instances, there is std::static_pointer_cast that behaves just like a static_cast , except that it accepts a shared_ptr and returns another one (and both point to the same object). 对于shared_ptr ,您可以为其具有多个实例,存在std::static_pointer_cast行为与static_cast相似,不同之处在于它接受shared_ptr并返回另一个(并且都指向同一个对象)。

If you absolutely need to use a unique_ptr , you'll have to create a function that first disowns your current unique_ptr and puts that pointer into a new one of the right type. 如果您绝对需要使用unique_ptr ,则必须创建一个函数,该函数首先放弃当前的unique_ptr然后将该指针放入正确类型的新指针中。 You might also need to do the opposite conversion after your function call. 函数调用之后,您可能还需要执行相反的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM