++C מפרקים וירטואלים.
כתבתי את הקוד הבא (לא חייבים לקרוא את כולו, אפשר לדלג לשאלה):
כתבתי את הקוד הבא (לא חייבים לקרוא את כולו, אפשר לדלג לשאלה):
include <iostream> using namespace std; class father { protected: int num; public: father(){static int c=0;father::num=++c;cout<<"father ctor
bject number "<<father::num<<endl;} ~father(){cout<<"father dtor
bject number "<<father::num<<endl;} }; class son
ublic father { private: int num; public: son(){static int c=0;son::num=++c;cout<<"son of "<<father::num<<" ctor:son number "<<son::num<<endl;} ~son(){cout<<"son of "<<father::num<<" dtor:son number "<<son::num<<endl;} }; int main(int argc, char *argv[]) { father f1; son s1; cin.get(); return 0; }
וקימפלתי ב++g גרסה 2.95.2 והפלט אומר שגם הבנאי וגם המפרק של האב רצו כאשר יצרתי אובייקט בן. עם הבנאי אין לי בעיה, אבל הזהירו לעשות תמיד מפרק וירטואלי בכדי שירוץ במחלקות יורשות, ופה הוא רץ בכל מקרה, אפילו שהוא לא וירטואלי, אז בשביל מה להכריז וירטואלי? תודה, סלע.