{
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_;
}
~Ptr()
{
}
private:
T* ptr_;
};
} // namespace scpp
#endif // __SCCP_REFCOUNTPTR_H__
测试代码(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"
#include "scpp_ptr.h"
#define STUDENTAGE 10
class Student
{
public:
Student(int age) : age_(age)
{
}
void ShowAge()
{
std::cout 《 "my age is : " 《 age_ 《 std::endl;
}
int GetAge() const
{
return age_;
}
void SetAge(int age)
{
age_ = age;
}
private:
int age_;
};
int _tmain(int argc, _TCHAR* argv[])
{
Student *student1 = new Student(STUDENTAGE); //@test:operator -> (T *p)
scpp::Ptr<Student> smartPoint1;
smartPoint1 = student1;
smartPoint1->ShowAge();
Student *student2 = new Student(STUDENTAGE); //@test:operator * (T *p)
scpp::Ptr<Student> smartPoint2;
smartPoint2 = student2;
(*smartPoint2)。ShowAge();
Student *student3 = new Student(STUDENTAGE); //@test:operator * (T *p)
scpp::Ptr<const Student> smartPoint3;
smartPoint3 = student3;
std::cout 《 "this student age is : " 《 smartPoint3->GetAge() 《 std::endl;
smartPoint3->SetAge(STUDENTAGE); //@因为被限制,所以无法使用,也没必要使用
return 0;
}