?
Solved

virtual destuctor

Posted on 2011-03-04
6
Medium Priority
?
297 Views
Last Modified: 2012-05-11
I have seen following in my appln.

class A {

virtual ~A();
}

class B : public A {
  B()
}

Inside B.cpp, the function ~A() and ~B() both is missing

Shouldn't the base class destructor MUST be defined in child class
0
Comment
Question by:learningunix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 35039432
Virtual destructors only guarantee for the calling order, nothing else - you don't necessarily have to provide an explicit destructor in a derived class, the compiler will create a default destructor that will then be chained in the sequence - see also http://www.codersource.net/c/c-miscellaneous/c-virtual-destructors.aspx
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 35041693
Unlike ordinary member functions, a virtual destructor is not overridden when redefined in a derived class. Rather, it is extended: the lower-most destructor first invokes the destructor of its base class and only then, it is executed.

0
 
LVL 86

Expert Comment

by:jkr
ID: 35041726
DeepuAbrahamK, it's always a good idea to quote your sources: http://www.codeguru.com/forum/archive/index.php/t-445242.html

STLDude
February 4th, 2008, 10:58 AM
A little more info:
Unlike ordinary member functions, a virtual destructor is not overridden when redefined in a derived class. Rather, it is extended: the lower-most destructor first invokes the destructor of its base class and only then, it is executed.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 35041767

class A
{
public:
      A()
      {
            cout<<"I am inside the ctor : A"<<endl;
      }

    virtual ~A()
      {
            cout<<"I am inside the dtor : A"<<endl;
      }
};

class B:public A
{
public:
      B()
      {
            cout<<"I am inside the ctor : B"<<endl;
      }

    virtual ~B()
      {
            cout<<"I am inside the dtor : B"<<endl;
      }
};


int main () {
  A* a = new B();
  delete a;
}
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 35041775
I found that statement is very crisp:
pleasw refer : http://www.devx.com/tips/Tip/12729
0
 

Author Closing Comment

by:learningunix
ID: 35046552
thx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question