不多說廢話咯,代碼裡很清晰,學C++已經3年了,當時很多細節都沒關注過,現在看effective c++ 發現已經錯過了很多經典動作。-_-...
class CPoint{<br />int x,y;<br />public:<br />CPoint(int x,int y)<br />{<br />//cout<<"constructer be call"<<endl;<br />this->x = x;<br />this->y = y;<br />}<br />CPoint(){<br />x = 1;<br />y = 1;<br />}<br />void operator ++()//return value should be CPoint if u want to use (++point).display()<br />{<br />x++;<br />y++;<br />}<br />friend CPoint operator ++(CPoint &point,int)<br />{<br />CPoint temp = point;<br />point.x++;<br />point.y++;<br />return temp;<br />}<br />CPoint& operator --()<br />{<br />x--;<br />y--;<br />return *this;<br />}<br />CPoint* operator --(int)<br />{<br />CPoint* temp = new CPoint(x,y);//stack memory,it will not be free until you delete or program exit<br />//call new get memory first,and then call the constructor<br />x--;<br />y--;<br />return temp;<br />}<br />CPoint operator +(CPoint point)//(point+point2).display() to do that,you must return CPoint<br />{<br />CPoint temp(x,y);<br />temp.x += point.x;<br />temp.y += point.y;<br />return temp;<br />}<br />CPoint& operator +=(CPoint point)<br />{<br />x += point.x;<br />y += point.y;<br />return *this;<br />}<br />CPoint operator =(CPoint& point)//can not be extend.no friend,no victurl<br />{<br />x += point.x;<br />y += point.y;<br />return *this;//return for method (point = point+point2).display();<br />}<br />operator double()//with out return value,no friend<br />{<br />return (double)x/y;<br />}</p><p>void* operator new(size_t size)//must return void*,when you use new CPoint(x,y),it will be call.<br />{<br />cout<<"size:"<<size<<endl;<br />//return malloc(size);//they need memory<br />return ::operator new(size);<br />}<br />void* operator new[](size_t size)<br />{<br />cout<<"new[] size:"<<size<<endl;<br />return ::operator new(size);<br />}<br />void* operator new(size_t size,int num1,char* str)<br />{<br />cout<<"num:"<<num1<<"str:"<<str<<endl;<br />return malloc(size);<br />}<br />void operator delete(void* p,size_t size)<br />{<br />cout<<"call delete"<<size<<endl;<br />//free(p);.<br />::operator delete(p);<br />}<br />void operator delete(void* p,int num1,char* str)//????<br />{<br />cout<<"call delete 2"<<endl;<br />free(p);<br />}<br />void operator delete[](void* p,size_t size)<br />{<br />cout<<"call delete[]"<<size<<endl;<br />::delete[] p;//it will delete all the array<br />}<br />void operator[](int index)<br />{<br />cout<<index<<endl;<br />}<br />~CPoint()<br />{<br />//cout<<"call destructer"<<endl;<br />}<br />void display()<br />{<br />cout<<"x:"<<x<<"y:"<<y<<endl;<br />}</p><p>}; </p><p>class CPointChilden:public CPoint{<br />int z;<br />public:<br />CPointChilden(int x,int y,int z):CPoint(x,y)<br />{<br />this->z = z;<br />}<br />};</p><p>int main(int argc, char* argv[])<br />{<br />CPoint point(1,1);<br />point.display();<br />(point++).display();<br />point.display();<br />(point--)->display();//記憶體泄露<br />point.display();<br />(--point).display();</p><p>CPoint point2(1,2);<br />point = point+point2;//(point+point2).display();<br />point.display();</p><p>CPointChilden point3(7,8,9);<br />point3.display();<br />point3++;</p><p>point3.display();<br />float num = point3;//it will use double() firstly,and use (float);<br />cout<<num<<endl;</p><p>CPoint* point4 = new CPoint(3,4);<br />delete point4;//delete[] point4; thay are different</p><p>CPoint* point5 = new(0,"hello world.") CPoint(5,6);<br />delete point5;</p><p>CPoint* pointArray = new CPoint[3];<br />delete[] pointArray;//#####</p><p>cout<<endl;<br />CPoint pointArray2[2];//without call new or new[];<br />pointArray2[1][2];//2 call operator[],you can also to do point2.operator[](2);<br />pointArray2[1].operator ++();//but operator ++,can not work<br />pointArray2[1].display();<br />return 0;<br />}