On Wed, Nov 18, 2009 at 1:30 AM, Tim Kroeger
<[email protected]> wrote:
>
> On Tue, 17 Nov 2009, John Peterson wrote:
>
>> As I understand the way destructors work, if you delete an object of
>> type derived through an object of type pointer-to-base, as long as the
>> base class destructor is declared virtual, the derived class
>> destructor will be called *in addition to* the destructor for *every*
>> intermediate type. Correct?
>
> This is always done, no matter whether the destructor is virtual or not.
I don't think so. After sending that email, I wrote a test code and
in fact the destructor does need to be virtual for the destructor to
travel "all the way" up the chain. Try the test code below with and
without virtual commented...
#include <iostream>
class A
{
public:
/*virtual*/ ~A()
{
std::cout << "Calling ~A()" << std::endl;
}
};
class B : public A
{
public:
/*virtual*/ ~B()
{
std::cout << "Calling ~B()" << std::endl;
}
};
class C : public B
{
public:
/*virtual*/ ~C()
{
std::cout << "Calling ~C()" << std::endl;
}
};
int main()
{
{
A* a = new C;
delete a;
}
return 0;
}
--
John
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Libmesh-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libmesh-devel