473,498 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting array of derived class

i had a question while i was reading the book "MEC++ chapter 3"

in the book, he says this will not work.

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

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

void delete(BST array[])
{
delete [] array;
}

BalancedBST *balTreeArray = new BalancedBST[10];

deleteArray(balTreeArray);
but with the VIsual C++ 2005 it seems to be worked fine

the set of "BalancedBST" and "BST" is printed 10 times.

did i understand the book wrong way? or it's just because of compiler
dependency?
Dec 30 '07 #1
1 1359
SeniorLee wrote:
i had a question while i was reading the book "MEC++ chapter 3"

in the book, he says this will not work.

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

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

void delete(BST array[])
{
delete [] array;
}

BalancedBST *balTreeArray = new BalancedBST[10];

deleteArray(balTreeArray);
but with the VIsual C++ 2005 it seems to be worked fine

the set of "BalancedBST" and "BST" is printed 10 times.

did i understand the book wrong way?
No.
or it's just because of compiler dependency?
Yes. You have undefined behavior, which on your platform happens to look
fine. Hint: this can be caused by the size of the base and the derived
class being equal so that vtable pointers happen to reside at the right
position; however, even though the undefined behavior could have an
explanation, it is still undefined.
Best

Kai-Uwe Bux
Dec 30 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4763
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
7
1949
by: Rohit | last post by:
Iam writing an application that uses an abstract base class and a derived class that is implementation of the abstract base class. Say I have this piece of code: Derived *ptrToDerived=NULL;...
10
3738
by: amparikh | last post by:
Ok, my question is not about Virtual destructors and why, but more on the significance. Generally we have a virtual destructor in the base class ( and inadvertently in the derived class) so that...
4
13996
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
5
4474
by: nadeem_far | last post by:
Hello All, I have been trying to solve the following issue and it seems like there is no solution available any where and maybe people at Microsoft should may be of some help. I am writing a...
7
1753
by: Ron | last post by:
Hello, I have 4 classes that use 4 DTS packages on 4 different tables. So I have Dim cls1 As New clsDTS1, cls2 As New clsDTS2 Dim cls3 As New clsDTS3, cls4 As New clsDTS4 Each class has a...
4
1313
by: Miro | last post by:
Vb2003, im still learning vb.net but I do not understand my output from this logic. If someone can help me out here. Cor Ligthert, you I believe were on the right track of what Im trying to...
13
1978
by: Jack | last post by:
I have a class called "Base". This class has a protected member variable "m_base" which can be retrieved using the public member function "GetBaseMember". "m_base" is initialized to "1" and is...
3
3726
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
One more for today.... As I add more and more lines to my RichTextBox the array that holds its strings gets bigger and bigger and the vertical scroll bar gets smaller and smaller until the...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
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
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
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...
1
4910
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...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.