|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|