operator -. - : . - . _ _ :: operator # (_) { //... }_ , , _ , , # . _ . this. _ . this. , "". class vector{ int x, y, z; public: vector operator+ (vector t); vector operator= (vectort); vector operator++ (void); void show (void); void assign (int mx, int my, int mz); } vector vector :: operator+ (vector t) { vector temp; temp.x = x + t.x; temp.y = y + t.y; temp.z = z + t.z; return temp; } vector vector :: operator= (vector t) { x = t.x; y = t.y; z = t.z; return *this; } vector vector :: operator++ (void) { x++; y++; z++ return *this; } void vector :: show (void) { cout << x << ","; cout << y << ","; cout << z << " \n"; } void vector :: assign (int mx, int my, int mz) { x = mx; y = my; z = mz; } main (void) { vector a, b, c; a.assign (1, 2, 3); b.assign (10, 10, 10); a.show(); b.show(); c = a + b; c.show(); c = a + b + c; c.show(); c++; c.show(); } - , - . _ operator # (_) { //... }_ . , . , , typedef, . char, . typedef struct { char *str} Item; int operator < (const Item& a, const Item& b) { return strcmp (a.str, b.str) < 0; } . . , . , C++. . # ## . : . :: .* ? -- -, , this . . class vrctor{ int x, y, z; public: friend vector operator+ (vector t1, vector t2); } vector operator+ (vector t1, vector t2) { vector tmp; tmp.x = t1.x + t2.x; tmp.y = t1.x + t2.y; tmp.z = t1.x + t2.z; return temp; } [], , . . class vector{ int v[3]; public: int& operator[] (int i); } int& vector :: operator[] (int i) { return v[i]; } //... v[0] = 100; // [] (), . . struct Item{ int item; Item (int t){ item = t; } operator int(){ return item; } }; Item t (11); cout << (int)t; // 11 |
Нет комментариев. Оставить комментарий: |