重载operator->()可以增加一些附加的操作,比如计数
#include<iostream>
using namespace std;
class Image{
public:
void Draw(int a,int b)
{
cout<<"a="<<a<<"b="<<b<<endl;
}
};
class Imageptr
{
public:
Imageptr(char* name);
virtual ~Imageptr();
void Draw(int a,int b);
void LoadImage();
Image* operator->();//对他进行重载之后,调用的时候先调用这个
Image& operator*();
private:
char* ImageFileName;
Image* _image;
};
Imageptr::Imageptr(char* name)
{
ImageFileName=name;
_image=0;
}
Imageptr::~Imageptr()
{
delete ImageFileName;
}
void Imageptr::LoadImage()
{
_image=new Image();
}
Image* Imageptr::operator->()
{
LoadImage();
}
Image& Imageptr::operator*()
{
LoadImage();
}
void Imageptr::Draw(int a,int b)
{
_image->Draw(a,b);
}
int main()
{
Imageptr p("zhang");
p->Draw(1,2);
system("pause");
return 0;
}