Solved

virtual destuctor

Posted on 2011-03-04
6
295 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 500 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
Technology Partners: 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 122
Unresolved External Symbols 3 130
Problem with SqlConnection 4 184
learn programming 8 73
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

730 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