inherit & virtual destruct
It’s a long article.
But it will help you to realize about inherit and why destruct add keyword of virtual.
#include "stdafx.h" class Class_X { public: Class_X() {printf("Class_X Construct\n");} ~Class_X() {printf("Class_X Destruct\n");} }; class Class_Y { public: Class_Y() {printf("Class_Y Construct\n");} ~Class_Y() {printf("Class_Y Destruct\n");} }; class Class_A { public: Class_A() {printf("Class_A Construct\n");} virtual ~Class_A() {printf("Class_A Destruct\n");} Class_Y mClass_Y; }; class Class_B { public: Class_B() {printf("Class_B Construct\n");} ~Class_B() {printf("Class_B Destruct\n");} Class_Y mClass_Y; }; class Class_C : public Class_A { public: Class_C() {printf("Class_C Construct\n");} ~Class_C() {printf("Class_C Destruct\n");} Class_X mClass_X; }; class Class_D : public Class_B { public: Class_D() {printf("Class_D Construct\n");} ~Class_D() {printf("Class_D Destruct\n");} Class_X mClass_X; }; class Class_E : protected Class_B // you can try to add private { public: Class_E() {printf("Class_E Construct\n");} ~Class_E() {printf("Class_E Destruct\n");} Class_X mClass_X; }; int _tmain(int argc, _TCHAR* argv[]) { Class_A *pClass_A = new Class_A(); delete pClass_A; printf("\n"); Class_B *pClass_B = new Class_B(); delete pClass_B; printf("\n"); Class_C *pClass_C1 = new Class_C(); delete pClass_C1; printf("\n"); Class_A *pClass_C2 = new Class_C(); delete pClass_C2; printf("\n"); Class_D *pClass_D1 = new Class_D(); delete pClass_D1; printf("\n"); // there is different with other // please take care Class_B *pClass_D2 = new Class_D(); delete pClass_D2; printf("\n"); Class_E *pClass_E1 = new Class_E(); delete pClass_E1; printf("\n"); // you can remove the mark and compile it again if you are intrested. /*Class_B *pClass_E2 = new Class_E(); delete pClass_E2; printf("\n");*/ char cPause; do { cPause = getchar(); if(cPause == EOF) break; } while(cPause != '\n'); return 0; }
There are results.
You can copy the source code as upward to compile by yourself if the result is difficult to look.
Class_Y Construct Class_A Construct Class_A Destruct Class_Y Destruct Class_Y Construct Class_B Construct Class_B Destruct Class_Y Destruct Class_Y Construct Class_A Construct Class_X Construct Class_C Construct Class_C Destruct Class_X Destruct Class_A Destruct Class_Y Destruct Class_Y Construct Class_A Construct Class_X Construct Class_C Construct Class_C Destruct Class_X Destruct Class_A Destruct Class_Y Destruct Class_Y Construct Class_B Construct Class_X Construct Class_D Construct Class_D Destruct Class_X Destruct Class_B Destruct Class_Y Destruct Class_Y Construct Class_B Construct Class_X Construct Class_D Construct Class_B Destruct Class_Y Destruct Class_Y Construct Class_B Construct Class_X Construct Class_E Construct Class_E Destruct Class_X Destruct Class_B Destruct Class_Y Destruct
It won’t be cleared in memories if you new a child class to variable of parent class without adding the virtual of keyword on destruct
But I couldn’t find any description in the internet.
So I don’t know why.
Please tell me if you are the who you have known.