#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; }
我上面不明白。
答案 0 :(得分:1)
int &
此函数的返回值是int
operator *()
覆盖一元*
(取消引用)运算符。
{ return *ptr; }
返回指出的内容。