{
scpp::RefCountPtr<Student> smartPoint(new Student(STUDENTAGE));
scpp::RefCountPtr<Student> smartPoint2;
smartPoint2 = smartPoint; //@test:赋值操作符
smartPoint = NULL;
smartPoint2->ShowAge();
scpp::RefCountPtr<Student> smartPoint3(smartPoint2); //@test:拷贝构造函数
smartPoint2 = NULL;
smartPoint3->ShowAge();
scpp::RefCountPtr<Student> *smartPoint4; //@test:引用
smartPoint4 = &smartPoint3;
smartPoint4->Get()->ShowAge();
Student studen3 = *smartPoint3; //@test:Get()
studen3.ShowAge();
smartPoint3 = NULL;
Student *student4 = new Student(STUDENTAGE); //@使用方式
scpp::RefCountPtr<Student> smartPoint5;
smartPoint5 = student4;
smartPoint5->ShowAge();
return 0;
}
作用域指针:
如果不打算复制智能指针(因此拷贝构造函数和赋值操作符被声明为私有),只是想保证被分配的资源将被正确地回收时使用这种方式,将会减少计数指针保存技术的int*的空间
scpp_scopedptr.h:
[cpp]
#ifndef __SCCP_SCOPEDPTR_H__
#define __SCCP_SCOPEDPTR_H__
#include "scpp_assert.h"
namespace scpp
{
template <typename T>
class ScopedPtr
{
public:
explicit ScopedPtr(T* p = NULL)
: ptr_(p)
{
}
ScopedPtr<T>& operator = (T* p)
{
if (ptr_ != p)
{
delete ptr_;
ptr_ = p;
}
return *this;
}
~ScopedPtr()
{
delete ptr_;
}
T* operator -> () const
{
SCPP_TEST_ASSERT(ptr_ != NULL, "Attempt to use operator -> on NULL pointer.");
return ptr_;
}
T& operator * () const
{
SCPP_TEST_ASSERT(ptr_ != NULL, "Attempt to use operator -> on NULL pointer.");
return *ptr_;
}
T* Release()
{
T* p = ptr_;
ptr_ = NULL;
return p;
}
private:
T* ptr_;
ScopedPtr(const ScopedPtr<T>& rhs);
ScopedPtr<T>& operator = (const ScopedPtr<T>& rhs);
};
} // namespace scpp
#endif // __SCPP_SCOPEDPTR_HPP_INCLUDED__
测试代码(vs2012+win7环境):
[cpp]
#include "stdafx.h"
#include "scpp_assert.h"
#include "iostream"
#include "scpp_vector.h"
#include "scpp_array.h"
#include "scpp_matrix.h"
#include "algorithm"
#include "scpp_types.h"
#include "scpp_refcountptr.h"
#include "scpp_scopedptr.h"
#define STUDENTAGE 10
class Student
{
public:
Student(int age) : age_(age)
{
}
void ShowAge()
{
std::cout 《 "my age is : " 《 age_ 《 std::endl;
}
private:
int age_;
};
int _tmain(int argc, _TCHAR* argv[])
{
scpp::ScopedPtr<Student> smartPoint(new Student(STUDENTAGE)); //@test:构造函数
smartPoint->ShowAge();
scpp::ScopedPtr<Student> smartPoint2; //@test:实体类的赋值操作
Student *student = new Student(STUDENTAGE); //@使用方式
smartPoint2 = student;
smartPoint2->ShowAge(); //@test:重载->
(*smartPoint2)。ShowAge(); //@test:重载*
scpp::ScopedPtr<Student> *smartPoint3; //@test:Release()
smartPoint3 = &smartPoint2;
Student *students2;
students2 = smartPoint3->Release(); //释放smartpoint保存原来的地址
students2->ShowAge();
return 0;
}
综合两个smart point的例子,使用方式都是"每次使用new操作符创建一个对象时,立即把结果赋值给一个智能指针".
以上的两个smart point其实都没解决一个问题,就是当我们的对象是const的情况,怎么办?分析一下这种情况,传入smart point的对象是一个const,这以为这里面的一些成员变量是无法修改的,所以有了下面的一种半智能的smart point:
scpp_ptr.h:
[cpp]
#ifndef __SCCP_PTR_H__
#define __SCCP_PTR_H__
#include "scpp_assert.h"
namespace scpp
{
template <typename T>
class Ptr
{
public:
explicit Ptr(T *p = NULL)
: ptr_(p)
{
}
T* Get() const
{
return ptr_;
}
Ptr<T>& operator = (T *p)
{
ptr_ = p;
return *this;
}
T& operator * () const