SmartPtrTA
来源:互联网 发布:silverlight for mac 编辑:程序博客网 时间:2024/06/16 16:31
前言
初级智能指针的模板实现
实现
// SmartPtrTA.cpp : Defines the entry point for the console application.//#include "common.h"#include "SmartPtrTA.h"#include "A.h"void fnTest();int main(int argc, char* argv[]) { fnTest();return 0;}void fnTest() { CSmartPtrTA<CA> sp1; CSmartPtrTA<CA> sp2; sp1 = new CA(); sp2 = sp1; sp2->foo();}
/// @file common.h/// @brief 公用头文件#ifndef COMMON_H_20160212#define COMMON_H_20160212#include <stdlib.h>#include <stdio.h>#include <tchar.h>#include <assert.h>#ifndef SAFE_DEL#define SAFE_DEL(x) \if (NULL != (x)) { \ delete (x); \ (x) = NULL; \}#endif // #ifndef SAFE_DEL(x) #endif // #ifndef COMMON_H_20160212
/// @file SmartPtrTA.h/// @brief 初级智能指针的模板实现#ifndef SMARTPTRTA_H_20160212#define SMARTPTRTA_H_20160212template<typename T>class CSmartPtrTA { public: explicit CSmartPtrTA() { m_pObj = NULL; } virtual ~CSmartPtrTA() { SAFE_DEL(m_pObj); } // for sp = new CA(); /// 防止产生CSmartPtrTA临时对象 void operator =(T* pIn) { SAFE_DEL(m_pObj); m_pObj = pIn; } // for sp2 = sp1; void operator =(CSmartPtrTA<T>& src) { SAFE_DEL(m_pObj); m_pObj = src.m_pObj; src.m_pObj = NULL; ///< ! } // sp2->foo(); T* operator ->() { assert(NULL != m_pObj); return m_pObj; } private: T* m_pObj;};#endif // #ifndef SMARTPTRTA_H_20160212
// A.h: interface for the CA class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_A_H__09B0C40A_9AE0_4694_B425_89C6087538CA__INCLUDED_)#define AFX_A_H__09B0C40A_9AE0_4694_B425_89C6087538CA__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include "common.h"class CA {public:CA();virtual ~CA(); inline void foo() { printf("CA::foo\n"); }};#endif // !defined(AFX_A_H__09B0C40A_9AE0_4694_B425_89C6087538CA__INCLUDED_)
// A.cpp: implementation of the CA class.////////////////////////////////////////////////////////////////////////#include "A.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CA::CA(){}CA::~CA(){}
0 0
- SmartPtrTA
- Java核心技术(第8版)学习笔记_继承(1)
- 深入理解Java虚拟机----(八)类加载和执行子系统的应用
- 深入理解Java虚拟机----(九)程序编译和代码优化
- 08 通过比较操作符实现代码优化-DirectX游戏开发初级教程
- bzoj2653 Middle 二分&主席树
- SmartPtrTA
- html5全屏
- makefile 文件编写规则
- PHP Manual 笔记(更新中)
- hdu 5294 Tricks Device
- mysql数据库C API函数mysql_store_result和mysql_use_result的区别
- python实例1--用正则表达式爬取静态网页上的图片
- Java打印杨辉三角形
- 51nod 活动安排问题之二