C++怎么获取widget的所有权
本篇内容主要讲解"C++怎么获取widget的所有权",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C++怎么获取widget的所有权"吧!
R.32: 通过unique_ptr
Reason(原因)
以这种方式使用unique_ptr可以从文档和实现两个方面强制进行所有权的移交。
Example(示例)
void sink(unique_ptr); // takes ownership of the widget
void uses(widget*); // just uses the widget
Example, bad(反面示例)
void thinko(const unique_ptr&); // usually not what you want
Enforcement(实施建议)
(Simple) Warn if a function takes a Unique_pointer
parameter by lvalue reference and does not either assign to it or call reset() on it on at least one code path. Suggest taking a T* or T& instead. (简单)如果一个函数通过左值引用方式使用了Unique_pointer
参数,却没有对它赋值,或者在至少一条代码路径上调用了reset,报警。建议使用T*或者T&代替。 (Simple) ((Foundation)) Warn if a function takes a Unique_pointer
parameter by reference to const. Suggest taking a const T* or const T& instead. (简单)((基本)) 如果一个函数以常量引用方式使用Unique_pointer
参数,建议使用const T*或者const T&代替。
到此,相信大家对"C++怎么获取widget的所有权"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!