#include "comm.h"
#ifdef _rec_ptr
#include <string>
#include <iostream>
using namespace std;
struct Rect
{
string name;
};
class RectPtr
{
Rect *pr;
public:
RectPtr(): pr(0) {}
Rect* operator->()
{
if(pr == 0)
{
pr = new Rect;
pr->name = "RectPtr::Rect::name";
}
return pr;
}
Rect& operator*()
{
return *pr;
}
~RectPtr()
{
delete pr;
}
};
void test(char * args[], int argc)
{
RectPtr ptr;
cout << "the name initialized: " << ptr->name << '\n';
ptr->name = "icanth"; // == (ptr->operator())->name = "icanth";
cout << "the name modified: " << ptr->name << endl;
}
#endif
运行截图: