sonyhandle.h

来自「C++程序设计教程(第二版)课件以及源代码 是初学者接触并能很好理解的参考书」· C头文件 代码 · 共 33 行

H
33
字号
//=====================================
// sonyhandle.h
//=====================================
#ifndef HEADER_SONYHANDLE
#define HEADER_SONYHANDLE
//-------------------------------------
#include"sony.h"
//-------------------------------------
class SonyHandle{
  Sony* sp;
  int* count;
public:
  SonyHandle(Sony* pp) : sp(pp), count(new int(1)){}
  SonyHandle(const SonyHandle& sh):sp(sh.sp),count(sh.count){ (*count)++; }
  Sony* operator->(){ return sp; }
  SonyHandle& operator=(const SonyHandle& sh){
    if(sh.sp == sp) return *this;  // 本来就指向同一个对象的情况
    (*this).~SonyHandle();
    sp = sh.sp;
    count = sh.count;
    (*count)++;
    return *this;
  }
 ~SonyHandle(){
    if(--(*count)==0){
      delete sp;
      delete count;
    }
  }
};//-----------------------------------
#endif  // HEADER_SONYHANDLE

 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?