C++/C++ 11 14 17

shared_from_this(), weak_from_this()

산과 나무 2019. 5. 31. 09:02

C++ 17에서 weak_from_this() 항목이 추가되었다.

 

예제) s

class Foo : public enable_shared_from_this<Foo>
{
public:
   shared_ptr<Foo> getPointer() 

   {
       return shared_from_this();
   }

  weak_ptr<Foo> GetWeakPointer()

  {  

        return weak_from_this();  

  }
};
int main()
{
   auto ptr1 = make_shared();
   auto ptr2 = ptr1->getPointer();
}