3 顶层父类

1 准则

  • 尽量使用单重继承的方式进行系统设计
  • 尽量保持系统中只存在单一的继承树
  • 尽量使用组合关系代替继承关系

2 顶层父类的创建

  • 创建 DTLib::Object 类的意义

    • 所有数据结构都继承自 Object
    • 统一动态内存申请的行为,提高代码的移植性(自定义 newdelete 操作符)
    • 在堆中创建 Object 子类的对象,失败时返回 NULL 值,而不是抛出标准库中的一个异常出来
    • Object 类为纯虚父类,所有子类都能进行动态类型识别
  • 顶层父类的接口定义

    class Object
    {
    public:
        void* operator new (unsigned int size) throw();
        void operator delete (void* p );
        void* operator new[] (unsigned int size) throw();
        void operator delete[] (void* p);
        virtual ~Object() = 0;
    }
    
  • Demo

    //Object.h
    #pragma once
    namespace DTLib
    {
    	class Object
    	{
        public:
            void* operator new (unsigned int size) throw();
            void operator delete (void* p);
            void* operator new[] (unsigned int size) throw();
            void operator delete[] (void* p);
            virtual ~Object() = 0;
    	};
    }
    
    
    //Object.cpp
    #include "Object.h"
    #include <cstdlib>
    namespace DTLib
    {
        //throw() 为异常规格说明,表明当前这个函数不会抛出任何异常
    	void* Object::operator new (unsigned int size) throw() {
    		return malloc(size);
    	}
    
    	void Object::operator delete (void* p) {
            free(p);
    	}
    
        //数组版本
    	void* Object::operator new[](unsigned int size) throw() {
    		return malloc(size);
    	}
    
    	void Object::operator delete[] (void* p) {
    		free(p);
    	}
    
    	Object:: ~Object() {
    
    	}
    }
    
  • 使用

    • 先修改下 Object.cpp 的代码
    • 可以看到运行结果,说明这里使用的不是C++编译器默认的 newdelete 的实现,而是在顶层父类 Object 中自定义的实现
    //Object.cpp
    #include "Object.h"
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    namespace DTLib
    {
    	//单一版本
    	void* Object::operator new (unsigned int size) throw() {
    		cout << "Object::operator new: " << size << endl;
    		return malloc(size);
    	}
    
    	void Object::operator delete (void* p) {
    		cout << "Object::operator delete: " << p << endl;
    		free(p);
    	}
    
    	//数组版本
    	void* Object::operator new[](unsigned int size) throw() {
    		return malloc(size);
    	}
    
    	void Object::operator delete[](void* p) {
    		free(p);
    	}
    
    	Object:: ~Object() {
    
    	}
    }
    
    //main.cpp
    #include <iostream>
    #include "SmartPointer.h"
    #include "Object.h"
    using namespace std;
    using namespace DTLib;
    
    //定义两个Object子类
    class Test :public Object
    {
    public:
    	int i;
    	int j;
    };
    
    class Child :public Test
    {
    public:
    	int k;
    };
    
    int main()
    {
    	Object* obj1 = new Test();
    	Object* obj2 = new Child();
    
    	//......
    
    	cout << "obj1 = " << obj1 << endl;
    	cout << "obj2 = " << obj2 << endl;
    
    	delete obj1;
    	delete obj2;
    
    	return 0;
    }
    //输出结果
    Object::operator new: 12  //int*2+虚函数标指针
    Object::operator new: 16  //int*3+虚函数标指针
    obj1 = 00A3DB40
    obj2 = 00A3BED8
    Object::operator delete: 00A3DB40
    Object::operator delete: 00A3BED8
    
posted @ 2020-09-06 21:41  nxgy  阅读(113)  评论(0编辑  收藏  举报