Solved

virtual destuctor

Posted on 2011-03-04
6
292 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now