2013-04-18 8 views
15

BI के साथ enable_shared_from_this का उपयोग मेरे कोड में enable_shared_from_this का उपयोग कर रहा है, और मुझे यकीन नहीं है कि इसका उपयोग सही है या नहीं। यह सोचते हैं कि वे हमेशा डी के shared_ptr के माध्यम से कहा जा रहा हैएकाधिक विरासत

class A: public std::enable_shared_from_this<A> 
{ 
public: 
    void foo1() 
    { 
     auto ptr = shared_from_this(); 
    } 
}; 

class B:public std::enable_shared_from_this<B> 
{ 
public: 
    void foo2() 
    { 
     auto ptr = shared_from_this(); 
    } 
}; 

class C:public std::enable_shared_from_this<C> 
{ 
public: 
    void foo3() 
    { 
     auto ptr = shared_from_this(); 
    } 
}; 

class D: public A, public B, public C 
{ 
public: 
    void foo() 
    { 
     auto ptr = A::shared_from_this(); 
    } 
}; 

make_shared_from_this() सही के इन उपयोग कर रहे हैं, यह कोड है?

+0

मुझे नहीं लगता कि 'foo2' या' foo3' संकलन होगा ... – aschepler

+0

हां कि मतलब नहीं है, केवल वर्ग एक enable_shared_from_this <> विरासत –

+0

मुझे लगता है कि तुम क्या enable_shared_from_this पर एक नज़र रखना चाहिए कर देता है। इस [प्रश्न] का जवाब देखें (http://stackoverflow.com/questions/712279/what-is-the-usefulness-of-enable-shared-from- यह) –

उत्तर

21

वास्तव में आप इसे गलत कर रहे हैं। यदि आपके पास सरल विरासत है, तो बेस क्लास में केवल enable_shared_from this से प्राप्त करें, और व्युत्पन्न वर्ग इसे मुफ्त में प्राप्त करें। आप एकाधिक वंशानुक्रम है (जैसे यह लगता है) (बेशक आप परिणाम खिन्न करने की आवश्यकता होगी)

, आप चाल वर्णित here और भी here का उपयोग करना चाहिए:

/* Trick to allow multiple inheritance of objects 
* inheriting shared_from_this. 
* cf. https://stackoverflow.com/a/12793989/587407 
*/ 

/* First a common base class 
* of course, one should always virtually inherit from it. 
*/ 
class MultipleInheritableEnableSharedFromThis: public std::enable_shared_from_this<MultipleInheritableEnableSharedFromThis> 
{ 
public: 
    virtual ~MultipleInheritableEnableSharedFromThis() 
    {} 
}; 

template <class T> 
class inheritable_enable_shared_from_this : virtual public MultipleInheritableEnableSharedFromThis 
{ 
public: 
    std::shared_ptr<T> shared_from_this() { 
    return std::dynamic_pointer_cast<T>(MultipleInheritableEnableSharedFromThis::shared_from_this()); 
    } 
    /* Utility method to easily downcast. 
    * Useful when a child doesn't inherit directly from enable_shared_from_this 
    * but wants to use the feature. 
    */ 
    template <class Down> 
    std::shared_ptr<Down> downcasted_shared_from_this() { 
    return std::dynamic_pointer_cast<Down>(MultipleInheritableEnableSharedFromThis::shared_from_this()); 
    } 
}; 

फिर अपने कोड हो जाता है:

class A: public inheritable_enable_shared_from_this<A> 
{ 
public: 
    void foo1() 
    { 
     auto ptr = shared_from_this(); 
    } 
}; 

class B: public inheritable_enable_shared_from_this<B> 
{ 
public: 
    void foo2() 
    { 
     auto ptr = shared_from_this(); 
    } 
}; 

class C: public inheritable_enable_shared_from_this<C> 
{ 
public: 
    void foo3() 
    { 
     auto ptr = shared_from_this(); 
    } 
}; 

class D: public A, public B, public C 
{ 
public: 
    void foo() 
    { 
     auto ptr = A::downcasted_shared_from_this<D>(); 
    } 
}; 
+0

नोट: व्युत्पन्न कक्षाएं केवल वंचित सक्षम_श्रेड_फ्रॉम_थिस का उपयोग करने में सक्षम होंगी, यदि यह सार्वजनिक है, यानी। 'क्लास बेस क्लास: सार्वजनिक enable_shared_from_this '। – Andrew

संबंधित मुद्दे