गतिशील बाध्यकारी vpointer और vtable का उपयोग कर रहा है। हालांकि, गतिशील बाध्यकारी केवल फ़ंक्शन पॉइंटर पर लागू होता है। गतिशील बाध्य तर्क के लिए कोई तंत्र नहीं है।
इस प्रकार, डिफ़ॉल्ट तर्क स्थिरता समय पर स्थिर रूप से निर्धारित किया जाता है। इस मामले में, यह स्थिर रूप से बीपी प्रकार द्वारा निर्धारित किया जाता है, जो बेस क्लास के लिए सूचक है।इस प्रकार डेटा = 10 को फंक्शन तर्क के रूप में पास किया जाता है, जबकि फ़ंक्शन पॉइंटर व्युत्पन्न क्लास सदस्य फ़ंक्शन को इंगित कर रहा है: D :: प्रिंट। अनिवार्य रूप से, यह डी :: प्रिंट (10) कहते हैं।
निम्नलिखित कोड स्निपेट और परिणामी आउटपुट स्पष्ट रूप से बिंदु का प्रदर्शन करते हैं: भले ही यह व्युत्पन्न कॉल सदस्य फ़ंक्शन व्युत्पन्न :: आकार बदलें (int) कहता है, यह बेस क्लास डिफ़ॉल्ट तर्क: आकार = 0 पास करता है।
आभासी शून्य व्युत्पन्न :: आकार (int) आकार 0
#include <iostream>
#include <stdio.h>
using namespace std;
#define pr_dbgc(fmt,args...) \
printf("%d %s " fmt "\n",__LINE__,__PRETTY_FUNCTION__, ##args);
class Base {
public:
virtual void resize(int size=0){
pr_dbgc("size %d",size);
}
};
class Derived : public Base {
public:
void resize(int size=3){
pr_dbgc("size %d",size);
}
};
int main()
{
Base * base_p = new Base;
Derived * derived_p = new Derived;
base_p->resize(); /* calling base member function
resize with default
argument value --- size 0 */
derived_p->resize(); /* calling derived member
function resize with default
argument default --- size 3 */
base_p = derived_p; /* dynamic binding using vpointer
and vtable */
/* however, this dynamic binding only
applied to function pointer.
There is no mechanism to dynamic
binding argument. */
/* So, the default argument is determined
statically by base_p type,
which is pointer to base class. Thus
size = 0 is passed as function
argument */
base_p->resize(); /* polymorphism: calling derived class
member function
however with base member function
default value 0 --- size 0 */
return 0;
}
#if 0
The following shows the outputs:
17 virtual void Base::resize(int) size 0
24 virtual void Derived::resize(int) size 3
24 virtual void Derived::resize(int) size 0
#endif
स्रोत
2015-04-07 03:54:26
एक जवाब आपकी समस्या नहीं सुलझती (या आप यह समझ में आता है), तो कृपया इसे स्वीकार करते हैं, उत्तर के बाईं पर हरे रंग की टिक का उपयोग कर। –