英文:
Creating shared_ptr only class with private destructor?
问题
我能理解你的需求,你可以考虑使用 std::shared_ptr
的自定义删除器(custom deleter)来达到你的目的。这样,你就不需要 DestructorHelper
了。
英文:
I have a data class that is passed between threads. I would like this class to be a std::shared_ptr
only class that cannot be destroyed by calling its destructor from outside and so I want the destructor to be private as well. My current solution is
template<typename T>
struct DestructorHelper
{
static void Destroy(void* v) { delete static_cast<T*>(v); }
};
class SharedOnly
{
public:
SharedOnly(const SharedOnly& other) = delete; // deleted copy constructor
SharedOnly& operator=(const SharedOnly& other) = delete; // deleted copy assignment operator
SharedOnly(SharedOnly&& other) = delete; // deleted move constructor
SharedOnly& operator=(SharedOnly&& other) = delete; // deleted move assignment operator
static std::shared_ptr<SharedOnly> create()
{
auto objPtr = new SharedOnly;
auto desPtr = &DestructorHelper<SharedOnly>::Destroy;
std::shared_ptr<SharedOnly> shared(objPtr, desPtr);
return shared;
}
private:
SharedOnly() = default;
~SharedOnly() = default;
friend struct DestructorHelper<SharedOnly>;
};
Is there any way I could make this class without the DestructorHelper
? What I am looking for is an elegant solution to make a std::shared_ptr
only class so that it can be passed around to different threads and is only destroyed when the last shared pointer pointing to the object is destroyed.
答案1
得分: 5
你可以使用 lambda 表达式来解决在 create
函数内访问析构函数的问题:
static std::shared_ptr<SharedOnly> create()
{
return {new SharedOnly, [](SharedOnly *const s) { delete s; }};
}
英文:
You can use a lambda to resolve the destructor access from within create
:
static std::shared_ptr<SharedOnly> create()
{
return {new SharedOnly, [](SharedOnly *const s) { delete s; }};
}
答案2
得分: 3
您正在使用shared_ptr
,也就是说,您已经在使用C++11或更高版本,您可以将DestructorHelper
放入Lambda函数中。
class SharedOnly
{
public:
SharedOnly(const SharedOnly& other) = delete; // 删除了复制构造函数
SharedOnly& operator=(const SharedOnly& other) = delete; // 删除了复制赋值运算符
SharedOnly(SharedOnly&& other) = delete; // 删除了移动构造函数
SharedOnly& operator=(SharedOnly&& other) = delete; // 删除了移动赋值运算符
static std::shared_ptr<SharedOnly> create()
{
auto objPtr = new SharedOnly;
std::shared_ptr<SharedOnly> shared(objPtr, [](SharedOnly *ptr) {
delete ptr;
});
return shared;
}
private:
SharedOnly() = default;
~SharedOnly() = default;
};
英文:
As you're using share_ptr, that is to say, you're already using c++11 or above, you could put your DestructorHelper to the lambda function.
class SharedOnly
{
public:
SharedOnly(const SharedOnly& other) = delete; // deleted copy constructor
SharedOnly& operator=(const SharedOnly& other) = delete; // deleted copy assignment operator
SharedOnly(SharedOnly&& other) = delete; // deleted move constructor
SharedOnly& operator=(SharedOnly&& other) = delete; // deleted move assignment operator
static std::shared_ptr<SharedOnly> create()
{
auto objPtr = new SharedOnly;
std::shared_ptr<SharedOnly> shared(objPtr, [](SharedOnly *ptr) {
delete ptr;
});
return shared;
}
private:
SharedOnly() = default;
~SharedOnly() = default;
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论