473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Virtual Destructors

This is a strange difference I noted between Borland 5.5 compiler and the VC++ 7.0 compiler.

The following program crashes when compiled and executed using a VC++ 7.0 compiler , but DOES NOT crash when using the Borland C++ 5.5 compiler.

ANYBODY ANY CLUES ?

#include <iostream.h>

class A
{
public:
A() { cout << "A::A()" << endl; }
~A() { cout << "~A::A()" << endl; }
};

class B : public A
{
public:
B() { cout << "B::B()" << endl; }
virtual ~B() { cout << "~B::B()" << endl; }
};

void main()
{
A* a = new B;
delete a;
}
Apr 30 '07 #1
4 1550
weaknessforcats
9,208 Expert Mod 8TB
Virtual destructors must be virtual in the base class.

You need a virtual destructor in A because you are deleting a pointer to A.

If you don't have this, results are indeterminate.
Apr 30 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Oh, by the way:

iostream.h?

That's the pre-ANSI C++ that went obsolete in Sep 1998.
Apr 30 '07 #3
Banfa
9,065 Expert Mod 8TB
void main()
Main needs to be declared as returning int otherwise you are invoking undefined behaviour, anything may happen.
Apr 30 '07 #4
Oh, by the way:

iostream.h?

That's the pre-ANSI C++ that went obsolete in Sep 1998.
Sorry about that. Actually , this was copied and pasted from the Editplus window in which I used the Borland compiler.
But , why does this program not CRASH on Borland 5.5 ? That is the strange part.
If it is to be indeterminate , then shouldn't it be a behaviour seen across all C++ Compilers ?
I think there is something strange happening with the vtable layout in this case. Will examine more.
May 1 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chunhui Han | last post by:
Hi, I was recently reading about virtual base classes in C++. The book I was reading says that it is illegal to have non-virtual destructor for the virtual base class. It seems to me that...
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
23
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
11
by: Bonj | last post by:
Hello, Can anyone help me with these fairly simple questions. 1) What is the point in virtual destructors - I've heard it's a good thing for base-classes, but what are the advantages and...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
4
by: Zeng | last post by:
I often run into situation where I would like to have a method such as that has derived behavior similar to destructors in c++, is that possible? public BaseClass { private int m_dataInBase;...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
9
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.