⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prg13_5a.cpp

📁 这是数据结构和算法的国外经典书籍.清华大学出版社出版的<数据结构C++语言描述-应用模板库STL>陈君 译 英文名称是Data Structures with C++ Using STL.
💻 CPP
字号:
// File: prg13_5a.cpp
// the program demonstrates the need for a virtual destructor
// in the base class. the base class, baseCL, does not declare
// a virtual destructor, and the derived class, dynDerived,
// allocates dynamic memory. after allocating a dynDerived object
// and storing the pointer in a baseCL pointer, the program calls
// delete which causes only the base class destructor to execute. the
// dynamic memory allocated by the derived object is not removed,
// causing a memory leak

#include <iostream>

using namespace std;

class baseCL
{
	public:
		baseCL()
		{	cout << "baseCL constructor - no action" << endl;}

		~baseCL()		// not a virtual destructor
		{ cout << "baseCL destructor - no action" << endl; }
};

class dynDerived: public baseCL
{
 	public:
		dynDerived() : baseCL()
		{
      	cout << "dynDerived constructor - allocate 4-element array"
				  << endl;
			dArr = new int [4];
		}

		~dynDerived()
		{
      	cout << "dynDerived destructor - deallocate 4-element array"
		        << endl;
      	delete [] dArr;
      }
	private:
		int *dArr; 				// pointer to derived class dynamic array
};

int main()
{
	baseCL *basePtr = new dynDerived;

   delete basePtr;
	
	return 0;
}

/*
Run: (baseCL destructor is not virtual):

In baseCL constructor - no action
In dynDerived constructor - allocate 4-element array
In baseCL destructor - no action
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -