shared_ptr로 객체를 관리하는 클래스 A가 있는데요.
함수 안에서 this로 넘길려고 할 때, enable_shared_from_this 상속 받아서 shared_from_this()를 사용합니다.
예를들어 이런식으로 사용되는데요.
void func(shared_ptr<A> a)
{
}
class A : public enable_shared_from_this<A>
{
void f(){
func(shared_from_this());
}
}
여기서 질문 몇가지,
1. 왜 굳이 귀찮게 enable_shared_from_this를 상속받고~ 생쇼를 해야하나요? 그냥 this 넘기면 shared_ptr로 받을 수 있게 해주면 안되나요?
2. 만약에 A 클래스가 다른 클래스를 상속 받는데 그 클래스도 enable_shared_from_this를 상속 받으면 어찌 처리해야 할까요?
예를들어 이렇게 말이죠.
class P : public enable_shared_from_this<P>
class A : public P, public enable_shared_from_this<A>
1. STL 디자인 설계 실패
2. 무작정하면 UB나 세그폴트 뜨니까 트릭 써서 우회해야 한다고 함.
https://stackoverflow.com/questions/16082785/use-of-enable-shared-from-this-with-multiple-inheritance
갠적으론
shared_ptr 안쓰고 unique_ptr 랑 생포인터만 씀
상위 클래스를 다운 캐스트 해서 사용한다라.. 한번 써봐야겠네요. 감사합니다.
오 된다. ㅋㅋ 시벌 C++