के लिए मूल वाक्यविन्यास प्रश्न मैं साझा_ptr में नया हूं।shared_ptr
//first question
shared_ptr<classA>ptr(new classA()); //works
shared_ptr<classA>ptr;
ptr = ?? //how could I create a new object to assign it to shared pointer?
//second question
shared_ptr<classA>ptr2; //could be tested for NULL from the if statement below
shared_ptr<classA> ptr3(new classA());
ptr3 = ?? //how could I assign NULL again to ptr3 so that the if statement below becomes true?
if(!ptr3) cout << "ptr equals null";
क्यों यहां बेहतर make_shared है के साथ का उपयोग करके? –
@ फ्रेड: जिम्मेदारी का पृथक्करण। 'make_shared <>' लाइब्रेरी को 'नया' संभालने की अनुमति देता है, इसलिए आपका कोड स्पष्ट रूप से असंतुलित 'नया' आमंत्रण प्रदर्शित नहीं करता है। लाइब्रेरी 'डिलीट' करती है, इसलिए इसे 'नया' भी करना चाहिए। –
@ फ्रेड नर्क: 'make_shared' संभावित रूप से एक आवंटन को समाप्त कर सकता है। – ybungalobill