누군가 아래 나와 같이 사용하면이 두 가지 방법의 차이점을 말해 줄 수 있습니까?부스트 :: shared_ptr 반환 및 반환 된 원시 포인터에서 boost :: shared_ptr 생성 사이의 차이점은 무엇입니까?
CreateBoostContainer를 사용하면 코드가 잘 작동합니다. 그러나 CreateContainer를 사용하면 ContainerC에서 shared_from_this를 사용하려고 할 때 나중에 Foo 함수의 코드에서 boost :: bad_weak_ptr 예외가 발생합니다. 하나의 스레드 만 사용하고 있습니다.
감사합니다.
사용법 :
SceneElementNodeC* poNode(new SceneElementNodeC(CreateBoostContainer()));
SceneElementNodeC* poNode(new SceneElementNodeC(boost::shared_ptr<SceneElementI>(CreateContainer())));
정의 :
boost::shared_ptr<SceneElementI> LoaderC::CreateBoostContainer() const
{
return boost::shared_ptr<SceneElementI>(new ContainerC());
}
SceneElementI* LoaderC::CreateContainer() const
{
return new ContainerC();
}
SceneElementNodeC :
class SceneElementNodeC
{
SceneElementNodeC(boost::shared_ptr<SceneElementI> spSceneElement)
: m_spSceneElement(spSceneElement)
{};
}
ContainerC :
class ContainerC : public SceneElementI, public boost::enable_shared_from_this<ContainerC>
{
ContainerC()
{};
void Foo(VisitorI* poVisitor)
{
poVisitor->Visit(shared_from_this());
};
}
먼저 차이점을 상세히 설명해 주셔서 감사 드리며 정확히 이해하지 못했습니다. – schluchc
CreateBoostContainer의 이름에 대해서는 물론 끔찍하지만이 질문에서 두 함수를 구별하는 것이 었습니다. 두 번째 요점에 대해서, 당신 말이 맞습니다. 그러나 나는 그런 경우에 여전히 질문을 향상시킬 수있는 기회가 있기를 바랍니다. – schluchc