main.cpp测试代码

#include "TestSmartPointer"
void fun()
{
SP<TestSmartPointer> sp1=new TestSmartPointer("A");
SP<TestSmartPointer> sp2=sp1;
sp1=sp2;
}
void main()
{
fun();
//system("pause");
}

TestSmartPointer头文件

#pragma once

#include "Referenced"
#include "SmartPointer" #include <string> class TestSmartPointer : public Referenced
{
public:
inline TestSmartPointer(std::string name):Referenced(),_name(name) {}
protected: virtual ~TestSmartPointer() {} std::string _name;
};

Referenced计数器

#ifndef REFERENCED
#define REFERENCED 1 #include <iostream>
using namespace std; /** Base class from providing referencing counted objects.*/
class Referenced
{
public:
Referenced() { _refCount=; }
Referenced(const Referenced&) { _refCount=; }
inline Referenced& operator = (const Referenced&) { return *this; } /** Increment the reference count by one, indicating that
this object has another pointer which is referencing it.*/
inline void ref() const; /** Decrement the reference count by one, indicating that
a pointer to this object is referencing it. If the
reference count goes to zero, it is assumed that this object
is no longer referenced and is automatically deleted.*/
inline void unref() const; /** Decrement the reference count by one, indicating that
a pointer to this object is referencing it. However, do
not delete it, even if ref count goes to 0. Warning, unref_nodelete()
should only be called if the user knows exactly who will
be resonsible for, one should prefer unref() over unref_nodelete()
as the later can lead to memory leaks.*/
inline void unref_nodelete() const; /** Return the number pointers currently referencing this object. */
inline int referenceCount() const { return _refCount; } protected:
virtual ~Referenced()
{
if (_refCount>)
{
cout<<"Warning: deleting still referenced object "<<this<<" of type '"<<typeid(this).name()<<"'"<<std::endl;
cout<<"the final reference count was "<<_refCount<<", memory corruption possible."<<std::endl;
}
}
mutable int _refCount;
}; inline void Referenced::ref() const
{
++_refCount;
} inline void Referenced::unref() const
{
bool needDelete = false;
--_refCount;
needDelete = _refCount<=; if (needDelete)
{
delete this;
}
}
inline void Referenced::unref_nodelete() const
{
--_refCount;
} #endif

SmartPointer智能指针模板类

#ifndef SMART_POINTER
#define SMART_POINTER 1 /** Smart pointer for handling referenced counted objects.*/
template<class T>
class SP
{
public:
typedef T element_type; SP() :_ptr(0L) {}
SP(T* t):_ptr(t) { if (_ptr) _ptr->ref(); }
SP(const SP& rp):_ptr(rp._ptr) { if (_ptr) _ptr->ref(); }
~SP() { if (_ptr) _ptr->unref(); _ptr=; } inline SP& operator = (const SP& rp)
{
if (_ptr==rp._ptr) return *this;
T* tmp_ptr = _ptr;
_ptr = rp._ptr;
if (_ptr) _ptr->ref();
// unref second to prevent any deletion of any object which might
// be referenced by the other object. i.e rp is child of the
// original _ptr.
if (tmp_ptr) tmp_ptr->unref();
return *this;
} inline SP& operator = (T* ptr)
{
if (_ptr==ptr) return *this;
T* tmp_ptr = _ptr;
_ptr = ptr;
if (_ptr) _ptr->ref();
// unref second to prevent any deletion of any object which might
// be referenced by the other object. i.e rp is child of the
// original _ptr.
if (tmp_ptr) tmp_ptr->unref();
return *this;
} // comparison operators for SP.
inline bool operator == (const SP& rp) const { return (_ptr==rp._ptr); }
inline bool operator != (const SP& rp) const { return (_ptr!=rp._ptr); }
inline bool operator < (const SP& rp) const { return (_ptr<rp._ptr); }
inline bool operator > (const SP& rp) const { return (_ptr>rp._ptr); } // comparison operator for const T*.
inline bool operator == (const T* ptr) const { return (_ptr==ptr); }
inline bool operator != (const T* ptr) const { return (_ptr!=ptr); }
inline bool operator < (const T* ptr) const { return (_ptr<ptr); }
inline bool operator > (const T* ptr) const { return (_ptr>ptr); } inline T& operator*() { return *_ptr; } inline const T& operator*() const { return *_ptr; } inline T* operator->() { return _ptr; } inline const T* operator->() const { return _ptr; } inline bool operator!() const { return _ptr==0L; } inline bool valid() const { return _ptr!=0L; } inline T* get() { return _ptr; } inline const T* get() const { return _ptr; } /** take control over the object pointed to by SP, unreference but do not delete even if ref count goes to 0,
* return the pointer to the object.
* Note, do not use this unless you are 100% sure your code handles the deletion of the object correctly, and
* only use when absolutely required.*/
inline T* take() { return release();} inline T* release() { T* tmp=_ptr; if (_ptr) _ptr->unref_nodelete(); _ptr=; return tmp;} private:
T* _ptr;
}; #endif

