template <class T> class enable_shared_from_this;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// enable_shared_from_this example
#include <iostream>
#include <memory>
struct C : std::enable_shared_from_this<C> { };
int main () {
std::shared_ptr<C> foo, bar;
foo = std::make_shared<C>();
bar = foo->shared_from_this();
if (!foo.owner_before(bar) && !bar.owner_before(foo))
std::cout << "foo and bar share ownership";
return 0;
}
foo and bar share ownership