Tuesday, March 27, 2012
Virtual destructors in C++
C++ gives default destructor, if you not define the destructor. But this wont be help full if you use dynamic memory allocation. You need to write the destructor function manually to deallocate the memory. This works fine if there is no derived class. If there is any derived class with memory allocation, you should use virtual destructors. see the sample code below.
this is base CTOR
this is derived CTOR
this is base DTOR
In the above sample code, there is derived class with char pointer member data. Memory is allocated in derived constructor and deallocated in derived destructor. In main() function , derived object is created using new, this is assigned to base object. And base object is deallocated using delete. In the output you got, base constructor, derived constructor, and base destructor. And there is no derived destructor. Actually, we allocated memory in derived constructor. so there is a memory leak, because we are not freed the allocated memory.
To avoid such problems, we need to use virtual destructors. Actually compiler looks for the base destructor and checks for the virtual keyword for the destructor, if it finds virtual keyword, it further goes to derived destructor and goes on till the refered derived class. from there it calls destructor one by one from derived destructor to till it reaches base destructor. If there is no virtual keyword in the base destructor, it stops in base destructor itself. See the sample code with virtual destructor.
Code with virtual destructor:
Output:
this is base CTOR
this is derived CTOR
this is derived DTOR
this is base DTOR
In this example, we used virtual destructor, when it calls deleting base pointer, it looks for the base destructor, then it looks for the virtual keyword for the destructor, it will get the keyword, so it looks for the derived destructor, it finds the destructor, and there is no derived classes, so it calls the derived destructor, then it calls the base destructor as shown above. In red color output , is the derived destructor.
#include<iostream> using namespace std; class base { public: base() //constructor { cout<<"this is base CTOR\n"; } ~base() //not a virtual destructor { cout<<"this is base DTOR\n"; } }; class derived:public base { char *str; public: derived() { cout<<"this is derived CTOR\n"; str = new char[20]; // allocating space } ~derived() { cout<<"this is derived DTOR\n"; delete[] str; // deleting } }; main() { derived *d = new derived(); // creating derived object base *b = d; // assigning derived object to base object delete b; // deleting base object }Output:
this is base CTOR
this is derived CTOR
this is base DTOR
In the above sample code, there is derived class with char pointer member data. Memory is allocated in derived constructor and deallocated in derived destructor. In main() function , derived object is created using new, this is assigned to base object. And base object is deallocated using delete. In the output you got, base constructor, derived constructor, and base destructor. And there is no derived destructor. Actually, we allocated memory in derived constructor. so there is a memory leak, because we are not freed the allocated memory.
To avoid such problems, we need to use virtual destructors. Actually compiler looks for the base destructor and checks for the virtual keyword for the destructor, if it finds virtual keyword, it further goes to derived destructor and goes on till the refered derived class. from there it calls destructor one by one from derived destructor to till it reaches base destructor. If there is no virtual keyword in the base destructor, it stops in base destructor itself. See the sample code with virtual destructor.
Code with virtual destructor:
#include<iostream> using namespace std; class base { public: base() // base constructor { cout<<"this is base CTOR\n"; } virtual ~base() // virtual destructor { cout<<"this is base DTOR\n"; } }; class derived:public base { char *str; public: derived() { cout<<"this is derived CTOR\n"; str = new char[20]; // allocating space } ~derived() { cout<<"this is derived DTOR\n"; delete[] str; // deleting } }; main() { derived *d = new derived(); // creating derived object base *b = d; // assigning derived object to base object delete b; // deleting base object }
Output:
this is base CTOR
this is derived CTOR
this is derived DTOR
this is base DTOR
In this example, we used virtual destructor, when it calls deleting base pointer, it looks for the base destructor, then it looks for the virtual keyword for the destructor, it will get the keyword, so it looks for the derived destructor, it finds the destructor, and there is no derived classes, so it calls the derived destructor, then it calls the base destructor as shown above. In red color output , is the derived destructor.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...
No comments:
Post a Comment