简体   繁体   English

通过引用将unique_ptr传递给函数

[英]passing unique_ptr to a function by reference

I noticed a behaviour in some library that I am compiling, which was slightly unexpected and wanted to clarify that. 我注意到我正在编译的某个库中有一个行为,这有点出乎意料,因此想澄清一下。

There is a class which has a method of the following form: 有一个类,该类具有以下形式的方法:

void notify(Frame & frame);

Now, there is a caller which uses a unique_ptr as follows: 现在,有一个调用者使用unique_ptr ,如下所示:

std::unique_ptr <Frame> localFrame (new Frame(rows, cols));

Now when it calls the method it does: 现在,当它调用该方法时,它将执行以下操作:

obj->notify(*localFrame);

So this relies on some implicit conversion of the underlying pointer to a reference. 因此,这依赖于基础指针到引用的某种隐式转换。

My question is that is this cross platform and expected behaviour? 我的问题是这个跨平台和预期的行为吗? Is there any use for me to do something something like: 对我来说有什么用吗?

obj->notify(*localFrame->get());

"Some implicit conversion" is std::unique_ptr::operator* which is quite a standard operator that returns a reference to the pointed-to object. “某些隐式转换”是std :: unique_ptr :: operator * ,它是一个标准的运算符,它返回对指向对象的引用。 You don't need to overcomplicate it. 您不需要太复杂。

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

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