
On Sep 29, 2009, at 1:56 AM, Gennadiy Rozental wrote:
Peter Klotz
writes: 1) Why are you insisting on casting tu.second to test_case*?
Actually you want your derived class destroyed but tu.second is a base class pointer of type test_unit*. You can simply call "delete tu.second" if the test_unit destructor is virtual. The whole casting thing is in my opinion just a workaround for not making the destructor virtual.
I disagree. The whole idea is that I do NOT use these pointers polymorphically. And never intended to. This is just a storage for proper memory management, where I've decided to use one array instead of two and thus keep them as test_unit*, knowing that I have correct type at runtime anyway.
Making the destructor virtual is only make sense if there are other virtual function (and even in this case there is exception). test_unit has none. Also don't forget that making needlessly class destructor virtual you immediately increase instance size by 4 or 8
I see your point, but by not using the pointer polymorphically to delete the derived class objects, you now need to write a workaround for how some compilers are implemented. But if the base class destructor were virtual, the delete would be guaranteed to work by the language definition, regardless of the compiler implementation. You wouldn't care how the compiler implemented it, it would just work. And it will still just work as compilers and runtime libraries for C++ evolve. That seems to make more sense to me. I think the size argument is less important today than it might have been in the past. Any user of Boost has bought into a ton of template classes and methods that generate a lot of code and produce large programs. We've all got lots of memory. And even if I had a million test cases, that just costs me an extra 4-8 Mb, and even then it's probably only in my test environment, not my released product. I'll trade that to gain reliability any day. - Rush