根据OSG中的ref_ptr和Reference简化的智能指针的更多相关文章

  1. stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结

    stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...

  2. 不可不表的OSG智能指针之强指针与弱指针 《转载》

    不可不表的OSG智能指针之强指针与弱指针 <转载> 使用OSG的人都知道OSG的内存管理方式采用了智能指针,通过智能指针的方式让OSG自己处理对象的销毁工作.在OSG中有两个智能指针类型, ...

  3. C++中智能指针的设计和使用

    转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/7561235 智能指针(smart pointer)是存储指向动态分配(堆 ...

  4. 智能指针 与 oc中的指针

     智能指针 与 oc中的指针 智能指针的原理及实现 当类中有指针成员时,一般有两种方式来管理指针成员:一是采用值型的方式管理,每个类对象都保留一份指针指向的对象的拷贝:另一种更优雅的方式是使用智能指针 ...

  5. OSG中的HUD

    OSG中的HUD 所谓HUD节点,说白了就是无论三维场景中的内容怎么改变,它都能在屏幕上固定位置显示的节点. 实现要点: 关闭光照,不受场景光照影响,所有内容以同一亮度显示 关闭深度测试 调整渲染顺序 ...

  6. osg中使用MatrixTransform来实现模型的平移/旋转/缩放

    osg中使用MatrixTransform来实现模型的平移/旋转/缩放 转自:http://www.cnblogs.com/kekec/archive/2011/08/15/2139893.html# ...

  7. OSG中的示例程序简介

    OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示 ...

  8. OSG中的示例程序简介(转)

    OSG中的示例程序简介 1.example_osganimate一)演示了路径动画的使用 (AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera ...

  9. OSG中相机参数的更改

    #pragma comment(lib, "osg.lib") #pragma comment(lib, "osgDB.lib") #pragma commen ...

随机推荐

  1. POJ 1850 Code

    组合数学.... Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7202 Accepted: 3361 Descrip ...

  2. jquery选择器(一)-基础选择器

    1. ID元素选择器 $("#btn1") 2. class元素选择器 $(".btn") 3. 标签元素选择器 $("div") 4. 全 ...

  3. 第三天 moyax

    struct Blog { static let BaseURL = NSURL(string: "http://192.168.1.103/blog")! } extension ...

  4. saltstack 入门命令

    master服务启动 CentOS 7 (Debian.OpenSuse.Fedora) systemctl start salt-master /etc/init.d/salt-master sta ...

  5. BZOJ1452——[JSOI2009]Count

    1.题目大意: 就是给一个n×m的方格,然后一些平面上的 求和 修改操作 2.分析:二维树状数组裸题 #include <cstdio> #include <cstdlib> ...

  6. CSS3中动画属性transform、transition和animation

    Transform:变形 在网页设计中,CSS被习惯性的理解为擅长表现静态样式,动态的元素必须借助于javascript才可以实现,而CSS3的出现改变了这一思维方式.CSS3除了增加革命性的创新功能 ...

  7. ssh(安全外壳层)

    SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程登录会 ...

  8. 数据结构大二课程设计:QT实现线段树

    源码以及编译文件下载地址:http://download.csdn.net/detail/zhiyanpianyu1234/9445909#comment 加入了一些小东西,一直觉得课设是做给自己看的 ...

  9. c语言数据问题

    变量都有作用域,链接属性,和存储类型3个属性,这三个属性决定了变量的作用域和生存期的问题 在c语言中包含4中类型, 整形 浮点型 指针 聚合类型(数组,结构体等) ------------------ ...

  10. 前端之js的常用用法

    js生成标签 // 将标签添加到i1里面 var tag = document.createElement('input'); tag.setAttribute('type', 'text'); ta ...