std::enable_shared_from_this的用法

282 阅读2分钟
#include <iostream>
#include <memory>

class Good : public std::enable_shared_from_this<Good> {
public:
    std::shared_ptr<Good> getptr() {
        return shared_from_this();
    }
};

class Best : public std::enable_shared_from_this<Best> {
    struct Private {
    };

public:
    // Constructor is only usable by this class
    Best(Private) {}

    // Everyone else has to use this factory function
    // Hence all Best objects will be contained in shared_ptr
    static std::shared_ptr<Best> create() {
        return std::make_shared<Best>(Private());
    }

    std::shared_ptr<Best> getptr() {
        return shared_from_this();
    }
};


struct Bad {
    std::shared_ptr<Bad> getptr() {
        return std::shared_ptr<Bad>(this);
    }

    ~Bad() { std::cout << "Bad::~Bad() called\n"; }
};

void testGood() {
    // Good: the two shared_ptr's share the same object
    std::shared_ptr<Good> good0 = std::make_shared<Good>();
    std::shared_ptr<Good> good1 = good0->getptr();
    std::cout << "good1.use_count() = " << good1.use_count() << '\n';
}

void misuseGood() {
    // Bad: shared_from_this is called without having std::shared_ptr owning the caller
    try {
        Good not_so_good;
        std::shared_ptr<Good> gp1 = not_so_good.getptr();
    }
    catch (std::bad_weak_ptr &e) {
        // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17)
        std::cout << e.what() << '\n';
    }
}

void testBest() {
    // Best: Same but can't stack-allocate it:
    std::shared_ptr<Best> best0 = Best::create();
    std::shared_ptr<Best> best1 = best0->getptr();
    std::cout << "best1.use_count() = " << best1.use_count() << '\n';

    // Best stackBest; // <- Will not compile because Best::Best() is private.
}

void testBad() {
    // Bad, each shared_ptr thinks it's the only owner of the object
    std::shared_ptr<Bad> bad0 = std::make_shared<Bad>();
    std::shared_ptr<Bad> bad1 = bad0->getptr();
    std::cout << "bad1.use_count() = " << bad1.use_count() << '\n';
} // UB: double-delete of Bad

int main() {
    testGood();
    misuseGood();

    testBest();

    testBad();
}

这段代码演示了C++中使用std::enable_shared_from_this的不同情况,以及它的正确和不正确的用法。

  1. Good 类:

    • Good 类继承自 std::enable_shared_from_this<Good>,这意味着你可以在对象的成员函数中使用 shared_from_this() 函数来获取一个与当前对象相关联的 std::shared_ptr
    • getptr 函数返回一个 shared_ptr,指向调用它的对象。
    • testGood 函数演示了正确的用法,其中创建了两个 shared_ptr,它们共享同一个 Good 对象。
  2. Best 类:

    • Best 类也继承自 std::enable_shared_from_this<Best>,但它的构造函数是私有的,因此不能直接创建 Best 对象。
    • 通过 static 成员函数 create 来创建 Best 对象,该函数返回一个 std::shared_ptr<Best>
    • getptr 函数与 Good 类似,用于获取与调用对象相关联的 std::shared_ptr
    • testBest 函数演示了 Best 类的正确用法。
  3. Bad 类:

    • Bad 类没有继承 std::enable_shared_from_this
    • getptr 函数试图手动创建一个 shared_ptr,但这是不安全的,因为它没有与其他 shared_ptr 共享所有权。
    • testBad 函数演示了不正确的用法,其中两个 shared_ptr 对象都试图管理同一个 Bad 对象,这会导致未定义的行为和双重删除。

总结:

  • std::enable_shared_from_this 用于在类成员函数中获取与对象关联的 shared_ptr
  • 对于希望通过 shared_ptr 来管理对象的类,应该继承自 std::enable_shared_from_this,并使用 shared_from_this() 来获取 shared_ptr
  • 对于类的构造函数是私有的情况,可以通过静态工厂函数来创建对象,并确保所有对象都由 shared_ptr 来管理。
  • 不要手动创建 shared_ptr,除非你明确知道自己在做什么,否则可能导致不安全的内存管理。