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的析构函数。
时间: 2024-10-01 04:25:56