GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 163 of 203    Print  
Virtual destructor in derived classes
Can Virtual destructor is used in derived classes?

What is the answer to the following question?

Class Base {
Public:
Base();
Virtual ~Base();
};

Class Derived : protected Base {
Public:
Virtual ~Derived();
};

Int main()
{
Base *pb = new Derived;
Return 0;
}


Referring to the sample code above, which one of the following statements is true?
Choice 1 The pointer returned by new cannot be type cast from Derived* to Base*.
Choice 2 The code compiles with no errors.
Choice 3 A constructor needs to be added to Derived class.
Choice 4 A pointer to a Base class cannot point to an instance of a Derived class.
Choice 5 Derived class cannot have a virtual destructor.



  
Total Answers and Comments: 5 Last Update: September 11, 2008     Asked by: vvadan 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: pareshsinha
 
Since Derived class has been derived from protected Base, this code will generate compilation error - unable to cast from derived * to base *. To resolve the the error change access protection of Base from protected to public.

Above answer was rated as good by the following members:
swaticsabale
April 25, 2008 01:23:22   #1  
pareshsinha Member Since: April 2008   Contribution: 1    

RE: Virtual destructor in derived classes
Since Derived class has been derived from protected Base this code will generate compilation error - unable to cast from derived * to base *. To resolve the the error change access protection of Base from protected to public.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
May 13, 2008 06:32:04   #2  
nannamu Member Since: May 2008   Contribution: 1    

RE: Virtual destructor in derived classes

When should your C++ object's destructor be virtual?

First of all what does it mean to have a virtual destructor?

Well what does it mean to have a virtual method?

If a method is virtual then calling the method on an object always invokes the method as implemented by the most heavily derived class. If the method is not virtual then the implementation corresponding to the compile-time type of the object pointer.

For example consider this:

class Sample {
public:
void f();
virtual void vf();
};

class Derived : public Sample {
public:
void f();
void vf();
}

void function()
{
Derived d;
Sample* p &d;
p->f();
p->vf();
}

The call to p->f() will result in a call to Sample::f because p is a pointer to a Sample. The actual object is of type Derived but the pointer is merely a pointer to a Sample. The pointer type is used because f is not virtual.

On the other hand the call to The call to p->vf() will result in a call to Derived::vf the most heavily derived type because vf is virtual.

Okay you knew that.

Virtual destructors work exactly the same way. It's just that you rarely invoke the destructor explicitly. Rather it's invoked when an automatic object goes out of scope or when you delete the object.

void function()
{
Sample* p new Derived;
delete p;
}

Since Sample does not have a virtual destructor the delete p invokes the destructor of the class of the pointer (Sample::~Sample()) rather than the destructor of the most derived type (Derived::~Derived()). And as you can see this is the wrong thing to do in the above scenario.

Armed with this information you can now answer the question.

A class must have a virtual destructor if it meets both of the following criteria:

  • You do a delete p.
  • It is possible that p actually points to a derived class.

Some people say that you need a virtual destructor if and only if you have a virtual method. This is wrong in both directions.

Example of a case where a class has no virtual methods but still needs a virtual destructor:

class Sample { };
class Derived : public Sample
{
CComPtr<IStream> m_p;
public:
Derived() { CreateStreamOnHGlobal(NULL TRUE &m_p); }
};

Sample *p new Derived;
delete p;

The delete p will invoke Sample::~Sample instead of Derived::~Derived resulting in a leak of the stream m_p.

And here's an example of a case where a class has virtual methods but does not require a virtual destructor.

class Sample { public: virtual void vf(); }
class Derived : public Sample { public: virtual void vf(); }

Derived *p new Derived;
delete p;

Since the object deletion occurs from the pointer type that matches the type of the actual object the correct destructor will be invoked. This pattern happens often in COM objects which expose several virtual methods corresponding to its interfaces but where the object itself is destroyed by its own implementation and not from a base calss pointer. (Notice that no COM interfaces contain virtual destructors.)

The problem with knowing when to make your destructor virtual or not is that you have to know how people will be using your class. If C++ had a "sealed" keyword then the rule would be simpler: If you do a "delete p" where p is a pointer to an unsealed class then that class needs have a virtual destructor. (The imaginary "sealed" keyword makes it explicit when a class can act as the base class for another class.)


 
Is this answer useful? Yes | No
June 09, 2008 16:12:52   #3  
akrika Member Since: June 2008   Contribution: 1    

RE: Virtual destructor in derived classes
no
 
Is this answer useful? Yes | No
June 20, 2008 06:03:29   #4  
gaurava12 Member Since: June 2008   Contribution: 1    

RE: Virtual destructor in derived classes
Choice 3 A constructor needs to be added to Derived class.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
September 11, 2008 09:27:38   #5  
OSaienni Member Since: September 2008   Contribution: 7    

RE: Virtual destructor in derived classes
Choice 4 A pointer to a Base class cannot point to an instance of a Derived class. class cannot point to an instance of a Derived class.

This the the only truthful answer.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape