我一直在研究 SFML,想知道如何构建一个简单的 2D 游戏。我只是注意到这种行为,无法弄清楚发生了什么。让我困惑的示例代码:
struct Unique {};
class Shared {
public:
Shared() {
p = make_unique<Unique>();
}
unique_ptr<Unique> p;
};
void SharedCopyTest() {
Shared foo;
//Shared copy = foo; // Error: function "Shared::Shared(const Shared &)"
// (declared implicitly) cannot be referenced
// -- it is a deleted function
shared_ptr<Shared> sharedPtr = make_shared<Shared>();
shared_ptr<Shared> ptrCopy = sharedPtr; // No error
}
此时,&sharedPtr->p == &ptrCopy->p
; 但它怎么可能,如果p
是类型unique_ptr<T>
?