A
aka
// classA.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
static unsigned int count = 0;
class A
{
public:
A() {
p = this;
}
~A() {
if (p) {
cout << count++ << endl;
delete p;
p = 0;
}
}
private:
A *p;
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
return 0;
}
/* compiler: MSC++ compiler in visual c++ 2005 express edition beta
command: cl /nologo /EHsc classA.cpp
*/
/* result:
0
1
2
...
23485
press any key to continue
*/
/*
question: Is it an undefined behavior in C++ Standard?
details: p is a point of class A, when destructor call 'delete p',it will
cause destructor again.
Is there any limit calling destructor recursively?
*/ÿÿÿ
//
#include "stdafx.h"
#include <iostream>
using namespace std;
static unsigned int count = 0;
class A
{
public:
A() {
p = this;
}
~A() {
if (p) {
cout << count++ << endl;
delete p;
p = 0;
}
}
private:
A *p;
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
return 0;
}
/* compiler: MSC++ compiler in visual c++ 2005 express edition beta
command: cl /nologo /EHsc classA.cpp
*/
/* result:
0
1
2
...
23485
press any key to continue
*/
/*
question: Is it an undefined behavior in C++ Standard?
details: p is a point of class A, when destructor call 'delete p',it will
cause destructor again.
Is there any limit calling destructor recursively?
*/ÿÿÿ