#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的不同情况,以及它的正确和不正确的用法。
-
Good类:Good类继承自std::enable_shared_from_this<Good>,这意味着你可以在对象的成员函数中使用shared_from_this()函数来获取一个与当前对象相关联的std::shared_ptr。getptr函数返回一个shared_ptr,指向调用它的对象。testGood函数演示了正确的用法,其中创建了两个shared_ptr,它们共享同一个Good对象。
-
Best类:Best类也继承自std::enable_shared_from_this<Best>,但它的构造函数是私有的,因此不能直接创建Best对象。- 通过
static成员函数create来创建Best对象,该函数返回一个std::shared_ptr<Best>。 getptr函数与Good类似,用于获取与调用对象相关联的std::shared_ptr。testBest函数演示了Best类的正确用法。
-
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,除非你明确知道自己在做什么,否则可能导致不安全的内存管理。