一个具有对象计数功能的基类
2017-12-21 15:36
211 查看
源自more effective c++ item M26:
目的:
为了做到一个类具有对类对象数量限制功能及不再派生新的子类;做法是继承一个计数功能的基类,并将自身构造私有化。过程是Printer在进行构造的时候,会先进行基类的构造,在构造时会调用init()函数,函数会判定是否已经到达限制,若到达限制就进行throw,否则就继续进行构造。
代码:
目的:
为了做到一个类具有对类对象数量限制功能及不再派生新的子类;做法是继承一个计数功能的基类,并将自身构造私有化。过程是Printer在进行构造的时候,会先进行基类的构造,在构造时会调用init()函数,函数会判定是否已经到达限制,若到达限制就进行throw,否则就继续进行构造。
代码:
#include<iostream> #include<exception> #include<Error.h> using namespace std; template <typename BeingCounted> class Counted{ public: class TooMany :public exception{//抛异常 public: TooMany():exception("the number is enough...."){} }; static int objectCount(){return numobject;}//声明静态确保所有对象共享 protected: Counted(); Counted(const Counted &rhs); ~Counted(){--numobject;} private: static int numobject; static const size_t maxobject; void init();//使用init统一调配,避免构造语句的重复 }; template<typename BeingCounted> Counted<BeingCounted>::Counted(){ init(); } template<typename BeingCounted> Counted<BeingCounted>::Counted(const Counted&rhs){ init(); } template<typename BeingCounted> void Counted<BeingCounted>::init(){ if(numobject>=maxobject)throw TooMany(); ++numobject; } class Printer:private Counted<Printer>{ public: static Printer*makePrinter();//使用静态伪构造方法禁止Printer派生出新的子类 static Printer*makePrinter(const Printer&rhs); ~Printer(); void submitJob(const Printer&job); void reset(); void performSelfTest(); using Counted<Printer>::objectCount; using Counted<Printer>::TooMany;//使TooMany在Printer中权限的为public private: Printer(); Printer(const Printer&rhs); }; Printer *Printer::makePrinter(){ return new Printer; } Printer*Printer::makePrinter(const Printer&rhs){ return new Printer(rhs); } Printer::Printer(){ } Printer::Printer(const Printer&rhs){ } const size_t Counted<Printer>::maxobject=3;//const static对象需要在类外进行初始化。 int Counted<Printer>::numobject=0; int main(void){ try{ Printer *p1=Printer::makePrinter(); cout<<"build success..."<<endl; Printer*p2=Printer::makePrinter(*p1); cout<<"build 9e5a success..."<<endl; Printer*p3=Printer::makePrinter(); cout<<"build success..."<<endl; Printer*p4=Printer::makePrinter(); cout<<"build success..."<<endl; } catch(Printer::TooMany &me){ cout<<me.what(); } system("pause"); return 0; }
相关文章推荐
- 一个具有对象计数功能的基类
- 冰编程对派出类的功能进行验证。。 轿车类等 派生出自行车类 停车等方法。然后从该类为基类 最大速度。重量等字段以及开车 要求具有速度 定义一个车辆类
- 并编程对派出类的功能进行验证。。 轿车类等 派生出自行车类 停车等方法。然后从该类为基类 最大速度。重量等字段以及开车 要求具有速度 定义一个车辆类
- 地磅称量系统之(53)在封装对象的类库中实现包括IDataErrorInfo接口提供的所有方法和并且扩展对异常具有添加和删除功能的基本业务对象基类
- 定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义 Speak()成员函数,基类中定义为虚函数,定义一个Dog类的对象,调用Speak函数,观察运行结果
- 设计:设计一个分数类,具有一下的功能
- 用unity写的 定义一个车辆(Vehicle)基类,具有Run、Stop等方法,具有Speed(速度)、MaxSpeed(最大速度)、Weight(重量)等域。然后以该类为基类,派生出bicycl
- 很实用的一个ext表格,具有很好的分页功能。
- 隐藏main()函数,使用一个全局对象的成员函数替代main()函数的功能
- WPF:一个具有拖入和删除功能的图片框控件
- java第七周实验封装一类对象English,该类对象具有一种功能printEnglish输出英文字母表。再封装一类对象Greek,该类必须是English的子类,该类对象不仅可以调用方法printE
- 定义一个计算机类:属性:品牌;价格;颜色; 方法:(1)编程的功能 (2)上网的功能 实例化两个对象:“lenovo”;“hasee”
- ls命令具有一个-r选项,可以递归的列出子目录中的内容。请编写一个具有同样功能的程序。
- 一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 使用反射将一个对象的值复制给另一个对象(同一个对象或者基于同一个基类)
- (ASP.NET)通过Cookie对象实现页面访问计数功能。
- 在jsp页面,如何让一个button具有submit的功能
- 分享一个通过面向对象方式完成的拖拽功能
- 实现一个具有百度文库文档转换功能的工具类