PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Polymorphism: Compile-Time (Function and Operator Overloading) (कंपाइल-टाइम पॉलीमॉर्फिज्म: फंक्शन ओवरलोडिंग और ऑपरेटर ओवरलोडिंग) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Compile-time polymorphism (static or early binding) in C++ resolves function and operator calls during compilation via function overloading, operator overloading, and templates. Function overloading uses name mangling based on parameter signatures, while operator overloading customizes C++ operators for user-defined types without altering precedence or arity.
अध्याय सार: C++ में कंपाइल-टाइम पॉलीमॉर्फिज्म (स्टैटिक या अर्ली बाइंडिंग) कंपाइलेशन के समय ही फंक्शन ओवरलोडिंग, ऑपरेटर ओवरलोडिंग और टेम्प्लेट्स के माध्यम से सही कॉल तय कर लेता है। फंक्शन ओवरलोडिंग पैरामीटर सिग्नेचर के आधार पर नेम मैंगलिंग का उपयोग करती है, जबकि ऑपरेटर ओवरलोडिंग यूजर-डिफाइंड टाइप्स के लिए ऑपरेटर्स को विशेष अर्थ देती है।
Polymorphism: Compile-Time (Function and Operator Overloading): Detailed Notes & Architecture (कंपाइल-टाइम पॉलीमॉर्फिज्म: फंक्शन ओवरलोडिंग और ऑपरेटर ओवरलोडिंग)
1. Function Overloading and Name Mangling (फंक्शन ओवरलोडिंग और नेम मैंगलिंग (Name Mangling))
[EN] Function overloading permits multiple functions in the same scope to share an identical name provided their parameter lists differ in count, parameter types, or parameter sequence. Return types alone are never part of the C++ overload signature, so two functions differing solely in return type fail to compile. Internally, the C++ compiler implements overloading using name mangling (name decoration), encoding parameter types into distinct linker symbols (e.g., `_Z3addii` vs `_Z3adddd`).
[HI] फंक्शन ओवरलोडिंग में एक ही स्कोप में एक समान नाम के कई फंक्शन बनाए जा सकते हैं, बशर्ते उनके पैरामीटर की संख्या, प्रकार या क्रम भिन्न हों। केवल रिटर्न टाइप अलग होने पर फंक्शन ओवरलोड नहीं हो सकते और कंपाइलर एरर देता है। आंतरिक रूप से C++ कंपाइलर नेम मैंगलिंग (Name Mangling) का उपयोग करके पैरामीटर टाइप्स को लिंकर के लिए अलग-अलग यूनिक प्रतीकों में बदल देता है।
2. Overload Resolution and Ambiguity Cases (ओवरलोड रेजोल्यूशन और अस्पष्टता के कारण)
[EN] During overload resolution, the compiler ranks candidates from exact match, integral/floating promotion (`char -> int`, `float -> double`), standard conversion (`int -> double`), to user-defined conversions. Ambiguity errors occur when two candidates match equally well, such as calling `fn(3.5)` against `fn(float)` and `fn(long)` (both require standard conversions from `double`). Default arguments (`void f(int x)` and `void f(int x, int y = 0)`) and pass-by-value vs pass-by-reference (`f(int)` and `f(int&)`) can also trigger compile-time ambiguity on `f(5)` or `f(a)`.
[HI] ओवरलोड रेजोल्यूशन में कंपाइलर पहले एक्जैक्ट मैच, फिर प्रमोशन (`char -> int`, `float -> double`), फिर स्टैंडर्ड कन्वर्जन (`int -> double`) और अंत में यूजर-डिफाइंड कन्वर्जन देखता है। यदि दो विकल्प समान स्तर पर फिट बैठें—जैसे `fn(float)` और `fn(long)` के बीच `fn(3.5)` (`double`) कॉल करना—तो एम्बियुइटी एरर आता है। डिफ़ॉल्ट आर्गुमेंट्स (`f(int)` व `f(int, int=0)`) और वैल्यू बनाम रेफरेंस पैरामीटर्स भी कई बार कॉल के समय अस्पष्टता उत्पन्न कर देते हैं।
3. Operator Overloading Rules and Restrictions (ऑपरेटर ओवरलोडिंग के नियम और सीमाएँ)
[EN] Operator overloading defines a function named `operator@` for user-defined classes, structs, or enums; at least one operand must be a user-defined type. Five C++ operators cannot be overloaded: scope resolution (`::`), member access (`.`), pointer-to-member access (`.*`), ternary conditional (`?:`), and `sizeof` (alongside compile-time operators like `typeid` and `alignof`). Overloading can never invent new operator tokens (like `**`), nor alter an operator's built-in precedence, associativity, or operand count (arity).
[HI] ऑपरेटर ओवरलोडिंग यूजर-डिफाइंड क्लास या स्ट्रक्ट के लिए `operator@` फंक्शन परिभाषित करती है, जिसमें कम से कम एक ऑपरेंड यूजर-डिफाइंड टाइप होना अनिवार्य है। पाँच मुख्य C++ ऑपरेटर ओवरलोड नहीं हो सकते: `::` (स्कोप रेजोल्यूशन), `.` (डॉट), `.*` (मेंबर पॉइंटर), `?:` (टर्नरी) और `sizeof`। ओवरलोडिंग से न तो कोई नया ऑपरेटर चिह्न (`**`) बनाया जा सकता है और न ही ऑपरेटर की प्रिसिडेंस, एसोसिएटिविटी या ऑपरेंड संख्या बदली जा सकती है।
4. Member vs Friend Operator Functions (मेंबर बनाम फ्रेंड (Friend) ऑपरेटर फंक्शन)
[EN] Four operators MUST be overloaded as non-static member functions and cannot be non-member friends: assignment (`=`), function call (`()`), subscript (`[]`), and member access (`->`). For binary operators written as member functions, the left operand is implicitly `*this` and only 1 explicit parameter is declared; as a `friend` non-member, 2 parameters are required. Friend non-member overloading is essential when the left operand is a primitive or external stream type (e.g., `std::cout << obj` or `10 + obj`), while post-increment `operator++(int)` uses a dummy `int` parameter to distinguish itself from pre-increment `operator++()`.
[HI] चार ऑपरेटर—असाइनमेंट (`=`), फंक्शन कॉल (`()`), सबस्क्रिप्ट (`[]`) और एरो (`->`)—अनिवार्य रूप से नॉन-स्टैटिक मेंबर फंक्शन के रूप में ही ओवरलोड होते हैं। मेंबर फंक्शन में बाइनरी ऑपरेटर 1 एक्सप्लिसिट पैरामीटर (बायाँ ऑपरेंड `*this`) लेता है, जबकि `friend` नॉन-मेंबर फंक्शन में 2 पैरामीटर होते हैं। जब बायाँ ऑपरेंड बिल्ट-इन टाइप या स्ट्रीम हो (जैसे `cout << obj` या `5 + obj`), तब `friend` फंक्शन जरूरी होता है; पोस्ट-इंक्रीमेंट `operator++(int)` डमी `int` लेता है।
Syntax, Formula & Technical Example (Polymorphism: Compile-Time (Function and Operator Overloading))
#include <iostream>
class Complex {
int r, i;
public:
Complex(int r = 0, int i = 0) : r(r), i(i) {}
Complex operator+(const Complex& o) const {
return Complex(r + o.r, i + o.i);
}
void show() const { std::cout << r << "+" << i << "i\n"; }
};
Polymorphism: Compile-Time (Function and Operator Overloading) Quick Reference & Comparison Table
| Operator / Feature | Overload Form | Key C++ Rule |
|---|---|---|
| `::`, `.`, `.*`, `?:`, `sizeof` | Not Overloadable | Strictly forbidden by C++ grammar |
| `=`, `()`, `[]`, `->` | Member Function Only | Cannot be overloaded as friend functions |
| `<<`, `>>` (Stream I/O) | Friend Non-Member | Left operand is std::ostream or std::istream |
| `++obj` vs `obj++` | 0 args vs dummy int | Post-increment takes dummy (int) parameter |
Polymorphism: Compile-Time (Function and Operator Overloading) Top 20 Bilingual MCQs with Answers
Q1. Which technique does the C++ compiler use internally to support function overloading at link time?
(लिंक टाइम पर फंक्शन ओवरलोडिंग को सपोर्ट करने के लिए C++ कंपाइलर आंतरिक रूप से किस तकनीक का उपयोग करता है?)
(A) Dynamic type casting
(B) Name mangling (name decoration)
(C) Garbage collection
(D) Virtual table (vtable) lookup
Correct Answer: (B)
[EN] The compiler encodes function names and parameter types into unique mangled symbol names for the linker.
[HI] कंपाइलर फंक्शन के नाम और उसके पैरामीटर टाइप्स को मिलाकर लिंकर के लिए यूनिक नाम बनाता है, जिसे नेम मैंगलिंग कहते हैं।
Q2. Can two functions in the same C++ scope be overloaded if they have identical parameter lists and differ only in return type?
(क्या एक ही स्कोप में दो फंक्शन केवल रिटर्न टाइप अलग होने और समान पैरामीटर होने पर ओवरलोड हो सकते हैं?)
(A) Yes, if one returns `void` and the other `int`
(B) No, return type alone cannot distinguish overloaded functions
(C) Yes, if both are static member functions
(D) Yes, when marked `inline`
Correct Answer: (B)
[EN] C++ overload resolution ignores the return type; identical parameter signatures produce a redeclaration compile error.
[HI] C++ में ओवरलोड रेजोल्यूशन रिटर्न टाइप पर निर्भर नहीं करता; समान पैरामीटर पर केवल रिटर्न टाइप बदलने से कंपाइल एरर आता है।
Q3. Which of the following C++ operators CANNOT be overloaded?
(निम्नलिखित में से कौन-सा C++ ऑपरेटर ओवरलोड नहीं किया जा सकता है?)
(A) `?:` (ternary conditional operator)
(B) `new` (dynamic allocation operator)
(C) `[]` (subscript operator)
(D) `()` (function call operator)
Correct Answer: (A)
[EN] The five non-overloadable operators include `::`, `.`, `.*`, `?:`, and `sizeof`; `[]`, `()`, and `new` can all be overloaded.
[HI] पाँच ऑपरेटर `::`, `.`, `.*`, `?:` और `sizeof` ओवरलोड नहीं किए जा सकते, जबकि `[]`, `()` और `new` ओवरलोड हो सकते हैं।
Q4. Which operator MUST be overloaded as a non-static member function and CANNOT be a `friend` non-member function?
(इनमें से कौन-सा ऑपरेटर केवल नॉन-स्टैटिक मेंबर फंक्शन के रूप में ही ओवरलोड हो सकता है, `friend` के रूप में नहीं?)
(A) `==` (equality comparison)
(B) `<<` (stream insertion)
(C) `=` (assignment operator)
(D) `+` (binary addition)
Correct Answer: (C)
[EN] The operators `=`, `()`, `[]`, and `->` must always be non-static member functions.
[HI] असाइनमेंट (`=`), कॉल (`()`), सबस्क्रिप्ट (`[]`) और एरो (`->`) ऑपरेटर केवल मेंबर फंक्शन ही हो सकते हैं।
Q5. How does the C++ compiler differentiate between pre-increment (`++x`) and post-increment (`x++`) when overloading `operator++`?
(`operator++` को ओवरलोड करते समय कंपाइलर प्री-इंक्रीमेंट (`++x`) और पोस्ट-इंक्रीमेंट (`x++`) में अंतर कैसे करता है?)
(A) Pre-increment must be a `friend` function
(B) Post-increment takes a dummy `int` parameter (`operator++(int)`)
(C) Post-increment returns `void`
(D) Pre-increment takes a dummy `bool` parameter
Correct Answer: (B)
[EN] The dummy `int` parameter in `operator++(int)` exists solely as a syntactic marker for the postfix form.
[HI] पोस्टफिक्स `operator++(int)` में एक डमी `int` पैरामीटर केवल कंपाइलर को प्रीफिक्स से अंतर बताने के लिए दिया जाता है।
Q6. How many explicit parameters does a `friend` function require to overload a binary operator like `+`?
(`+` जैसे बाइनरी ऑपरेटर को `friend` फंक्शन के रूप में ओवरलोड करने के लिए कितने एक्सप्लिसिट पैरामीटर चाहिए?)
(A) 1 parameter
(B) 3 parameters
(C) 2 parameters
(D) 0 parameters
Correct Answer: (C)
[EN] Because a friend function has no implicit `this` pointer, both left and right operands must be passed explicitly.
[HI] friend फंक्शन में `this` पॉइंटर नहीं होता, इसलिए बाइनरी ऑपरेटर के दोनों ऑपरेंड (2 पैरामीटर) पास करने होते हैं।
Q7. Why must stream insertion (`<<`) in `std::cout << myObj;` be overloaded as a non-member (usually `friend`) function rather than a member of `MyClass`?
(`std::cout << myObj;` के लिए `<<` ऑपरेटर को `MyClass` के मेंबर के बजाय नॉन-मेंबर (`friend`) फंक्शन के रूप में क्यों ओवरलोड करते हैं?)
(A) Because the left operand is `std::ostream`, whose standard library class we should not modify
(B) Because `<<` is in the non-overloadable list for member functions
(C) Because `std::cout` is a private global keyword
(D) Because member functions cannot return references
Correct Answer: (A)
[EN] In a member overload `A << B`, `A` must be `*this`; since `std::cout` is on the left, a free function taking `(std::ostream&, const MyClass&)` is required.
[HI] मेंबर ओवरलोड में बायाँ ऑपरेंड `*this` होता है; चूँकि `std::cout` (`std::ostream`) बाईं ओर है, इसलिए नॉन-मेंबर `friend` फंक्शन जरूरी है।
Q8. What happens when you call `calc(10)` if both `void calc(int a)` and `void calc(int a, int b = 5)` are defined in the same scope?
(यदि एक ही स्कोप में `void calc(int a)` और `void calc(int a, int b = 5)` दोनों परिभाषित हों, तो `calc(10)` कॉल करने पर क्या होगा?)
(A) `void calc(int a, int b = 5)` is preferred
(B) Both functions execute sequentially
(C) Compile-time ambiguity error
(D) `void calc(int a)` is preferred unconditionally
Correct Answer: (C)
[EN] Both functions match a single `int` argument equally well (one exact, one via default argument), causing overload ambiguity.
[HI] `calc(10)` दोनों फंक्शन्स के साथ समान रूप से मैच होता है, जिससे कंपाइल-टाइम एम्बियुइटी एरर आता है।
Q9. What does `void show(int x) const` vs `void show(int x)` inside a class demonstrate?
(एक क्लास के भीतर `void show(int x) const` और `void show(int x)` का होना क्या दर्शाता है?)
(A) Compile error because parameter lists are identical
(B) Runtime polymorphism via virtual dispatch
(C) Valid function overloading based on `const` qualification of the implicit `this` pointer
(D) Operator overloading
Correct Answer: (C)
[EN] `const` objects invoke the `const`-qualified overload, while non-const objects prefer the non-const overload.
[HI] यह मेंबर फंक्शन ओवरलोडिंग का मान्य उदाहरण है जहाँ `const` ऑब्जेक्ट `const` वर्जन और नॉन-`const` ऑब्जेक्ट नॉन-`const` वर्जन कॉल करता है।
Q10. Which type promotion occurs first in C++ overload resolution when calling `f('A')` if only `f(int)` and `f(double)` exist?
(यदि केवल `f(int)` और `f(double)` मौजूद हों, तो `f('A')` कॉल करने पर कौन-सा फंक्शन चुना जाएगा?)
(A) Compile error due to char mismatch
(B) Ambiguous call error
(C) `f(double)` via floating promotion
(D) `f(int)` via integral promotion
Correct Answer: (D)
[EN] Integral promotion (`char` to `int`) outranks standard conversion (`char` to `double`) in C++ overload resolution.
[HI] ओवरलोड रेजोल्यूशन में इंटीग्रल प्रमोशन (`char` से `int`) को स्टैंडर्ड कन्वर्जन (`char` से `double`) से उच्च प्राथमिकता मिलती है।
Q11. When overloading `operator+` inside `class Box`, how many explicit parameters are declared in the member function signature?
(`class Box` के भीतर मेंबर फंक्शन के रूप में `operator+` को ओवरलोड करते समय कितने एक्सप्लिसिट पैरामीटर लिखे जाते हैं?)
(A) 1 parameter
(B) 3 parameters
(C) 2 parameters
(D) 0 parameters
Correct Answer: (A)
[EN] The left operand is passed implicitly via `this`, so only the right operand needs an explicit parameter.
[HI] मेंबर फंक्शन में बायाँ ऑपरेंड `this` पॉइंटर से मिल जाता है, इसलिए केवल दाएँ ऑपरेंड के लिए 1 पैरामीटर लिखा जाता है।
Q12. Which of the following changes when you overload an operator in C++?
(C++ में किसी ऑपरेटर को ओवरलोड करने पर निम्नलिखित में से क्या बदलता है?)
(A) Its arity (unary vs binary operand count)
(B) Its associativity (left-to-right vs right-to-left)
(C) Only its behavior/meaning for user-defined types
(D) Its operator precedence in expressions
Correct Answer: (C)
[EN] Operator overloading never changes precedence, associativity, or arity; it only defines behavior for custom types.
[HI] ऑपरेटर ओवरलोडिंग केवल यूजर-डिफाइंड टाइप्स के लिए उसका कार्य बदलती है; उसकी प्रिसिडेंस, एसोसिएटिविटी या ऑपरेंड संख्या नहीं बदलती।
Q13. Can you overload a function based purely on `typedef int MyInt;` vs `int`, i.e. `void g(int)` and `void g(MyInt)`?
(क्या `typedef int MyInt;` बनाकर `void g(int)` और `void g(MyInt)` को अलग-अलग ओवरलोड किया जा सकता है?)
(A) No, `typedef`/`using` creates an alias, not a distinct type, causing redefinition error
(B) Yes, `MyInt` is treated as a separate class type
(C) Yes, only when `using` is used instead of `typedef`
(D) Yes, if `MyInt` is `const`
Correct Answer: (A)
[EN] Type aliases (`typedef` and `using`) refer to the exact same underlying type (`int`), so declaring both defines the same function twice.
[HI] `typedef` और `using` केवल एक एलियास (उपनाम) बनाते हैं, कोई नया डेटा टाइप नहीं; इसलिए दोनों एक ही फंक्शन माने जाते हैं और रीडेफिनिशन एरर आता है।
Q14. Which syntax allows an object `f` of class `Functor` to be invoked like a function `f(3, 4)`?
(कौन-सा ऑपरेटर ओवरलोड करने से क्लास `Functor` का ऑब्जेक्ट `f` फंक्शन की तरह `f(3, 4)` कॉल किया जा सकता है?)
(A) Overloading `operator::`
(B) Overloading `operator->`
(C) Overloading `operator()` (function call operator)
(D) Overloading `operator[]`
Correct Answer: (C)
[EN] Classes overloading `operator()` create function objects (functors) widely used in STL algorithms and lambdas.
[HI] `operator()` (फंक्शन कॉल ऑपरेटर) को ओवरलोड करने से ऑब्जेक्ट फंक्टर बन जाते हैं और उन्हें `f(3, 4)` की तरह कॉल किया जा सकता है।
Q15. Why is compile-time polymorphism generally faster at runtime than virtual-function polymorphism?
(कंपाइल-टाइम पॉलीमॉर्फिज्म सामान्यतः वर्चुअल फंक्शन पॉलीमॉर्फिज्म की तुलना में रन-टाइम पर अधिक तेज क्यों होता है?)
(A) Because compile-time functions store arguments in GPU registers
(B) Because overloaded functions bypass the call stack completely
(C) The target function address is resolved at compile time and can be inlined without vtable indirection
(D) Because `friend` functions disable CPU caching
Correct Answer: (C)
[EN] Early binding lets the compiler emit direct calls or inline the body, avoiding vptr/vtable pointer lookups.
[HI] अर्ली बाइंडिंग में फंक्शन का एड्रेस कंपाइल समय पर तय हो जाता है और इनलाइनिंग संभव होती है, जिससे vtable लुकअप का समय बचता है।
Q16. Which statement about operator overloading in C++ is NOT true?
(C++ में ऑपरेटर ओवरलोडिंग के संबंध में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)
(A) The `.` (dot) operator cannot be overloaded
(B) You can overload `operator+` for two built-in `int` operands to change integer addition globally
(C) Unary minus `-obj` can be overloaded with 0 arguments as a member function
(D) At least one operand of an overloaded operator must be of user-defined type
Correct Answer: (B)
[EN] C++ strictly prohibits changing how operators behave on built-in primitive types alone; at least one user-defined type is mandatory.
[HI] दो बिल्ट-इन प्रिमिटिव टाइप्स (जैसे केवल `int + int`) के लिए ऑपरेटर का व्यवहार ओवरलोडिंग से नहीं बदला जा सकता; कम से कम एक यूजर-डिफाइंड टाइप जरूरी है।
Q17. Within Polymorphism: Compile-Time (Function and Operator Overloading), which operator / feature directly maps to: "Not Overloadable"?
(Polymorphism: Compile-Time (Function and Operator Overloading) में कौन सा Operator / Feature सीधे "Not Overloadable" से संबंधित है?)
(A) `=`, `()`, `[]`, `->`
(B) `::`, `.`, `.*`, `?:`, `sizeof`
(C) `++obj` vs `obj++`
(D) `<<`, `>>` (Stream I/O)
Correct Answer: (B)
[EN] In Polymorphism: Compile-Time (Function and Operator Overloading), '`::`, `.`, `.*`, `?:`, `sizeof`' handles 'Not Overloadable' (Strictly forbidden by C++ grammar).
[HI] Polymorphism: Compile-Time (Function and Operator Overloading) में '`::`, `.`, `.*`, `?:`, `sizeof`' का कार्य/विवरण 'Not Overloadable' (Strictly forbidden by C++ grammar) है।
Q18. Which option accurately describes '`=`, `()`, `[]`, `->`' in Polymorphism: Compile-Time (Function and Operator Overloading)?
(Polymorphism: Compile-Time (Function and Operator Overloading) के संदर्भ में '`=`, `()`, `[]`, `->`' की सही विशेषता कौन सी है?)
(A) Member Function Only
(B) Not Overloadable
(C) 0 args vs dummy int
(D) Friend Non-Member
Correct Answer: (A)
[EN] '`=`, `()`, `[]`, `->`' provides 'Member Function Only' — key point: Cannot be overloaded as friend functions.
[HI] '`=`, `()`, `[]`, `->`' का मुख्य विवरण 'Member Function Only' है (Cannot be overloaded as friend functions)।
Q19. In Polymorphism: Compile-Time (Function and Operator Overloading), identify the correct parameter for the specification: "Friend Non-Member".
(Polymorphism: Compile-Time (Function and Operator Overloading) में "Friend Non-Member" विवरण किस विकल्प के लिए सही है?)
(A) `::`, `.`, `.*`, `?:`, `sizeof`
(B) `=`, `()`, `[]`, `->`
(C) `<<`, `>>` (Stream I/O)
(D) `++obj` vs `obj++`
Correct Answer: (C)
[EN] '`<<`, `>>` (Stream I/O)' corresponds to 'Friend Non-Member' (Left operand is std::ostream or std::istream).
[HI] '`<<`, `>>` (Stream I/O)' का संबंध 'Friend Non-Member' (Left operand is std::ostream or std::istream) से है।
Q20. Considering Polymorphism: Compile-Time (Function and Operator Overloading), what key behavior defines '`++obj` vs `obj++`'?
(Polymorphism: Compile-Time (Function and Operator Overloading) में '`++obj` vs `obj++`' की मुख्य भूमिका या मान क्या है?)
(A) 0 args vs dummy int
(B) Friend Non-Member
(C) Member Function Only
(D) Not Overloadable
Correct Answer: (A)
[EN] '`++obj` vs `obj++`' represents '0 args vs dummy int' (Post-increment takes dummy (int) parameter).
[HI] '`++obj` vs `obj++`' का अर्थ/मान '0 args vs dummy int' है (Post-increment takes dummy (int) parameter)।
Polymorphism: Compile-Time (Function and Operator Overloading) — Exam FAQs
Q1: Why can't we overload `.` (dot) and `::` (scope resolution) in C++?
C++ में `.` (डॉट) और `::` (स्कोप रेजोल्यूशन) ऑपरेटर्स को ओवरलोड क्यों नहीं किया जा सकता?
[EN] The right-hand side of `.` and `::` is a member name (identifier), not a value/expression, and altering their meaning would break fundamental C++ name lookup.
[HI] `.` और `::` के दाईं ओर कोई वैल्यू नहीं बल्कि क्लास मेंबर या स्कोप का नाम होता है; इन्हें ओवरलोड करने की अनुमति देने से C++ का मूल नेम-लुकअप टूट जाएगा।
Q2: When should `operator++()` return by reference (`T&`) vs by value (`T`)?
`operator++()` को रेफरेंस (`T&`) द्वारा और मान (`T`) द्वारा कब रिटर्न करना चाहिए?
[EN] Pre-increment (`++obj`) modifies `*this` and returns `*this` by reference (`T&`), whereas post-increment (`obj++`) saves the old copy, increments `*this`, and returns the old value (`T`) by value.
[HI] प्री-इंक्रीमेंट (`++obj`) ऑब्जेक्ट बदलकर `*this` को रेफरेंस (`T&`) से लौटाता है, जबकि पोस्ट-इंक्रीमेंट (`obj++`) पुरानी कॉपी सेव करके उसे वैल्यू (`T`) द्वारा लौटाता है।
Q3: Are C++ templates also considered compile-time polymorphism?
क्या C++ टेम्प्लेट्स को भी कंपाइल-टाइम पॉलीमॉर्फिज्म माना जाता है?
[EN] Yes; templates provide parametric compile-time polymorphism by generating type-specific functions or classes during compilation.
[HI] हाँ; टेम्प्लेट्स कंपाइलेशन के समय अलग-अलग डेटा टाइप्स के लिए कोड जनरेट करके पैरामेट्रिक कंपाइल-टाइम पॉलीमॉर्फिज्म प्रदान करते हैं।
Post a Comment
Post a Comment