मेरे प्रश्न किसी ऑब्जेक्ट को वापस करने के तरीके से संबंधित हैं जिसमें प्रतिलिपि बनाने वाला नहीं है। उदाहरण के तौर पर चलो कल्पना करें कि मेरे पास कुछ bigResource
है जो ढेर में बैठता है, और मान लें कि मैं unique_ptr
का उपयोग करके इसका ट्रैक रखता हूं। अब मान लीजिए कि मैं इस संसाधन का स्वामित्व कैटरपिलर को देता हूं। तब मेरे पास CaterpillarWithBigResource
है। अब कुछ बिंदु पर, इस CaterpillarWithBigResource
एक ButterflyWithBigResource
में बदलने के लिए है, तो Caterpillar
वस्तु Butterfly
वस्तु को स्वामित्व स्थानांतरित करना होगा जा रहा है।किसी प्रतिलिपि के साथ किसी ऑब्जेक्ट को वापस कैसे करें
#include <cstdlib>
#include <iostream>
#include <memory>
class ButterflyWithBigResource {
public:
// If I uncomment just this line, I get an error
// ButterflyWithBigResource(const ButterflyWithBigResource& other) = default;
// If I uncomment just this line, I get an error
// ButterflyWithBigResource(const ButterflyWithBigResource& other) = delete;
// With both above lines commented out, I get no errors, and the program runs fine.
ButterflyWithBigResource(std::unique_ptr<int>&& bigResource) :
bigResource(std::move(bigResource)) {
}
const int& getResource() {
return *bigResource;
}
private:
std::unique_ptr<int> bigResource;
};
class CaterpillarWithBigResource {
public:
CaterpillarWithBigResource(int bigResource) :
bigResource(new int(bigResource)) {
}
ButterflyWithBigResource toButterfly() && {
return ButterflyWithBigResource(std::move(bigResource));
}
private:
std::unique_ptr<int> bigResource;
};
/*
*
*/
int main(int argc, char** argv) {
CaterpillarWithBigResource caterpillarWithBigResource(5);
ButterflyWithBigResource butterflyWithBigResource(std::move(caterpillarWithBigResource).toButterfly());
std::cout << butterflyWithBigResource.getResource() << std::endl;
return 0;
}
सूचना है कि न तो Caterpillar
और न ही Butterfly
डिफ़ॉल्ट प्रतिलिपि कंस्ट्रक्टर्स है, क्योंकि वे एक एक unique_ptr
है:
मैं इस स्थिति मॉडल करने के लिए निम्नलिखित कोड लिखा है। हालांकि, मैं यह समस्या होने की उम्मीद नहीं करता, इसलिए केवल रचनाकारों को आवश्यक होना चाहिए। आखिरकार, मैं केवल Caterpillar
से Butterfly
पर स्वामित्व स्थानांतरित कर रहा हूं।
वास्तव में जब मैं g++ -c -g -std=c++11 -MMD -MP -MF
g++
संस्करण 4.8.2
का उपयोग कर प्रोग्राम संकलित करता हूं तो यह ठीक काम करता है।
लेकिन अब अजीब बात यह है कि अगर मैं लाइन ButterflyWithBigResource(const ButterflyWithBigResource& other) = delete;
जोड़कर संकलक कि Butterfly
की कॉपी निर्माता हटा दी जाती है याद दिलाना, कार्यक्रम अब संकलित, और संकलक शिकायत है कि प्रति निर्माता हटा दी जाती है, ताकि मैं toButterfly
विधि में Butterfly
वापस न करें।
मैं तो है कि सब कुछ के बजाय लाइन ButterflyWithBigResource(const ButterflyWithBigResource& other) = default;
होने से ठीक है यह बताने के लिए कोशिश करते हैं, तो मैं फिर से एक ही त्रुटि मिलती है।
क्या मैं क्या करना चाहते toButterfly
में स्थानांतरित करने की की वापसी पता, और फिर बाद में Butterfly
के तर्क के रूप में उपयोग किया जाता है 'जब main()
में butterflyWithBigResource
निर्माण रों चाल निर्माता Butterfly
toButterfly
विधि में निर्माण के लिए है। क्या ऐसा करने का कोई तरीका है?
Btw, कोई कॉपी-निर्माता के साथ एक वस्तु वापस जाने के लिए, आप copy- का उपयोग करेंगे सूची-प्रारंभिकरण: 'वापसी {std :: move (bigRes हमारा)} ' – 0x499602D2