I just knew std::enable_shared_from_this
form this link.
But after reading the code below, I don\'t know when to use it.
try {
G
There're some use case which you can't use the template std::shared_ptr
like opaque pointer.
In that case, it's useful to have this:
In some_file.cpp
struct A : std::enable_shared_from_this {};
extern "C" void f_c(A*);
extern "C" void f_cpp(A* a) {
std::shared_ptr shared_a = a->shared_from_this();
// work with operation requires shared_ptr
}
int main()
{
std::shared_ptr a = std::make_shared();
f_c(a.get());
}
In some_other.c
struct A;
void f_cpp(struct A* a);
void f_c(struct A* a) {
f_cpp(a);
}