构造和析构

class CA
{
public:
	CA() { std::cout<<"CA construction"<<std::endl;};
	~CA(){ std::cout<<"CA destruction"<<std::endl;}; // 无virtual
};

class CB : public CA
{
public:
	CB() { std::cout<<"CB construction"<<std::endl;}
	~CB(){ std::cout<<"CB destruction"<<std::endl;}
};

void GetValueMax()
{
	CB b;  
}

int main()
{
	GetValueMax();

	std::cout<<std::endl<<std::endl;

	CA* pA;
	pA = new CB;
	delete pA;
}

输出:

CA construction

CB construction

CB destruction

CA destruction

 

CA construction

CB construction

CA destruction

如果CA的析构函数声明为virtual,则会调用CB的析构函数。

上一篇:Javascript中的this,call,apply,bind函数


下一篇:随机函数与概率设置