【发布时间】:2021-10-28 01:05:54
【问题描述】:
例如:
std::unique_ptr<int> int_ptr = new int(10); // error: conversion from ‘int*’ to non-scalar type ‘std::unique_ptr’ requested
std::unique_ptr<int> pointer(new int(10)); // this work fine
【问题讨论】:
-
@armagedescu for shared_ptr 也会产生同样的错误。
-
@armagedescu
unique_ptr不是单身人士。您可以拥有 N 个具有 N 个唯一值的单独实例。这与单例允许的相反。 -
@armagedescu Singleton 表示全球唯一,不适用于
unique_ptr。您正在考虑适用于unique_ptr的“唯一所有权”。 -
@armagedescu - 该链接中的问题顺便说一句,
unique_ptr是一个单身人士,但这不是问题的重点,并且没有任何 cmets 或答案解决了该断言.充其量是弱权威。习惯上,“singleton”表示全局共享的单个对象。
标签: c++ c++11 unique-ptr