Creating shared_ptr only class with private destructor?

huangapple go评论70阅读模式
英文:

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&lt;SharedOnly&gt; 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&amp; other) = delete; // deleted copy constructor
    SharedOnly&amp; operator=(const SharedOnly&amp; other) = delete; // deleted copy assignment operator
    SharedOnly(SharedOnly&amp;&amp; other) = delete; // deleted move constructor
    SharedOnly&amp; operator=(SharedOnly&amp;&amp; other) = delete; // deleted move assignment operator

    static std::shared_ptr&lt;SharedOnly&gt; create()
    {
        auto objPtr = new SharedOnly;
        std::shared_ptr&lt;SharedOnly&gt; shared(objPtr, [](SharedOnly *ptr) {
            delete ptr;
        });
        return shared;
    }

private:
    SharedOnly() = default;
    ~SharedOnly() = default;

};

huangapple
  • 本文由 发表于 2023年4月13日 17:15:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定