[Dev-C++] Constructor and Destructor
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Verma, L. <Loh...@hp...> - 2003-07-09 12:42:45
|
Hi
I have a question regarding constructors and destructors, and about class
pointers
If I have a class A (see below), Now the second call crashes
Also I can call a member function without allocating memory for a class
object.
Please see code below and advice.
Thanks a lot
Lohit
#include <iostream>
using namespace std;
class A{
public:
A(){
delete this;
}
~A(){
cout<<"~A";
}
f(){
cout<<"HELLO\n";
}
};
int main()
{
A *a=new A();//works
A b;//crashes why?? how is it any different from above.
A *c;
c->f();//works why when I have not allocated any memory for
this.
}
DISCLAIMER: The information in this message is confidential and may be
legally privileged. It is intended solely for the addressee. Access to this
message by anyone else is unauthorised. If you are not the intended
recipient, any disclosure, copying, or distribution of the message, or any
action or omission taken by you in reliance on it, is prohibited and may be
unlawful. Please immediately contact the sender if you have received this
message in error. Thank you.
|