最近遇到一个问题, 于是产生了一些思考。 先看程序:
#include <iostream>
using namespace std;
class A
{
public:
static int* fun();
A()
{
printf("constrct\n");
}
~A()
{
printf("destruct\n");
}
};
int* A::fun()
{
return NULL;
}
int main()
{
A::fun();
A::fun();
return 0;
}
结果显示, 构造函数和析构函数都不会被调用。
为什么呢? 因为调用A::fun()的时候, 并没有任何对象产生和销毁, fun是属于类的, 此时跟对象没有什么bird关系。 所以构造函数和析构函数都不会被调用。