, -. , aa 2, - aa+2 aa.operator+(2), 2+aa , int, + 2.operator+(aa). , aa+2 2+aa -. , . . , $$R.13.2. 7.3  , -, . , : class complex { double re, im; public: complex(double r, double i) { re=r; im=i; } friend complex operator+(complex, complex); friend complex operator+(complex, double); friend complex operator+(double, complex); friend complex operator-(complex, double); friend complex operator-(complex, double); friend complex operator-(double, complex); complex operator-(); // - friend complex operator*(complex, complex); friend complex operator*(complex, double); friend complex operator*(double, complex); // ... }; , : void f() { complex a(1,1), b(2,2), c(3,3), d(4,4), e(5,5); a = -b-c; b = c*2.0*c; c = (d+e)*a; } - , operator*() complex double . , (, , complex <complex.h>). 7.3.1  , , , double complex: class complex { // ... complex(double r) { re=r; im=0; } }; complex, double. . : complex z1 = complex(23); complex z2 = 23; z1 z2 complex(23). . , , . , complex : class complex { double re, im; public: complex(double r, double i =0) { re=r; im=i; } friend complex operator+(complex, complex); friend complex operator*(complex, complex); complex operator+=(complex); complex operator*=(complex); // ... }; . , . , a=b*2 a = operator*(b, complex( double(2), double(0) ) ) , + , , , . , , , operator*=(double): class complex { double re, im; public: complex(double r, double i =0) { re=r; im=i; } friend complex operator+(complex, complex); friend complex operator*(complex, complex); complex& operator+=(complex); complex& operator*=(complex); complex& operator*=(double); // ... }; *= += , , "" * + , : inline complex& complex::operator+=(complex a) { re += a.re; im += a.im; return *this; } , , "" . : inline complex operator+(complex a, complex b) { return complex(a.re+b.re, a.im+b.im); } return , . , , , , * + *= += , : matrix& matrix::operator*=(const matrix& a) { // ... return *this; } matrix operator*(const matrix& a, const matrix& b) { matrix prod = a; prod *= b; return prod; } , , , .. . , ($$7.3.3). , , ,- , . 7.3.2  , : [1] ( ). [2] , . [3] , . , , . - X::operator T(), T - , X T. , tiny (), 0..63, : class tiny { char v; void assign(int i) { if (i>63) { error(" "); v=i&~63; } v=i; } public: tiny(int i) { assign(i) } tiny(const tiny& t) { v = t.v; } tiny& operator=(const tiny& t) { v = t.v; return *this; } tiny& operator=(int i) { assign(i); return *this; } operator int() { return v; } }; tiny, int. tiny . tiny tiny::operator int(), tiny int. , int, tiny, int : void main() { tiny c1 = 2; tiny c2 = 62; tiny c3 = c2 -c1; // c3 = 60 tiny c4 = c3; // ( ) int i = c1 + c2; // i = 64 c1 = c2 + 2 * c1; // : c1 = 0 ( 66) c2 = c1 - i; // : c2 = 0 c3 = c2; // ( ) } tiny, . , []. , ( 100, , - ..). , + *. , ( ) , . istream ostream, , , : while (cin>>x) cout<<x; cin>>x istream&. , cin, while (. $$10.3.2). - , , , . , . . , . , , X::intof(), , , , X::operator int(). 7.3.3  X , X, X. . , . , . : class x { /* ... */ x(int); x(char*); }; class y { /* ... */ y(int); }; class z { /* ... */ z(x); }; x f(x); y f(y); z g(z); void k1() { f(1); // , : f(x(1)) f(y(1)) f(x(1)); f(y(1)); g("asdf"); // , g(z(x("asdf"))) } , : class x { /* ... */ x(int); }; void h(double); void h(x); void k2() { h(1); } h(1) h(double(1)), h(x(1)), . , , $$4.6.6 $$R.13.2, . , . . , . . , , . . (, , ), . , , . , , , complex . . , aa=f(1) f(1), aa . aa x, f(x(1)), x. aa y, f(y(1)). g("asdf"), g(z(x("asdf))) . , , . , , .. , ! , , , . , : class quad { // ... public: quad(double); // ... }; quad operator+(quad,quad); void f(double a1, double a2) { quad r1 = a1+a2; // quad r2 = quad(a1)+a2; // // quad } , , , , . , , , : class real { // ... public: operator double(); operator int(); // ... }; void g(real a) { double d = a; // d = a.double(); int i = a; // i = a.int(); d = a; // d = a.double(); i = a; // i = a.int(); } , . 7.4  , 1.2 12e3 double. , - . . , . , complex <complex.h> zz1*3+zz2*complex(1,2) , . * , + complex(3) complex(1,2) . 7.5  complex . , double, , . . , : class matrix { double m[4][4]; public: matrix(); friend matrix operator+(const matrix&, const matrix&); friend matrix operator*(const matrix&, const matrix&); }; . , .. , . : matrix operator+(const matrix& arg1, const& arg2) { matrix sum; for (int i = 0; i<4; i++) for (int j=0; j<4; j++) sum.m[i] [j] = arg1.m[i][j] + arg2.m[i][j]; return sum; } operator+() , . : class matrix { // ... friend matrix& operator+(const matrix&, const matrix&); friend matrix& operator*(const matrix&, const matrix&); }; , . , . , . , . ( ) , . . 7.6  string: struct string { char* p; int size; // , p string(int size) { p = new char[size=sz]; } ~string() { delete p; } }; - , . . $$5.5.1 : void f() { string s1(10); string s2(20) s1 = s2; } , s1 = s2 , . f() s1 s2 , , . string: struct string { char* p; int size; // , p string(int size) { p = new char[size=sz]; } ~string() { delete p; } string& operator=(const string&); }; string& string::operator=(const string& a) { if (this !=&a) { // , s=s delete p; p = new char[size=a.size]; strcpy(p,a.p); } return *this; } string . f() , : void f() { string s1(10); string s2 = s1; // , } string string::string(int), . , . string::operator(), : p , . , , . , . , , : struct string { char* p; int size; // , p string(int size) { p = new char[size=sz]; } ~string() { delete p; } string& operator=(const string&); string(const string&); }; string::string(const string& a) { p=new char[size=sz]; strcpy(p,a.p); } X X(const X&). , . , . X , , , , , : class X { // ... X(something); // , X(const X&); // operator=(const X&); // : // ~X(); // , }; , : . , .. . . , . : string g(string arg) { return arg; } main() { string s = "asdf"; s = g(s); } , g() s "asdf". s s, string. s g() string(const string&). , s. , , . , , , string::~string() (. $$R.12.2). X X::operator=(const X&) X::X(const X&) , . X. , , , , , . , : class Record { string name, address, profession; // ... }; void f(Record& r1) { Record r2 = r1; } string r1 string::operator=(const string&). - . . . 7.7  operator[] . () . , , . $$2.3.10, , . . : class assoc { struct pair { char* name; int val; }; pair* vec; int max; int free; assoc(const assoc&); // assoc& operator=(const assoc&); // public: assoc(int); int& operator[](const char*); void print_all(); }; assoc pair max. free . assoc, . : assoc::assoc(int s) { max = (s<16) ? 16 : s; free = 0; vec = new pair[max]; } , $$2.3.10. , , assoc : #include <string.h> int& assoc::operator[](const char* p) /* ( pair): p, , , p */ { register pair* pp; for (pp=&vec[free-1]; vec<=pp; pp-- ) if (strcmp(p,pp->name) == 0) return pp->val; if (free == max) { //: pair* nvec = new pair[max*2]; for (int i=0; i<max; i++) nvec[i] = vec[i]; delete vec; vec = nvec; max = 2*max; } pp = &vec[free++]; pp->name = new char[strlen(p)+1]; strcpy(pp->name,p); pp->val = 0; // = 0 return pp->val; } assoc , - . . : void assoc::print_all() { for (int i = 0; i<free; i++) cout << vec[i].name << ": " << vec[i].val << '\n'; } , : main() // // { const MAX = 256; // char buf[MAX]; assoc vec(512); while (cin>>buf) vec[buf]++; vec.print_all(); } , . $$7.14 [20]. $$8.8. operator[]() . , x[y] == y[x] , x . , , ($$7.2.2, . $$7.9). 7.8  , .. (-), , , - - . . operator()() . , , , , . assoc. assoc_iterator, assoc . , assoc, friend: class assoc { friend class assoc_iterator; pair* vec; int max; int free; public: assoc(int); int& operator[](const char*); }; : class assoc_iterator { const assoc* cs; // assoc int i; // public: assoc_iterator(const assoc& s) { cs = &s; i = 0; } pair* operator()() { return (i<cs->free)? &cs->vec[i++] : 0; } }; assoc assoc_iterator , () ( pair) . 0: main() // // { const MAX = 256; // char buf[MAX]; assoc vec(512); while (cin>>buf) vec[buf]++; assoc_iterator next(vec); pair* p; while ( p = next(vec) ) cout << p->name << ": " << p->val << '\n'; } , : , . , . , . , first(), next() last(), ++ , , (. $$8.8). , operator() . operator() -. 7.9  -> . , class Ptr { // ... X* operator->(); }; Ptr X , : void f(Ptr p) { p->m = 7; // (p.operator->())->m = 7 } p p.operator->() m, . operator->() . , , - -> : void g(Ptr p) { X* q1 = p->; // X* q2 = p.operator->(); // } -> " ", .. , . , RecPtr Rec, . RecPtr , . RecPtr::operator->() , RecPtr . class RecPtr { Rec* in_core_address; const char* identifier; // ... public: RecPtr(const char* p) : identifier(p) { in_core_address = 0; } ~RecPtr() { write_to_disc(in_core_address,identifier); } Rec* operator->(); }; Rec* RecPtr::operator->() { if (in_core_address == 0) in_core_address = read_from_disc(identifier); return in_core_address; } : main(int argc, const char* argv) { for (int i = argc; i; i--) { RecPtr p(argv[i]); p->update(); } } , RecPtr (. $$8), Record . , . -> , * []. , Y* p; p->m == (*p).m == p[0].m , . , - , : class X { Y* p; public: Y* operator->() { return p; } Y& operator*() { return *p; } Y& operator[](int i) { return p[i]; } }; , , , x , ++, += = +, ++x x+=1 x=x+1. -> [] , . , , -> , . ->, ++ , (. $$12.2.8 13.9). 7.10  " ", ++ -- , , . , " ", , . : void f1(T a) // { T v[200]; T* p = &v[10]; p--; *p = a; // : `p' , // ++p; *p = a; // } p CheckedPtrToT, , . , : class CheckedPtrToT { // ... }; void f2(T a) // { T v[200]; CheckedPtrToT p(&v[0],v,200); p--; *p = a; // : // `p' ++p; *p = a; // } ++, . , CheckedPtrToT : class CheckedPtrToT { T* p; T* array; int size; public: // `p' // `a' `s' CheckedPtrToT(T* p, T* a, int s); // `p' // CheckedPtrToT(T* p); T* operator++(); // T* operator++(int); // T* operator--(); // T* operator--(int); // T& operator*(); // }; int , . , . , operator++ , , , . "" ++ --. CheckedPtrToT : void f3(T a) // { T v[200]; CheckedPtrToT p(&v[0],v,200); p.operator--(1); p.operator*() = a; // : // `p' p.operator++(); p.operator*() = a; // } $$7.14 [19] CheckedPtrToT, ($$9.10[2]) , . ++ -- $$8.8. 7.11  string. , , C++. #include <iostream.h> #include <string.h> class string { struct srep { char* s; // int n; //