boost-smart-ptr

How to use boost::smart_ptr in polymorphism?

点点圈 提交于 2019-11-29 14:42:03
问题 Boost smart pointers can be used with polymorphism, but how do you cast the subclass back to the pointer? using namespace boost; // ... shared_ptr<SuperClass> a_ptr(new SubClass); // ... shared_ptr<SubClass> b_ptr = (shared_ptr<SubClass>)a_ptr; // Doesn't compile The last line doesn't compile and gives error C2440: 'type cast' : cannot convert from 'boost::shared_ptr <T> ' to 'boost::shared_ptr <T> ' 回答1: You need to use static_pointer_cast : struct B { virtual ~B() { } }; struct D : B { };

Is boost shared_ptr <XXX> thread safe?

十年热恋 提交于 2019-11-27 06:58:30
I have a question about boost::shared_ptr<T> . There are lots of thread. using namespace boost; class CResource { // xxxxxx } class CResourceBase { public: void SetResource(shared_ptr<CResource> res) { m_Res = res; } shared_ptr<CResource> GetResource() { return m_Res; } private: shared_ptr<CResource> m_Res; } CResourceBase base; //---------------------------------------------- // Thread_A: while (true) { //... shared_ptr<CResource> nowResource = base.GetResource(); nowResource.doSomeThing(); //... } // Thread_B: shared_ptr<CResource> nowResource; base.SetResource(nowResource); //... Q1 If

Is boost shared_ptr <XXX> thread safe?

寵の児 提交于 2019-11-26 12:59:06
问题 I have a question about boost::shared_ptr<T> . There are lots of thread. using namespace boost; class CResource { // xxxxxx } class CResourceBase { public: void SetResource(shared_ptr<CResource> res) { m_Res = res; } shared_ptr<CResource> GetResource() { return m_Res; } private: shared_ptr<CResource> m_Res; } CResourceBase base; //---------------------------------------------- // Thread_A: while (true) { //... shared_ptr<CResource> nowResource = base.GetResource(); nowResource.doSomeThing();