zoukankan      html  css  js  c++  java
  • 13.7 手柄(Handle) 简单

    //13.7.1 对像指针问题(Object Pointer Problem)
    //13.7.2 对像指针的外套(The Coat of Object Pointer)
    //Sony Handle的应用

    /*#include "createsony.h"
    #include "sonyhandle.h"
    #include <iostream>
    enum{PUREFLAT, PLASMA, LIQUIDCRYSTAL, NANOMETER};
    
    void fsh(SonyHandle& sh)
    {
    sh->adjustVolume();
    sh->switchChannel();
    }
    
    void createSonyObject(CreateSony* sp)
    {
    SonyHandle sh29(createSonyInch29(sp));
    SonyHandle sh34(createSonyInch34(sp));
    fsh(sh29);
    fsh(sh34);
    }
    
    int main()
    {
    if(CreateSony* sp = createCreateSony(PLASMA)){
    createSonyObject(sp);
    delete sp;
    }
    system("pause");
    return 0;
    }*/
    
    
    #include "createsony.h"
    #include "sonyhandle2.h"
    #include <iostream>
    enum{PUREFLAT, PLASMA, LIQUIDCRYSTAL, NANOMETER};
    
    void fsh(SonyHandle2& sh)
    {
    	sh->adjustVolume();
    	sh->switchChannel();
    }
    
    void createSonyObject(CreateSony* sp)
    {
    	SonyHandle2 sh29(createSonyInch29(sp));
    	SonyHandle2 sh34(createSonyInch34(sp));
    	fsh(sh29);
    	fsh(sh34);
    }
    
    int main()
    {
    	if(CreateSony* sp = createCreateSony(NANOMETER)){
    	    createSonyObject(sp);
    		delete sp;
    	}
        system("pause");
    	return 0;
    }
    

      

    #ifndef HEADER_SONYHANDLE
    #define HEADER_SONYHANDLE
    #include "sony2.h"
    
    class SonyHandle
    {
    	Sony* sp;
    public:
    	Sony* operator->(){ return sp;}
    	SonyHandle(Sony* pp) : sp(pp){};
    	~SonyHandle(){}
    };
    #endif;
    

      

    #ifndef HEADER_SONYHANDLE2
    #define HEADER_SONYHANDLE2
    #include "sony2.h"
    
    class SonyHandle2
    {
    	Sony* sp;  //定义一个Sony的指针
    	int* count;//定义一个整形变量
    public:
    	
    	SonyHandle2(Sony* pp) : sp(pp),count(new int(1)){}; 
    	SonyHandle2(const SonyHandle2& sh):sp(sh.sp),count(sh.count){ (*count)++; }
    
    
    	Sony* operator->(){ return sp;}
    	SonyHandle2& operator+(const SonyHandle2& sh){
    	    if(sh.sp == sp) return *this; //本来就指向同一个对像的情况
    		(*this).~SonyHandle2();
    		sp = sh.sp;
    		count = sh.count;
    		(*count) ++;
    		return *this;
    	}
    	
    	
    	//SonyHandle(Sony* pp) : sp(pp){};
    	~SonyHandle2(){
    	    if(--(*count)==0)
    		{
    			delete sp;
    			delete count;
    		}
    	}
    };
    #endif;
    

      

  • 相关阅读:
    IntellJ IDEA 使用技巧之组件窗口设置
    记springboot + MP +Hikari动态数据源配置
    manjaro升级后_sogou输入法异常
    python_mysql库安装问题
    通讯录制作(.csv文件转.vcf文件即vcard格式)
    win10_bat _运行python程序
    yield()返回参数函数使用
    python交换两个整型数据的数值
    Mongodb 4.0+安装
    C# winform 记住密码实现代码
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2357784.html
Copyright © 2011-2022 走看看