zoukankan      html  css  js  c++  java
  • 一个具有对象计数功能的基类

      参考<<More Effective C++>>一书中的条款26:"限制某个class所能产生的对象数量",参照"一个用来计算对象个数的基类"中的代码,简单的将里面的代码实现。一个具有对象计数功能的基类,简单来说,就是完成一个base class,作为对象计数之用,并让诸如Printer之类的class继承它。更多内容请参考<<More Effective C++>>一书中的条款26,那里有很详细的描述,这里不再讲解。下面来看看代码是如何实现的。

      代码如下:

    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    //一个具有对象计数功能的基类
    template<class BeingCounted>
    class Counted
    {
    public:
        class TooManyObjects{};   //定义一个异常类
        static int objectCount()
        {
            return numObjects;
        }
    protected:
        Counted();
        Counted(const Counted& rhs);
        ~Counted()
        {
            cout<<"销毁第 "<<numObjects<<" 个实例"<<endl;
            --numObjects;
        }
    private:
        static int numObjects;           //当前产生实例的个数
        static const size_t maxObjects;  //可以产生实例的最多个数
    
        void init();
    };
    
    template<class BeingCounted>
    Counted<BeingCounted>::Counted()
    {
        init();
    }
    
    template<class BeingCounted>
    Counted<BeingCounted>::Counted(const Counted& rhs)
    {
        init();
    }
    
    template<class BeingCounted>
    void Counted<BeingCounted>::init()
    {
        if (numObjects >= maxObjects)  //产生的实例大于最大值就抛出异常
        {
            throw TooManyObjects();
        }
        cout<<"构造第 "<<numObjects + 1<<" 个实例"<<endl;
        ++numObjects;
    }
    
    //继承自具有对象计数功能的基类
    class Printer : private Counted<Printer>
    {
    public:
        static  Printer* makePrinter();
        static  Printer* makePrinter(const Printer& rhs);
    
        ~Printer();
        using Counted<Printer>::objectCount;      //使基类的objectCount()在子类可见
        using Counted<Printer>::TooManyObjects;   //使基类的TooManyObjects在子类可见
    private:
        Printer();
        Printer(const Printer& rhs);
    };
    
    template<class BeingCounted>
    int Counted<BeingCounted>::numObjects;
    
    const size_t Counted<Printer>::maxObjects = 5;   //最多5个
    
    Printer::Printer()
    {
    }
    
    Printer::~Printer()
    {
    }
    
    Printer::Printer(const Printer& rhs)
    {
    }
    
    Printer* Printer::makePrinter()
    {
        return new Printer;
    }
    
    Printer* Printer::makePrinter(const Printer& rhs)
    {
        return new Printer(rhs);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        Printer *p1 = Printer::makePrinter();
        Printer *p2 = Printer::makePrinter();
        Printer *p3 = Printer::makePrinter(*p1);
        Printer *p4 = Printer::makePrinter(*p2);
        Printer *p5 = Printer::makePrinter();
    
        delete p4;
        delete p5;
        p4 = Printer::makePrinter();
        p5 = Printer::makePrinter();
        try
        {
            p1 = Printer::makePrinter(); //Printer产生的实例大于5,则抛出异常
        }
        catch (Printer::TooManyObjects)
        {
            cout<<"TooManyObjects"<<endl;
        }
    
        delete p1;
        delete p2;
        delete p3;
        delete p4;
        delete p5;
        cout<<"剩余实例为:"<<Printer::objectCount()<<endl;
    return 0; }

      运行界面如下:

                                          

  • 相关阅读:
    linux设备驱动学习笔记(1)
    linux 设备驱动程序中的一些关联性思考
    linux——(2)文件权限与目录配置
    linux——(1)初识linux
    设计模式-状态模式(State Pattern)
    设计模式-组合模式(Composite Pattern)
    设计模式-迭代器模式(Iterator Pattern)
    设计模式-模板方法模式(the Template Method Pattern)
    设计模式-外观模式(Facade Pattern)
    设计模式-适配器模式(Adapter Pattern)
  • 原文地址:https://www.cnblogs.com/venow/p/2728431.html
Copyright © 2011-2022 走看看