PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Multiple Inheritance and Diamond Problem: Virtual Base Class (डायमंड समस्या (Diamond Problem) और वर्चुअल बेस क्लास का समाधान) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: The Diamond Problem arises in C++ multipath (hybrid) inheritance when a derived class inherits from two intermediate classes that share a common ancestor, creating duplicate base subobjects and access ambiguity. Declaring the common ancestor as a virtual base class (`virtual public Base`) ensures only one shared base subobject exists and is initialized directly by the most-derived class.
अध्याय सार: C++ के मल्टीपाथ (हाइब्रिड) इनहेरिटेंस में डायमंड समस्या तब उत्पन्न होती है जब एक क्लास दो ऐसी मध्यवर्ती क्लासेस से इनहेरिट करती है जिनका एक साझा पूर्वज (Base) होता है, जिससे डुप्लीकेट कॉपी और अस्पष्टता बनती है। साझा पूर्वज को वर्चुअल बेस क्लास (`virtual public Base`) घोषित करने से डिराइव्ड ऑब्जेक्ट में बेस की केवल एक साझा कॉपी बनती है।
Multiple Inheritance and Diamond Problem: Virtual Base Class: Detailed Notes & Architecture (डायमंड समस्या (Diamond Problem) और वर्चुअल बेस क्लास का समाधान)
1. Anatomy of the Diamond Problem (डायमंड समस्या (Diamond Problem) की संरचना)
[EN] Suppose class A is inherited by both class B and class C, and class D inherits from both B and C (forming an A -> B,C -> D diamond graph). Without virtual inheritance, D inherits two completely distinct A subobjects: one inside B and another inside C. Consequently, accessing an inherited member like d.x or converting D* to A* produces a compile-time ambiguity error because the compiler cannot decide between B::A and C::A.
[HI] मान लीजिए क्लास A से क्लास B और C दोनों इनहेरिट करती हैं, और क्लास D दोनों B व C से इनहेरिट करती है (A -> B,C -> D डायमंड ग्राफ)। बिना वर्चुअल इनहेरिटेंस के, D के अंदर A की दो अलग-अलग कॉपियाँ (एक B के रास्ते, एक C के रास्ते) बन जाती हैं। इसके कारण d.x को एक्सेस करने या D* को A* में बदलने पर कंपाइल-टाइम एम्बियुइटी (अस्पष्टता) एरर आता है।
2. Virtual Base Class Solution Syntax (वर्चुअल बेस क्लास से समाधान का सिंटैक्स)
[EN] To merge the duplicate base subobjects, both intermediate classes B and C must inherit A using the virtual keyword: `class B : virtual public A` and `class C : virtual public A` (writing `public virtual A` is equivalent). The virtual keyword must appear in the intermediate classes (B and C), not in D's inheritance list `class D : public B, public C`. With virtual inheritance in place, D contains exactly one shared instance of A, eliminating ambiguity for d.x and implicit D* to A* upcasting.
[HI] डुप्लीकेट बेस सब-ऑब्जेक्ट्स को मिलाने के लिए दोनों मध्यवर्ती क्लासेस B और C को `virtual` कीवर्ड से A को इनहेरिट करना होता है: `class B : virtual public A` और `class C : virtual public A`। ध्यान दें कि `virtual` कीवर्ड B और C में लगाया जाता है, न कि D की लिस्ट (`class D : public B, public C`) में। इससे D के भीतर A का केवल एक साझा सब-ऑब्जेक्ट बनता है और d.x बिना किसी अस्पष्टता के एक्सेस हो जाता है।
3. Constructor Responsibility and Execution Order (कंस्ट्रक्टर की जिम्मेदारी और निष्पादन क्रम)
[EN] A critical rule of virtual inheritance is that the most-derived class (D) directly initializes the virtual base class (A) in its member initializer list, overriding calls made by B and C. If D omits an explicit call to A(...) in D's initializer list, the compiler invokes A's default constructor A() automatically rather than using B's or C's initializer arguments to A. Execution order is strictly: all virtual base classes first (depth-first, left-to-right), followed by non-virtual direct bases (B, then C), and finally D's own body.
[HI] वर्चुअल इनहेरिटेंस का एक प्रमुख नियम यह है कि सबसे निचली डिराइव्ड क्लास (D) ही सीधे वर्चुअल बेस क्लास (A) के कंस्ट्रक्टर को कॉल करती है, और B व C द्वारा A के कंस्ट्रक्टर कॉल अनदेखे कर दिए जाते हैं। यदि D अपनी इनिशियलाइजर लिस्ट में A(...) नहीं लिखती, तो कंपाइलर स्वतः A का डिफ़ॉल्ट कंस्ट्रक्टर A() चलाता है। निष्पादन क्रम हमेशा पहले वर्चुअल बेस (A), फिर नॉन-वर्चुअल बेस (B, C) और अंत में D होता है।
4. Memory Layout and vbptr Overhead (मेमोरी लेआउट और vbptr का ओवरहेड)
[EN] Because the position of shared virtual base A relative to B or C changes when B or C is embedded inside D, compilers cannot use fixed compile-time member offsets. Instead, compilers insert a hidden virtual base pointer (vbptr) or offset entries inside the vtable (vbtable) within B and C subobjects to locate the shared A at runtime. This adds pointer-sized memory overhead (e.g., 4 or 8 bytes per virtual inheritance path) and one level of pointer indirection when accessing members of A through B* or C*.
[HI] चूँकि D के भीतर साझा वर्चुअल बेस A की मेमोरी स्थिति B या C से बदलती रहती है, इसलिए कंपाइलर फिक्स्ड कंपाइल-टाइम ऑफसेट इस्तेमाल नहीं कर सकता। इसके बजाय कंपाइलर B और C में एक हिडन वर्चुअल बेस पॉइंटर (vbptr) या ऑफसेट टेबल (vbtable) जोड़ता है ताकि रन-टाइम पर साझा A का पता मिल सके। इससे ऑब्जेक्ट का आकार (sizeof) थोड़ा बढ़ता है और A के मेंबर एक्सेस में एक अतिरिक्त पॉइंटर इंडायरेक्शन जुड़ता है।
Syntax, Formula & Technical Example (Multiple Inheritance and Diamond Problem: Virtual Base Class)
#include <iostream>
class A { public: int val; A(int v = 0) : val(v) {} };
class B : virtual public A { public: B() : A(10) {} };
class C : virtual public A { public: C() : A(20) {} };
class D : public B, public C {
public:
D() : A(99), B(), C() {}
};
int main() { D d; std::cout << d.val; return 0; }
Multiple Inheritance and Diamond Problem: Virtual Base Class Quick Reference & Comparison Table
| Aspect | Without Virtual Base | With Virtual Base |
|---|---|---|
| Copies of Root Class A | 2 separate subobjects in D | 1 shared subobject in D |
| Unqualified Member Access | Compile error: ambiguous request | Unambiguous direct access |
| A Constructor Caller | Both B and C initialize own A | Most-derived class D calls A |
| Memory / Pointer Overhead | No vbptr; doubled A data size | Includes vbptr / vbtable offset |
Multiple Inheritance and Diamond Problem: Virtual Base Class Top 20 Bilingual MCQs with Answers
Q1. In a classic diamond hierarchy `A -> B`, `A -> C`, and `B, C -> D` WITHOUT virtual inheritance, how many copies of `A`'s data members exist in an object of `D`?
(बिना वर्चुअल इनहेरिटेंस के क्लासिक डायमंड हायरार्की (`A -> B`, `A -> C`, `B, C -> D`) में `D` के ऑब्जेक्ट में `A` की कितनी कॉपियाँ मौजूद होती हैं?)
(A) 4 copies
(B) 3 copies
(C) 1 copy
(D) 2 copies
Correct Answer: (D)
[EN] D inherits one A subobject through B and a second independent A subobject through C.
[HI] बिना virtual कीवर्ड के, D को एक A की कॉपी B से और दूसरी स्वतंत्र कॉपी C से प्राप्त होती है।
Q2. Where must the `virtual` keyword be placed to solve the diamond problem in `A -> (B, C) -> D`?
(`A -> (B, C) -> D` में डायमंड समस्या को हल करने के लिए `virtual` कीवर्ड कहाँ लगाया जाना चाहिए?)
(A) In the inheritance lists of both B and C (`class B : virtual public A`, `class C : virtual public A`)
(B) Only in `class D : virtual public B, virtual public C`
(C) Inside `class A`'s destructor only
(D) Before class A's definition (`virtual class A`)
Correct Answer: (A)
[EN] The intermediate classes B and C must inherit A virtually so they agree to share a single A subobject in D.
[HI] मध्यवर्ती क्लासेस B और C दोनों को A को वर्चुअली इनहेरिट करना चाहिए ताकि D में A का केवल एक साझा सब-ऑब्जेक्ट बने।
Q3. In the code `class A { public: int val; A(int v=0):val(v){} }; class B : virtual public A { public: B():A(10){} }; class C : virtual public A { public: C():A(20){} }; class D : public B, public C { public: D():B(),C(){} };`, what is `d.val`?
(यदि `A(int v=0)`, `B():A(10)`, `C():A(20)` (दोनों `virtual public A`) और `D():B(),C()` हो (जहाँ D ने A को कॉल नहीं किया), तो `d.val` क्या होगा?)
(A) 0
(B) Compile-time error
(C) 20
(D) 10
Correct Answer: (A)
[EN] Because D is the most-derived class and omitted A(...) in its initializer list, A's default constructor A(0) is called, ignoring B's and C's calls to A.
[HI] सबसे निचली क्लास D द्वारा A(...) न लिखने पर A का डिफ़ॉल्ट कंस्ट्रक्टर A(0) चलता है; B और C के कॉल अनदेखे हो जाते हैं।
Q4. What happens if `class B : virtual public A` and `class C : public A` (only one intermediate class uses `virtual`) in `class D : public B, public C`?
(यदि `class B : virtual public A` हो और `class C : public A` (केवल एक में virtual हो), तो `class D : public B, public C` में `A` की कितनी कॉपियाँ होंगी?)
(A) Compile error in class C
(B) 1 shared copy of A
(C) 2 copies of A (ambiguity still occurs)
(D) 3 copies of A
Correct Answer: (C)
[EN] Virtual inheritance only merges virtual base subobjects; a non-virtual inheritance path (C -> A) still creates its own separate A subobject.
[HI] केवल वर्चुअल पाथ वाले बेस आपस में मर्ज होते हैं; नॉन-वर्चुअल पाथ (C -> A) अपनी अलग कॉपी बनाता है जिससे अभी भी 2 कॉपियाँ रहती हैं।
Q5. With `class B : virtual public A`, `class C : virtual public A`, and `class D : public B, public C`, in which order do constructors execute when creating `D`?
(`class B : virtual public A`, `class C : virtual public A` और `class D : public B, public C` में `D` का ऑब्जेक्ट बनाते समय कंस्ट्रक्टर क्रम क्या होगा?)
(A) B -> A -> C -> D
(B) D -> B -> C -> A
(C) A -> B -> C -> D
(D) A -> B -> A -> C -> D
Correct Answer: (C)
[EN] Virtual base A is constructed once first, followed by non-virtual bases B and C in declaration order, and finally D.
[HI] वर्चुअल बेस A का कंस्ट्रक्टर सबसे पहले एक बार चलता है, फिर B, फिर C और अंत में D।
Q6. Are `class B : virtual public A` and `class B : public virtual A` both valid syntax in C++?
(क्या C++ में `class B : virtual public A` और `class B : public virtual A` दोनों मान्य सिंटैक्स हैं?)
(A) Only in C++17 and newer
(B) No, `public` must always precede `virtual`
(C) No, `virtual` must always precede `public`
(D) Yes, the order of `virtual` and `public` does not matter
Correct Answer: (D)
[EN] The C++ grammar allows the access specifier and the `virtual` keyword in either order in the base-specifier list.
[HI] C++ सिंटैक्स में बेस-स्पेसिफायर सूची के भीतर `virtual` और एक्सेस स्पेसिफायर (`public`) किसी भी क्रम में लिखे जा सकते हैं।
Q7. Which runtime mechanism do C++ compilers typically use inside `B` and `C` to locate the shared virtual base `A`?
(C++ कंपाइलर `B` और `C` में साझा वर्चुअल बेस `A` का पता लगाने के लिए सामान्यतः किस मैकेनिज्म का उपयोग करते हैं?)
(A) Friend function indirection
(B) Dynamic preprocessor macro substitution
(C) Static global variable lookup
(D) Virtual base pointer (vbptr) or offset entries in the vtable
Correct Answer: (D)
[EN] Because the offset from B or C to A varies depending on the most-derived class, compilers track it via vbptr/vbtable offsets.
[HI] चूँकि B या C से A का ऑफसेट सबसे निचली क्लास के आधार पर बदलता है, कंपाइलर `vbptr` या `vbtable` ऑफसेट उपयोग करते हैं।
Q8. Without virtual inheritance in `A -> (B, C) -> D`, how can `D` disambiguate accessing `A::x`?
(बिना वर्चुअल इनहेरिटेंस वाले डायमंड में `D` का ऑब्जेक्ट `d`, `A::x` को बिना एरर कैसे एक्सेस कर सकता है?)
(A) By writing `d.A::x` directly
(B) It is impossible under any syntax
(C) By marking `x` as `mutable`
(D) By qualifying the path: `d.B::x` or `d.C::x`
Correct Answer: (D)
[EN] Qualifying with an intermediate class (`d.B::x`) selects that specific copy of A, whereas `d.A::x` remains ambiguous.
[HI] मध्यवर्ती क्लास के नाम से (`d.B::x` या `d.C::x`) किसी एक विशेष पाथ की कॉपी को चुना जा सकता है।
Q9. Why can `static_cast<D*>(ptr_to_A)` fail to compile when `A` is a virtual base class of `D`?
(जब `A`, `D` का वर्चुअल बेस क्लास हो, तो `static_cast<D*>(ptr_to_A)` कंपाइल क्यों नहीं होता?)
(A) `A` must be `private` for `static_cast` to work
(B) Virtual base classes do not allow `D*` pointers
(C) Downcasting from a virtual base requires runtime offset info via `dynamic_cast`, not `static_cast`
(D) `static_cast` cannot cast any pointers in C++
Correct Answer: (C)
[EN] Because a virtual base's offset relative to D is resolved dynamically at runtime, C++ forbids `static_cast` down from a virtual base and mandates `dynamic_cast` (if polymorphic).
[HI] वर्चुअल बेस से डिराइव्ड क्लास का ऑफसेट रन-टाइम पर तय होता है, इसलिए `static_cast` वर्चुअल बेस से डाउनकास्ट नहीं कर सकता; इसके लिए `dynamic_cast` चाहिए।
Q10. Suppose `class A` has `int x;` (4 bytes) and `class B : virtual public A {};` has no data members. On a 64-bit compiler using 8-byte pointers, why is `sizeof(B)` typically 16 bytes instead of 4?
(मान लीजिए `class A` में केवल `int x;` (4 बाइट्स) है और `class B : virtual public A {};` खाली है। 64-बिट सिस्टम पर `sizeof(B)` 4 के बजाय 16 बाइट्स क्यों होता है?)
(A) Due to the hidden 8-byte `vbptr` (virtual base pointer) plus 4-byte `x` and struct alignment padding
(B) Because `virtual` doubles every `int` to 8 bytes
(C) Because `B` allocates 12 bytes for a default destructor
(D) Because every empty class is 16 bytes
Correct Answer: (A)
[EN] An 8-byte vbptr/vptr plus 4-byte int (12 bytes) aligns to 8-byte boundaries, yielding 16 bytes total.
[HI] 8-बाइट के हिडन `vbptr` और 4-बाइट के `int` (12 बाइट्स) में 8-बाइट एलाइनमेंट पैडिंग जुड़ने से कुल आकार 16 बाइट्स हो जाता है।
Q11. In multiple inheritance without a shared ancestor (`class C : public A, public B` where `A` and `B` have no base class), does the diamond problem occur?
(यदि `class C : public A, public B` में `A` और `B` का कोई साझा पूर्वज (common ancestor) न हो, तो क्या डायमंड समस्या उत्पन्न होती है?)
(A) Yes, C automatically inherits A twice
(B) Yes, constructors become invalid
(C) No, though same-named members in A and B still cause name ambiguity
(D) Yes, `virtual` is mandatory whenever there are 2 base classes
Correct Answer: (C)
[EN] The diamond problem specifically requires a common ancestor (`A -> B, C -> D`); independent bases only risk simple name collisions.
[HI] डायमंड समस्या के लिए साझा पूर्वज (`A -> B, C -> D`) होना आवश्यक है; स्वतंत्र बेस में केवल समान नाम टकराने की समस्या हो सकती है।
Q12. What error occurs if `class A` has only a parameterized constructor `A(int)` (no default constructor) and `D` virtually inherits `A` via `B` and `C` without calling `A(int)` in `D`'s initializer list?
(यदि `A` में केवल `A(int)` कंस्ट्रक्टर है (कोई डिफ़ॉल्ट कंस्ट्रक्टर नहीं) और `D` अपनी इनिशियलाइजर लिस्ट में `A(...)` को कॉल नहीं करता, तो क्या होगा?)
(A) Compile-time error: no matching default constructor for `A`
(B) `B` supplies its argument to `A(int)` automatically
(C) `C`'s constructor argument overrides `B`'s
(D) `A` is left uninitialized at runtime
Correct Answer: (A)
[EN] The most-derived class D must explicitly construct the virtual base A; otherwise the compiler searches for A() and fails.
[HI] सबसे निचली क्लास D को ही वर्चुअल बेस A(int) को सीधे इनिशियलाइज करना होता है; न करने पर डिफ़ॉल्ट A() न मिलने का कंपाइल एरर आता है।
Q13. Which design principle in languages like Java replaces C++ multiple class inheritance to avoid the diamond problem completely?
(Java जैसी भाषाओं में डायमंड समस्या से बचने के लिए C++ के मल्टीपल क्लास इनहेरिटेंस की जगह क्या उपयोग होता है?)
(A) Eliminating constructors entirely
(B) Single class inheritance combined with multiple interfaces
(C) Multilevel inheritance only
(D) Goto-based method dispatching
Correct Answer: (B)
[EN] Java restricts classes to single inheritance while allowing multiple `interface` implementations, avoiding duplicate state.
[HI] Java में एक समय में केवल एक क्लास से इनहेरिटेंस और कई `interface` लागू करने की अनुमति देकर डायमंड स्टेट समस्या से बचा जाता है।
Q14. In `A -> (B, C) -> D` with `B` and `C` both inheriting `virtual public A`, what happens when `D` calls `d.foo()`, if `A` defines `foo()`, `B` overrides `foo()`, and `C` does NOT define `foo()`?
(`A -> (B, C) -> D` (जहाँ B व C वर्चुअली A से इनहेरिट करते हैं) में यदि `A` में `foo()` हो, `B` उसे ओवरराइड करे, और `C` न करे, तो `d.foo()` किसे कॉल करेगा?)
(A) `B::foo()` due to C++ dominance rule
(B) Runtime exception
(C) Compile-time ambiguity between `B::foo()` and `A::foo()`
(D) `A::foo()` because `C` inherits directly from `A`
Correct Answer: (A)
[EN] Under the C++ dominance rule in virtual inheritance, `B::foo()` dominates `A::foo()` because `B` is more derived than `A` along that virtual path.
[HI] C++ के डोमिनेंस नियम (dominance rule) के अनुसार वर्चुअल इनहेरिटेंस में `B::foo()`, `A::foo()` पर हावी रहता है और बिना अस्पष्टता `B::foo()` चलता है।
Q15. If both `B` and `C` override `A::foo()` in a virtual diamond (`A -> (B, C) -> D`), and `D` does NOT override `foo()`, what happens on `d.foo()`?
(यदि वर्चुअल डायमंड में `B` और `C` दोनों `A::foo()` को ओवरराइड करें और `D` ओवरराइड न करे, तो `d.foo()` कॉल करने पर क्या होगा?)
(A) `A::foo()` is called as fallback
(B) `B::foo()` wins automatically
(C) Compile-time ambiguity error (`request for member 'foo' is ambiguous`)
(D) `C::foo()` wins automatically
Correct Answer: (C)
[EN] Virtual inheritance merges A's state, not conflicting overrides in B and C; D must override `foo()` or callers must qualify `d.B::foo()`.
[HI] वर्चुअल इनहेरिटेंस केवल A की कॉपी को मिलाता है; यदि B और C दोनों फंक्शन ओवरराइड करें तो `d.foo()` अस्पष्ट हो जाता है और कंपाइल एरर देता है।
Q16. Which statement about virtual base classes in C++ is NOT true?
(C++ में वर्चुअल बेस क्लास के बारे में कौन-सा कथन सत्य नहीं है?)
(A) The most-derived class is responsible for initializing the virtual base
(B) The virtual base constructor is called multiple times—once by B, once by C, and once by D
(C) Only one shared instance of the virtual base exists in the most-derived object
(D) Virtual base constructors execute before non-virtual base constructors
Correct Answer: (B)
[EN] The entire purpose of a virtual base is that its constructor runs strictly ONCE, invoked by the most-derived class.
[HI] वर्चुअल बेस क्लास का कंस्ट्रक्टर केवल एक बार (सबसे निचली डिराइव्ड क्लास द्वारा) चलता है, न कि B, C और D द्वारा बार-बार।
Q17. Within Multiple Inheritance and Diamond Problem: Virtual Base Class, which aspect directly maps to: "2 separate subobjects in D"?
(Multiple Inheritance and Diamond Problem: Virtual Base Class में कौन सा Aspect सीधे "2 separate subobjects in D" से संबंधित है?)
(A) A Constructor Caller
(B) Memory / Pointer Overhead
(C) Unqualified Member Access
(D) Copies of Root Class A
Correct Answer: (D)
[EN] In Multiple Inheritance and Diamond Problem: Virtual Base Class, 'Copies of Root Class A' handles '2 separate subobjects in D' (1 shared subobject in D).
[HI] Multiple Inheritance and Diamond Problem: Virtual Base Class में 'Copies of Root Class A' का कार्य/विवरण '2 separate subobjects in D' (1 shared subobject in D) है।
Q18. Which option accurately describes 'Unqualified Member Access' in Multiple Inheritance and Diamond Problem: Virtual Base Class?
(Multiple Inheritance and Diamond Problem: Virtual Base Class के संदर्भ में 'Unqualified Member Access' की सही विशेषता कौन सी है?)
(A) Compile error: ambiguous request
(B) No vbptr; doubled A data size
(C) Both B and C initialize own A
(D) 2 separate subobjects in D
Correct Answer: (A)
[EN] 'Unqualified Member Access' provides 'Compile error: ambiguous request' — key point: Unambiguous direct access.
[HI] 'Unqualified Member Access' का मुख्य विवरण 'Compile error: ambiguous request' है (Unambiguous direct access)।
Q19. In Multiple Inheritance and Diamond Problem: Virtual Base Class, identify the correct parameter for the specification: "Both B and C initialize own A".
(Multiple Inheritance and Diamond Problem: Virtual Base Class में "Both B and C initialize own A" विवरण किस विकल्प के लिए सही है?)
(A) Copies of Root Class A
(B) Memory / Pointer Overhead
(C) Unqualified Member Access
(D) A Constructor Caller
Correct Answer: (D)
[EN] 'A Constructor Caller' corresponds to 'Both B and C initialize own A' (Most-derived class D calls A).
[HI] 'A Constructor Caller' का संबंध 'Both B and C initialize own A' (Most-derived class D calls A) से है।
Q20. Considering Multiple Inheritance and Diamond Problem: Virtual Base Class, what key behavior defines 'Memory / Pointer Overhead'?
(Multiple Inheritance and Diamond Problem: Virtual Base Class में 'Memory / Pointer Overhead' की मुख्य भूमिका या मान क्या है?)
(A) 2 separate subobjects in D
(B) No vbptr; doubled A data size
(C) Both B and C initialize own A
(D) Compile error: ambiguous request
Correct Answer: (B)
[EN] 'Memory / Pointer Overhead' represents 'No vbptr; doubled A data size' (Includes vbptr / vbtable offset).
[HI] 'Memory / Pointer Overhead' का अर्थ/मान 'No vbptr; doubled A data size' है (Includes vbptr / vbtable offset)।
Multiple Inheritance and Diamond Problem: Virtual Base Class — Exam FAQs
Q1: Does virtual inheritance fix ambiguity if B and C each define their own separate variable `int x`?
क्या यदि B और C दोनों में अपना अलग वेरिएबल `int x` हो, तो भी वर्चुअल इनहेरिटेंस अस्पष्टता खत्म कर देगा?
[EN] No; virtual inheritance only merges members inherited from the common virtual base `A`. Members declared independently inside `B` and `C` remain distinct and ambiguous without `B::x` or `C::x`.
[HI] नहीं; वर्चुअल इनहेरिटेंस केवल साझा बेस `A` के मेंबर्स को मिलाता है। `B` और `C` के अपने स्वतंत्र मेंबर्स के लिए फिर भी `B::x` या `C::x` लिखना होगा।
Q2: Is there any runtime performance cost to using virtual base classes?
क्या वर्चुअल बेस क्लास के उपयोग से रन-टाइम पर कोई परफॉर्मेंस प्रभाव पड़ता है?
[EN] Yes, accessing members of a virtual base requires an extra pointer dereference via `vbptr`/`vbtable`, and objects carry an extra pointer per virtual base hierarchy.
[HI] हाँ, वर्चुअल बेस के मेंबर्स को एक्सेस करने में `vbptr`/`vbtable` के कारण एक अतिरिक्त पॉइंटर इंडायरेक्शन और ऑब्जेक्ट साइज में थोड़ा ओवरहेड जुड़ता है।
Q3: What is the difference between a virtual function and a virtual base class?
वर्चुअल फंक्शन और वर्चुअल बेस क्लास में क्या अंतर है?
[EN] A virtual function enables runtime polymorphism (dynamic dispatch via vtable), whereas a virtual base class resolves the diamond problem by ensuring a single shared ancestor subobject in multiple inheritance.
[HI] वर्चुअल फंक्शन रन-टाइम पॉलीमॉर्फिज्म (डायनामिक डिस्पैच) के लिए है, जबकि वर्चुअल बेस क्लास मल्टीपल इनहेरिटेंस की डायमंड समस्या में एक साझा बेस कॉपी बनाने के लिए है।
Post a Comment
Post a Comment