不多说废话咯,代码里很清晰,学C++已经3年了,当时很多细节都没关注过,现在看effective c++ 发现已经错过了很多经典动作。-_-... class CPoint{ int x,y; public: CPoint(int x,int y) { //cout<<"constructer be call"<<endl; this->x = x; this->y = y; } CPoint(){ x = 1; y = 1; } void operator ++()//return value should be CPoint if u want to use (++point).display() { x++; y++; } friend CPoint operator ++(CPoint &point,int) { CPoint temp = point; point.x++; point.y++; return temp; } CPoint& operator --() { x--; y--; return *this; } CPoint* operator --(int) { CPoint* temp = new CPoint(x,y);//stack memory,it will not be free until you delete or program exit //call new get memory first,and then call the constructor x--; y--; return temp; } CPoint operator +(CPoint point)//(point+point2).display() to do that,you must return CPoint { CPoint temp(x,y); temp.x += point.x; temp.y += point.y; return temp; } CPoint& operator +=(CPoint point) { x += point.x; y += point.y; return *this; } CPoint operator =(CPoint& point)//can not be extend.no friend,no victurl { x += point.x; y += point.y; return *this;//return for method (point = point+point2).display(); } operator double()//with out return value,no friend { return (double)x/y; } void* operator new(size_t size)//must return void*,when you use new CPoint(x,y),it will be call. { cout<<"size:"<<size<<endl; //return malloc(size);//they need memory return ::operator new(size); } void* operator new[](size_t size) { cout<<"new[] size:"<<size<<endl; return ::operator new(size); } void* operator new(size_t size,int num1,char* str) { cout<<"num:"<<num1<<"str:"<<str<<endl; return malloc(size); } void operator delete(void* p,size_t size) { cout<<"call delete"<<size<<endl; //free(p);. ::operator delete(p); } void operator delete(void* p,int num1,char* str)//???? { cout<<"call delete 2"<<endl; free(p); } void operator delete[](void* p,size_t size) { cout<<"call delete[]"<<size<<endl; ::delete[] p;//it will delete all the array } void operator[](int index) { cout<<index<<endl; } ~CPoint() { //cout<<"call destructer"<<endl; } void display() { cout<<"x:"<<x<<"y:"<<y<<endl; } }; class CPointChilden:public CPoint{ int z; public: CPointChilden(int x,int y,int z):CPoint(x,y) { this->z = z; } }; int main(int argc, char* argv[]) { CPoint point(1,1); point.display(); (point++).display(); point.display(); (point--)->display();//内存泄露 point.display(); (--point).display(); CPoint point2(1,2); point = point+point2;//(point+point2).display(); point.display(); CPointChilden point3(7,8,9); point3.display(); point3++; point3.display(); float num = point3;//it will use double() firstly,and use (float); cout<<num<<endl; CPoint* point4 = new CPoint(3,4); delete point4;//delete[] point4; thay are different CPoint* point5 = new(0,"hello world.") CPoint(5,6); delete point5; CPoint* pointArray = new CPoint[3]; delete[] pointArray;//##### cout<<endl; CPoint pointArray2[2];//without call new or new[]; pointArray2[1][2];//2 call operator[],you can also to do point2.operator[](2); pointArray2[1].operator ++();//but operator ++,can not work pointArray2[1].display(); return 0; }