⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sonyhandle.h

📁 C++程序设计教程(第二版)课件以及源代码 是初学者接触并能很好理解的参考书
💻 H
字号:
//=====================================
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -