私のクレイジーなAutoArrayの話に戻ります...(そこから重要なビットを引用します:
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
のコピー セマンティクスを持つクラスが可能になるのでしょうか?