A
Alan
Using VC++ (1998) compiler with PFE32 editor in Win2K Pro SP4.
(DigitalMars CD on order. )
The program (below) instantiates a class and then deletes it.
I would have thought that reusing the deleted pointer would
have caused an exception, but it does not - or my coding or
thinking is incorrect?
#include <iostream>
using namespace std;
class A {
int a;
public:
A(): a(0) { cout << " A::A()" << endl; }
~A() { cout << "~A::A()" << endl << endl; }
void print() { cout << "a = " << a << endl; }
void set(const int& aa) { a=aa; print(); }
};
int main() {
A* b = new A; // ptr
A& c = *b; // ref
b->set(10); // test ptr
c.set(5); // test ref
delete b;
try {
A& y = *b; // no problems!
y.set(55); // causes system error (msg in Dos-box)
} catch(...) { cout << "failed" << endl; exit(-1); }
cout << "No failure" << endl; // always gets here
return 0;
}
(DigitalMars CD on order. )
The program (below) instantiates a class and then deletes it.
I would have thought that reusing the deleted pointer would
have caused an exception, but it does not - or my coding or
thinking is incorrect?
#include <iostream>
using namespace std;
class A {
int a;
public:
A(): a(0) { cout << " A::A()" << endl; }
~A() { cout << "~A::A()" << endl << endl; }
void print() { cout << "a = " << a << endl; }
void set(const int& aa) { a=aa; print(); }
};
int main() {
A* b = new A; // ptr
A& c = *b; // ref
b->set(10); // test ptr
c.set(5); // test ref
delete b;
try {
A& y = *b; // no problems!
y.set(55); // causes system error (msg in Dos-box)
} catch(...) { cout << "failed" << endl; exit(-1); }
cout << "No failure" << endl; // always gets here
return 0;
}