Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

c++ - unique_ptr to a derived class as an argument to a function that takes a unique_ptr to a base class

I'm trying to use a unique_ptr to derived class in a function that takes a unique_ptr to a base class. Something like:

class Base {};

class Derived : public Base {};

void f(unique_ptr<Base> const &base) {}

…

unique_ptr<Derived> derived = unique_ptr<Derived>(new Derived);
f(derived);

If I understand this answer correctly, this code should work, but it causes the following compile errors:

error C2664: 'f' : cannot convert parameter 1 from 'std::unique_ptr<_Ty>' to 'const std::unique_ptr<_Ty> &'

IntelliSense: no suitable user-defined conversion from "std::unique_ptr<Derived, std::default_delete<Derived>>" to "const std::unique_ptr<Base, std::default_delete<Base>>" exists

If I change f to take unique_ptr<Derived> const &derived, it works fine, but that's not what I want.

Am I doing something wrong? What can I do to work around this?

I'm using Visual Studio 2012.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have three options:

  1. Give up ownership. This will leave your local variable without access to the dynamic object after the function call; the object has been transferred to the callee:

    f(std::move(derived));
    
  2. Change the signature of f:

    void f(std::unique_ptr<Derived> const &);
    
  3. Change the type of your variable:

    std::unique_ptr<base> derived = std::unique_ptr<Derived>(new Derived);
    

    Or of course just:

    std::unique_ptr<base> derived(new Derived);
    

    Or even:

    std::unique_ptr<base> derived = std::make_unique<Derived>();
    
  4. Update: Or, as recommended in the comments, don't transfer ownership at all:

    void f(Base & b);
    
    f(*derived);
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...