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>