असाइनमेंट ऑपरेटर (सी ++)
C++ प्रोग्रामिंग भाषा में, असाइनमेंट ऑपरेटर, =
, असाइनमेंट (कंप्यूटर विज्ञान) के लिए इस्तेमाल किया जाने वाला ऑपरेटर (प्रोग्रामिंग) है। सी ++ में अधिकांश अन्य ऑपरेटरों की तरह, यह ऑपरेटर ओवरलोडिंग हो सकता है।
कॉपी असाइनमेंट ऑपरेटर, जिसे अक्सर असाइनमेंट ऑपरेटर कहा जाता है, असाइनमेंट ऑपरेटर का एक विशेष मामला है जहां स्रोत (दाएं हाथ की ओर) और गंतव्य (बाएं हाथ की ओर) एक ही वर्ग प्रकार के होते हैं। यह विशेष सदस्य कार्यों में से एक है, जिसका अर्थ है कि यदि प्रोग्रामर एक घोषित नहीं करता है तो इसका एक डिफ़ॉल्ट संस्करण संकलक द्वारा स्वचालित रूप से उत्पन्न होता है। डिफ़ॉल्ट संस्करण एक सदस्यवार कॉपी करता है, जहां प्रत्येक सदस्य को अपने स्वयं के कॉपी असाइनमेंट ऑपरेटर (जो प्रोग्रामर-घोषित या कंपाइलर-जेनरेट भी हो सकता है) द्वारा कॉपी किया जाता है।
कॉपी असाइनमेंट ऑपरेटर कॉपी कंस्ट्रक्टर से भिन्न होता है, जिसमें उसे असाइनमेंट के लक्ष्य के डेटा सदस्यों को साफ करना चाहिए (और सही ढंग से स्व-असाइनमेंट को संभालना चाहिए) जबकि कॉपी कंस्ट्रक्टर गैर-प्रारंभिक डेटा सदस्यों को मान प्रदान करता है।[1] उदाहरण के लिए:
My_Array first; // initialization by default constructor
My_Array second(first); // initialization by copy constructor
My_Array third = first; // Also initialization by copy constructor
second = third; // assignment by copy assignment operator
अतिभारित असाइनमेंट ऑपरेटर का वापसी मूल्य
भाषा एक अतिभारित असाइनमेंट ऑपरेटर को मनमाना रिटर्न प्रकार (सहित void
). हालांकि, ऑपरेटर को आमतौर पर असाइनी को संदर्भ वापस करने के लिए परिभाषित किया जाता है। यह बिल्ट-इन प्रकार (असाइनमेंट (कंप्यूटर विज्ञान)#असाइनमेंट का मूल्य) के लिए असाइनमेंट ऑपरेटर के व्यवहार के अनुरूप है और ऑपरेटर मंगलाचरण को एक अभिव्यक्ति के रूप में उपयोग करने की अनुमति देता है, उदाहरण के लिए कंट्रोल स्टेटमेंट या असाइनमेंट (कंप्यूटर साइंस)# में जंजीर असाइनमेंट। साथ ही, सी ++ मानक पुस्तकालय को कुछ उपयोगकर्ता द्वारा प्रदान किए गए प्रकारों के लिए इस व्यवहार की आवश्यकता होती है।[2]
ओवरलोडिंग कॉपी असाइनमेंट ऑपरेटर
जब वस्तुओं की गहरी प्रतिलिपि बनानी होती है, तो अपवाद सुरक्षा को ध्यान में रखा जाना चाहिए। इसे प्राप्त करने का एक तरीका जब संसाधन विलोपन कभी विफल नहीं होता है:
- नए संसाधन प्राप्त करें
- पुराने संसाधनों को जारी करें
- नए संसाधनों के हैंडल को ऑब्जेक्ट में असाइन करें
class My_Array{
int* array;
int count;
public:
My_Array& operator=(const My_Array& other)
{
if (this != &other) { // protect against invalid self-assignment
// 1: allocate new memory and copy the elements
int* new_array = new int[other.count];
std::copy(other.array, other.array + other.count, new_array);
// 2: deallocate old memory
delete[] array;
// 3: assign the new memory to the object
array = new_array;
count = other.count;
}
// by convention, always return *this
return *this;
}
// ...
};
हालाँकि, यदि सभी सदस्य उप-विषयों के लिए नो-फेल (अपवाद गारंटी | नो-थ्रो) स्वैप (कंप्यूटर विज्ञान) फ़ंक्शन उपलब्ध है और क्लास एक कॉपी कंस्ट्रक्टर और विध्वंसक (कंप्यूटर विज्ञान) प्रदान करता है (जो इसे नियम के अनुसार करना चाहिए) तीन में से (C++ प्रोग्रामिंग), कॉपी असाइनमेंट को लागू करने का सबसे सीधा तरीका इस प्रकार है:[3]
public:
void swap(My_Array & other) // the swap member function (should never fail!)
{
// swap all the members (and base subobject, if applicable) with other
using std::swap; // because of ADL the compiler will use
// custom swap for members if it exists
// falling back to std::swap
swap(array, other.array);
swap(count, other.count);
}
My_Array & operator = (My_Array other) // note: argument passed by value!
{
// swap this with other
swap(other);
// by convention, always return *this
return *this;
// other is destroyed, releasing the memory
}
विभिन्न वर्गों के बीच असाइनमेंट
सी ++ निहित कॉपी कन्स्ट्रक्टर और असाइनमेंट ऑपरेटर दोनों के माध्यम से विभिन्न वर्गों के बीच असाइनमेंट का समर्थन करता है, यदि गंतव्य उदाहरण वर्ग स्रोत उदाहरण वर्ग का पूर्वज है:
class Ancestor {
public:
int a;
};
class Descendant : public Ancestor {
public:
int b;
};
int main()
{
Descendant d;
Ancestor a(d);
Ancestor b(d);
a = d;
}
पूर्वज से वंशज वस्तुओं में प्रतिलिपि बनाने की अनुमति नहीं है, जो वंश के क्षेत्रों को प्रारंभ नहीं कर सकता है।
यह भी देखें
- ऑपरेटर ओवरलोडिंग
- असाइनमेंट ऑपरेटर ले जाएँ
- तीन का नियम (C++ प्रोग्रामिंग)
- सी और सी++ में ऑपरेटर्स
संदर्भ
- ↑ Stroustrup, Bjarne (2000). The C++ Programming Language (3 ed.). Addison-Wesley. p. 244. ISBN 978-0-201-70073-2.
- ↑ Working Draft, Standard for Programming Language C++, Section 17.6.3.1, Table 23; http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
- ↑ Sutter, H.; Alexandrescu, A. (October 2004), C++ Coding Standards, Addison-Wesley, ISBN 0-321-11358-6
बाहरी संबंध
- The Anatomy of the Assignment Operator, by Richard Gillam
- Collapse templates
- Navigational boxes
- Navigational boxes without horizontal lists
- Sidebars with styles needing conversion
- Templates generating microformats
- Templates that are not mobile friendly
- Wikipedia metatemplates
- सी ++
- ऑपरेटर्स (प्रोग्रामिंग)
- असाइनमेंट संचालन
- Machine Translated Page
- Created On 11/06/2023