回到我疯狂的AutoArray thingy ...(从那里引用重要的部分:
class AutoArray
{
void * buffer;
public:
//Creates a new empty AutoArray
AutoArray();
//std::auto_ptr copy semantics
AutoArray(AutoArray&); //Note it can't be const because the "other" reference
//is null'd on copy...
AutoArray& operator=(AutoArray);
~AutoArray();
//Nothrow swap
// Note: At the moment this method is not thread safe.
void Swap(AutoArray&);
};
)
无论如何,尝试实现复制构造函数。有一段客户端代码(尚未提交到 bitbucket,因为它不会构建)如下所示:
AutoArray NtQuerySystemInformation(...) { ... };
AutoArray systemInfoBuffer = NtQuerySystemInformation(...);
这失败了,因为复制构造函数将非const
引用作为参数....但是我看不出如何修改复制构造函数以获取const
引用,因为AutoArray
分配中使用的源已被修改(因此不会不是const
)。当然,您不能修改内容以使用按值传递,因为它是复制构造函数,那将是一个无限循环!
如果我使用auto_ptr
,这将是有效的:
std::auto_ptr NtQuerySystemInformation(...) { ... };
std::auto_ptr systemInfoBuffer = NtQuerySystemInformation(...);
那么,具有auto_ptr
' 的复制语义的类如何成为可能呢?