参照カウントの目的で、以下のサンプルコードのようにstd :: tr1 :: shared_ptrを使用するのは安全で正しいですか?(これは単なる特定のサンプルであり、クラスにはFILE *の代わりに他のもの(void *)を含めることができます)
class File
{
public:
File(const char* path, const char* mode) :
_refcount(new int(0))
{
this->_file = fopen(path, mode);
}
~File()
{
if (this->_refcount.unique())
{
if (this->_file != NULL)
{
fclose(this->_file);
}
}
}
int write(void* buff, size_t size)
{
fwrite(buff, size, 1, this->_file);
}
private:
FILE* _file;
std::tr1::shared_ptr<int> _refcount;
};