In C++, an overloaded unary operator defined as a member function requires how…
2022
In C++, an overloaded unary operator defined as a member function requires how many variables to be passed as function argument?
- A.
Zero
- B.
One
- C.
Two
- D.
An unary operator cannot be overloaded
Attempted by 91 students.
Show answer & explanation
Correct answer: A
When a unary operator is overloaded as a member function in C++, it does not require any explicit parameters. This is because the object on which the operator is called is implicitly passed through the 'this' pointer. The function is invoked on an instance of the class, and the 'this' pointer refers to that instance. Therefore, no arguments are needed in the function definition. For example, if we overload the '++' operator as a member function, the function signature would be 'ClassName operator++();' without any parameters. This is different from overloading as a non-member function, where one parameter is required to specify the object. Hence, the correct answer is zero arguments.
हिन्दी उत्तर: C++ में, जब एक यूनरी ऑपरेटर को सदस्य फंक्शन के रूप में ओवरलोड किया जाता है, तो इसमें कोई व्यक्तिगत पैरामीटर आवश्यक नहीं होता है। ऐसा इसलिए होता है क्योंकि ऑपरेटर को कॉल किए जाने वाली वस्तु को 'this' पॉइंटर के माध्यम से निहित रूप से पास किया जाता है। फंक्शन एक क्लास के एक उदाहरण पर कॉल किया जाता है, और 'this' पॉइंटर उस उदाहरण को संदर्भित करता है। इसलिए, फंक्शन परिभाषा में कोई पैरामीटर आवश्यक नहीं होता है। उदाहरण के लिए, यदि हम '++' ऑपरेटर को सदस्य फंक्शन के रूप में ओवरलोड करते हैं, तो फंक्शन सिग्नेचर 'ClassName operator++();' होगा जिसमें कोई पैरामीटर नहीं होगा। यह एक गैर-सदस्य फंक्शन के रूप में ओवरलोड करने से अलग है, जहां वस्तु को निर्दिष्ट करने के लिए एक पैरामीटर आवश्यक होता है। इसलिए, सही उत्तर शून्य आर्गुमेंट है।