Explain the need for "Virtual Destructor"?

Answers were Sorted based on User's Feedback



Explain the need for "Virtual Destructor"?..

Answer / lylez00

If A is a base class, and from that, B is derived, and a
dynamically allocated object of type B is deleted via a
pointer of type A, then B's destructor will not be invoked
unless A's destructor is virtual.

A *a = new B();
delete a; // won't invoke B's destructor unless A's
destructor is virtual

Is This Answer Correct ?    12 Yes 3 No

Explain the need for "Virtual Destructor"?..

Answer / p govind rao

A destructor can be declare virtual. virtual destructor is
mainly useful during inheritance.

class base
{
public:
base(){}
virtual ~base(){}
};

class derv
{ char *p;
public :
derv(){ptr=nes char[2];}
~derv(){delete ptr;}

} ;

main()
{
base *baseptr=new derv();
delete baseptr;
}

If base class, and derived class, and a dynamically
allocated object of type derived is deleted via a pointer
of type base, then derived's destructor will not be invoked
unless base's destructor is virtual.

base *baseptr = new derv();
delete baseptr; // won't invoke B's destructor unless A's
destructor is virtual

Is This Answer Correct ?    6 Yes 0 No

Post New Answer

More C++ General Interview Questions

What is a binary file? List the merits and demerits of the binary file usagein C++.

0 Answers  


What is the use of lambda in c++?

0 Answers  


What are special characters c++?

0 Answers  


What is the difference between = and == in C?

16 Answers   Christ University, Intel,


What do you know about near, far and huge pointer?

0 Answers  






What is auto type c++?

0 Answers  


What are the advantages of prototyping?

0 Answers  


Assume an array of structure is in order by studentID field of the record, where student IDs go from 101 to 500. Write the most efficient pseudocode algorithm you can to find the record with a specific studentID if every single student ID from 101 to 500 is used and the array has 400 elements. Write the most efficient pseudocode algorithm you can to find a record with a studentID near the end of the IDs, say in the range from 450 to 500, if not every single student ID in the range of 101 to 500 is used and the array size is only 300

0 Answers  


What do the header files usually contains?

0 Answers  


How does java differ from c and c++?

0 Answers  


What is an associative container in c++?

0 Answers  


Evaluate as true or false: !(1 &&0 || !1) a) True b) False c) Invalid statement

0 Answers  


Categories