zoukankan
html css js c++ java
自己写的智能指针源码
//
-------------------------------------------------
//
Smart Pointer
//
-------------------------------------------------
#ifndef _SMART_POINTER_H
#define
_SMART_POINTER_H
extern
std::list
<
short
>
ptrCounter;
template
<
class
T
>
class
smartPtr
{
private
:
T
*
ptr;
short
*
counter;
#ifdef _DEBUG
#define
_DEBUG_FILE_NAME_LEN 32
#define
_DEBUG_RECORD_LEN 32
typedef
struct
__debug_record
{
char
file[_DEBUG_FILE_NAME_LEN];
int
line;
}
debug_record;
public
:
debug_record ptr_changed_rec[_DEBUG_RECORD_LEN];
int
ptr_changed_time;
#endif
//
_DEBUG
public
:
#ifdef _DEBUG
smartPtr(LPCSTR file
=
"
Construction By Default
"
,
int
line
=
__LINE__)
{
#else
smartPtr()
{
#endif
//
_DEBUG
ptr
=
new
T;
ptrCounter.push_back(
1
);
counter
=
&
ptrCounter.back();
#ifdef _DEBUG
memset(ptr_changed_rec,
0
,
sizeof
(debug_record)
*
_DEBUG_RECORD_LEN);
strncpy(ptr_changed_rec[
0
].file,file,_DEBUG_FILE_NAME_LEN);
ptr_changed_rec[
0
].line
=
line;
ptr_changed_time
=
1
;
#endif
//
_DEBUG
}
#ifdef _DEBUG
smartPtr(
const
T
*
np,LPCSTR file
=
"
Construction By Pointer
"
,
int
line
=
__LINE__)
{
#else
smartPtr(
const
T
*
np)
{
#endif
//
_DEBUG
ptr
=
const_cast
<
T
*>
(np);
ptrCounter.push_back(
1
);
counter
=
&
ptrCounter.back();
#ifdef _DEBUG
memset(ptr_changed_rec,
0
,
sizeof
(debug_record)
*
_DEBUG_RECORD_LEN);
strncpy(ptr_changed_rec[
0
].file,file,_DEBUG_FILE_NAME_LEN);
ptr_changed_rec[
0
].line
=
line;
ptr_changed_time
=
1
;
#endif
//
_DEBUG
}
smartPtr(
const
smartPtr
<
T
>
&
np)
{
ptr
=
np.ptr;
counter
=
np.counter;
(
*
counter)
++
;
#ifdef _DEBUG
LPCSTR file
=
"
Construction By Copying
"
;
int
line
=
__LINE__;
memset(ptr_changed_rec,
0
,
sizeof
(debug_record)
*
_DEBUG_RECORD_LEN);
strncpy(ptr_changed_rec[
0
].file,file,_DEBUG_FILE_NAME_LEN);
ptr_changed_rec[
0
].line
=
line;
ptr_changed_time
=
1
;
#endif
//
_DEBUG
}
void
operator
=
(
const
smartPtr
<
T
>
&
np)
{
if
(counter
!=
NULL)
{
(
*
counter)
--
;
if
((
*
counter)
<=
0
)
delete ptr;
}
ptr
=
np.ptr;
counter
=
np.counter;
(
*
counter)
++
;
#ifdef _DEBUG
LPCSTR file
=
"
Value Assignment
"
;
int
line
=
__LINE__;
strncpy(ptr_changed_rec[ptr_changed_time].file,file,_DEBUG_FILE_NAME_LEN);
ptr_changed_rec[ptr_changed_time].line
=
line;
ptr_changed_time
++
;
#endif
//
_DEBUG
}
~
smartPtr()
{
(
*
counter)
--
;
if
((
*
counter)
<=
0
)
delete ptr;
}
T
*
operator
->
()
{
return
ptr;
}
T
*
real()
{
return
ptr;
}
}
;
#endif
//
_SMART_POINTER_H
查看全文
相关阅读:
根据数组对象中的某个属性值排序
vue小知识
vue项目中config文件中的 index.js 配置
小问题
原生无缝轮播
webpack打包提交代码
echarts
面试问题
MySql
vue-router 跳转原理
原文地址:https://www.cnblogs.com/len3d/p/234612.html
最新文章
零基础学python-12.1 while循环
如何比较自定义类型相等
如何判断字符串类型相等
如何判断int类型相等
如何将String转换为int
如何将int整型转换成String字符串类型
变量名和方法名
方法的命名方式
java数据类型
变量和常量
热门文章
Session保存数据
数据结构与算法复杂度分析
数组的sort方法原理
nodemon运行 提示错误:无法加载文件 C:UsersgxfAppDataRoaming pm odemon.ps1,因为在此系统上禁止运行脚本。
node学习笔记 http模块和url模块
html-withimg-loader打包html中图片出错
多入口项目中引用mui报错 "export 'default' (imported as 'mui') was not found in '../publicjs/mui'
Tapable.plugin is deprecated. Use new API on `.hooks` instead
又学到了一个骚操作,抓紧记在我的小本本上!
(剑指offer)丑数 JavaScript解法
Copyright © 2011-2022 走看看