设计模式之装饰模式(C++实现)

#define _CRTDBG_MAP_ALLOC  
#ifdef  _DEBUG  
#include <crtdbg.h> //
#include <iostream>//这两个头文件也必须放在#ifdef #endif 之间
#define new   new(_NORMAL_BLOCK, __FILE__, __LINE__)   //必须这么写才能看到内存泄漏处的文件名和行号
#endif  
#include <vector>
#include <string> //这两个可以随便放


using namespace std;

// base class
class Beverage
{
public:
	virtual string getDescription() = 0;
	virtual float cost() = 0;
	virtual ~Beverage()  //凡是涉及到多态,父类虚函数必须为virtual
	{
		cout <<"调用Beverage的虚析构函数" << endl;
	}
};

// base class of condiment, also a base decorator class
class CondimentDecorator : public Beverage
{
	public:
	Beverage *m_coffee;

	CondimentDecorator()
	{
		m_coffee = NULL;  //这步很重要
	}

	~CondimentDecorator()   //此时它为virtual,尽管没显示声明
	{
		cout << "调用CondimentDecorator的析构函数" << endl;
		delete m_coffee;
		m_coffee = NULL;


	}
};

// coffee DarkRoast
class DarkRoast: public Beverage
{
public:
	string getDescription()
	{
		return "Darkroast";
	}
	float cost()
	{
		return 5.0f;
	}
	~DarkRoast()  //此时它为virtual,尽管没显示声明
	{
		cout << "调用DarkRoast的析构函数" << endl;
	}
};

// coffee Espresso
class Espresso:  public Beverage
{
public:
	string getDescription()
	{
		return "Espresso";
	}
	float cost()
	{
		return 6.5f;
	}
	~Espresso()  //此时它为virtual,尽管没显示声明
	{
		cout << "调用Espresso的析构函数" << endl;
	}
};

// condiment milk
class Milk:  public CondimentDecorator
{
public:
	Milk() {}
	Milk(Beverage *coffee)
	{
		m_coffee = coffee;
	}
	 ~Milk()   //此时它为virtual,尽管没显示声明
	{
		cout << "调用Milk的析构函数" << endl;
		delete m_coffee;
	    m_coffee = NULL;

	}
	string getDescription()
	{
		return m_coffee->getDescription() + ", Milk";
	}
	float cost()
	{
		return m_coffee->cost() + 1.0f;
	}
};

// condiment mocha
class Mocha:  public CondimentDecorator
{
public:
	Mocha()
	{}
	Mocha(Beverage *coffee)
	{
		m_coffee = coffee;
	}
	~Mocha()//此时它为virtual,尽管没显示声明
	{
		cout << "调用Mocha的析构函数" << endl;
        delete m_coffee;
	    m_coffee = NULL;

	}
	string getDescription()
	{
		return m_coffee->getDescription() + ", Mocha";
	}
	float cost()
	{
		return m_coffee->cost() + 0.5f;
	}
};

class DMilk: public Milk
{
public:
	DMilk()
	{
		cout << "调用DMilk构造函数" << endl;
	}
	~DMilk()  //此时它为virtual,尽管没显示声明
	{
		cout << "调用DMilk析构函数" << endl;
	}
};

int main()
{

	Beverage *coffee = new Espresso;
 	coffee = new Milk(coffee);
 	coffee = new Mocha(coffee);
	cout<<coffee->getDescription()<<" $"<<coffee->cost() << endl;
	delete coffee;	
	coffee = NULL;

	Milk *milk = new DMilk;
	delete milk;
	milk = NULL;
	_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
	_CrtDumpMemoryLeaks();
	return 0;
}


 

一个类维护一个虚函数相关的表--vtable(__vfptr指向它),函数声明前面包含关键字“virtual”的函数,就会创建一个指向该函数的指针(函数指针)被存入vtable中。

虚函数表的作用是用来实现多态,但同时也带来了执行效率和额外内存空间的增加。

 

再来看虚析构函数,它所存在的意义:基类的指针指向派生类对象,用基类的指针删除派生类对象。

#include <iostream>
using namespace std;

class A
{
public:
    A()
    {
        cout <<"A..."<<endl;
    }
    ~A()
    {
        cout <<"~A..."<<endl;    
    }
};

class B :public A
{
public :
    B()
    {
        cout <<"B..."<<endl;
    }
    ~B()
    {
        cout <<"~B..."<<endl;
        
    }    
};

int main()
{
    A *a = new B();
    delete a;

    return 0 ;
}

输出:

A…

B…

~A…

派生类的析构函数未被调用,为什么呢?

派生类继承自基类,那么基类就只会存在于派生类中,直到派生类调用析构函数后。

假定:基类的析构函数调用比派生类要早,会造成的一种情况就是类成员不存在了,而类本身却还在,但是类存在的情况下,类成员应该还存在。所以这就矛盾了,所以派生类的析构函数会先被调用,基类的析构函数再被调用。

修改一下代码:

#include <iostream>
using namespace std;

class A
{
public:
    A()
    {
        cout <<"A..."<<endl;
    }
    virtual ~A()
    {
        cout <<"~A..."<<endl;    
    }
};

class B :public A
{
public :
    B()
    {
        cout <<"B..."<<endl;
    }
    ~B()
    {
        cout <<"~B..."<<endl;
        
    }    
};

int main()
{
    A *a = new B();
    delete a;

    return 0 ;
}

输出:

A…

B…

~B…

~A…

仅仅只是在基类的析构函数前面加了一个“virtual”,使它成为“虚析构函数”了,这就是“虚析构函数”存在的意义 :)

 

析构函数的作用并不是删除对象,而是撤销对象占用内存之前完成的一些清理工作…

//=========================================

总结:如果某个类不包含虚函数,那一般是表示它将不作为一个基类来使用。当一个类不准备作为基类使用时,就不要定义虚析构函数了,因为它会增加一个虚函数表,使得对象的体积翻倍,还有可能降低其可移值性。

所以基本的一条是:无故的声明虚析构函数和永远不去声明一样是错误的。

当且仅当类里包含至少一个虚函数的时候,才去声明虚析构函数。

抽象类是准备被用做基类的,基类必须要有一个虚析构函数,纯虚函数会产生抽象类,所以在想要成为抽象类的类里声明一个纯虚析构函数。

定义一个函数为虚函数,不代表该函数未被实现,只是为了来实现多态。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值