标签:ima pre return name 文件 int images src code
代码示例:
非虚析构的情况下.
#include <stdio.h> #include <iostream> using namespace std; class A { public: A() { std::cout << "A is created." << std::endl; } ~A() { std::cout << "A is deleted." << std::endl; } }; class B : public A { public: B() { std::cout << "B is created." << std::endl; } ~B() { std::cout << "B is deleted." << std::endl; } }; int main() { A* pA = new B(); delete pA; return 0; }
虚析构的情况下.
#include <stdio.h> #include <iostream> using namespace std; class A { public: A() { std::cout << "A is created." << std::endl; } virtual ~A() { std::cout << "A is deleted." << std::endl; } }; class B : public A { public: B() { std::cout << "B is created." << std::endl; } ~B() { std::cout << "B is deleted." << std::endl; } }; int main() { A* pA = new B(); delete pA; return 0; }
如果在类型B中创建了一些资源,比如文件句柄、内存等,在这种情况下都得不到释放,从而导致资源泄漏。
标签:ima pre return name 文件 int images src code
原文地址:http://www.cnblogs.com/dsdr/p/6087115.html