hi guys and gals, here i've got a problem and ask for your kindly help.
why can't we delete an array of type t2 by deleting a 'pointer of type t1(p23)' that is actually pointing to 'new t2[2]', whereas we do can delete an object of type t2 by deleting a 'pointer of type t1(p22)'Code:#include <cstdlib> #include <cmath> #include <iosfwd> #include <iostream> #include <iterator> #include <vector> #include <cstring> #include <iostream> #include <stdexcept> #include <string> #include <sstream> using namespace std; class t1 { public: t1() { cout << "t1: " << this << endl; } virtual ~t1() { cout << "~t1: " << this << endl; } int a, b, c; char d, f, g; }; class t2 : public t1 { public: t2() { cout << "t2: " << this << endl; } ~t2() { cout << "~t2: " << this << endl; } int i1, i2, i3; }; int main() { t1* p11 = new t1; delete p11; // OK t1* p12 = new t1[2]; delete [] p12; // OK t2* p21 = new t2[2]; delete [] p21; // OK t1* p22 = new t2; delete p22; // OK t1* p23 = new t2[2]; delete [] p23; // Core Dump, why? }



LinkBack URL
About LinkBacks



CornedBee