#include<iostream>
using namespace std;
class SmartPointer
{
private:
int *ptr;
public:
SmartPointer(int *p);
//int operator &();
//int operator *();
int &operator *() { return *ptr; }
~SmartPointer();
};
SmartPointer::SmartPointer(int *p = NULL)
{
cout<<"Initilaize ABC"<<endl;
ptr = p;
}
SmartPointer::~SmartPointer()
{
cout<<"De-Initilaize ABC"<<endl;
delete ptr;
}
/*
int SmartPointer:: operator &()
{
return *ptr;
}
int SmartPointer:: operator *()
{
return *ptr;
}
*/
int main()
{
int iNumber = 10;
//int *ptrToNumber;
SmartPointer a(new int());
*a = 10;
cout<<"value in a:"<<*a<<endl;
cout<<"Address of a:"<<&a<<endl;
return 0;
}
上面是智能指针的代码。但是我没有得到很少的东西。
1。 int &operator *() { return *ptr; }
我不明白。
int &
此功能的返回值是指int
operator *()
覆盖Unary *
(Derference)操作员。
{ return *ptr; }
返回指向的内容。