<yancheng.cheok@gmail.comwrote in message
news:1166037604.056258.257380@73g2000cwn.googlegro ups.com...
Quote:
Hello all, I try to figure out what is the sequence when we create and
delete an object. After experiment on my VC++ 2003, I found that here
is the sequence
>
new-constructor-destructor-delete
>
What I am concern is, is this sequence a C++ standard specification? Or
it is compiler dependent?
>
Thank you. Here is the code I use to perform experiment:
>
#include <iostream>
#include <cstdlib>
>
using namespace std;
>
class a
{
public:
a()
{
cout<< "constructor"<< endl;
}
>
~a()
{
cout<< "destructor"<< endl;
}
>
void * operator new(size_t s)
{
cout<< "new"<< endl;
return malloc(s);
}
>
void operator delete(void * mem)
{
cout<< "delete"<< endl;
free(mem);
}
};
>
int main()
{
a* aa = new a();
delete aa;
>
getchar();
}
>
Here is the result:
>
new
constructor
destructor
delete
AFAIK it's the only way it could work. There has to be memory set aside
before the constructor can be called. So it would have to be new ->
constructor
And the destructor has to have memory to run against, so it would have to be
destructor -delete also.