Y
yancheng.cheok
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
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