08/06/17 21:25:32
>>102
何をしたいのか理解に苦しむけど、リファレンス返すのはどう?
#include <iostream>
#include <memory>
class NoCopy {
int val_;
NoCopy(const NoCopy&);
NoCopy& operator=(const NoCopy&);
public:
NoCopy(int val) : val_(val) {}
void dump() const { std::cerr << val_ << std::endl; }
};
class Hoge {
std::auto_ptr<NoCopy> nocopy_;
public:
Hoge() : nocopy_(0) {}
operator NoCopy&() {
nocopy_ = std::auto_ptr<NoCopy>(new NoCopy(1));
return *nocopy_.get();
}
};
int main() {
Hoge h;
NoCopy& nocopy = h;
nocopy.dump();
}