                                                           ++   714

15

                      15.   
                           
                             

      15      : 
        .   
           ,   
     .        
      .    
          ,   ,
        ,    
    : ,  , ,     
    ,   ,      
    new  delete.    ,   
     ,      (), 
         .   
    ,       
           .
          
           , -
       .

                           15.1.  

        ,    
      (.  4)
   . ,   String   3.15  
 .    :

                                                           ++   715

   #include <iostream>

   class String;

   istream& operator>>( istream &, const String & );
   ostream& operator<<( ostream &, const String & );

   class String {
   public:
      //   
      //   
      String( const char* = 0 );
      String( const String & );

      // :  
      ~String();

      //    
      String& operator=( const String & );
      String& operator=( const char * );

      //    
      char& operator[]( int );

      //    
      // str1 == str2;
      bool operator==( const char * );
      bool operator==( const String & );

      //    
      int size() { return _size; };
      char * c_str() { return _string; }
   private:
      int _size;
      char *_string;
   };

     String     .    
 :

   //    
   String& operator=( const String & );
   String& operator=( const char * );

       . (   
 14.7.)     C- 
  String:

   String name;
   name = "Sherlock"; //   operator=( char * )

( ,   ,     15.3.)
            :

                                                           ++   716

   //    
   char& operator[]( int );

         String   , 
   :

   if ( name[0] != 'S' )
   cout << ", -  \n";

(      15.4.)
            
String.           C-
:

   //    
   // str1 == str2;
   bool operator==( const char * );
   bool operator==( const String & );

           ,
   4,      ,  
 . ,      
 String,        - concat().  
concat(),  , , append()?       ,
     ,    .   
,    .  ,  concat() 
    operator+=().    
:

   #include "String.h"

   int main() {
      String name1 "Sherlock";
      String name2 "Holmes";
      name1 += " ";
      name1 += name2;
      if (! ( name1 == "Sherlock Holmes" ) )
         cout << "  \n";
   }

           ,   -
,        operator,     
    C++  (. . 15.1).  
 operator+=()   String:

                                                           ++   717

   class String {
   public:
      //    +=
      String& operator+=( const String & );
      String& operator+=( const char * );
      // ...
   private:
      // ...
   };

    :

   #include <cstring>
   inline String& String::operator+=( const String &rhs )
   {
      //  ,    rhs, 
      if ( rhs._string )
      {
         String tmp( *this );

         //   , 
         //    
         _size += rhs._size;
         delete [] _string;
         _string = new char[ _size + 1 ];

         //       
         //     ,    rhs
         strcpy( _string, tmp._string );
         strcpy( _string + tmp._size, rhs._string );
      }
      return *this;
   }

   inline String& String::operator+=( const char *s )
   {
      //   s 
      if ( s )
      {
         String tmp( *this );
         //   , 
         //    
         _size += strlen( s );
         delete [] _string;
         _string = new char[ _size + 1 ];

         //       
         //     C-,    s
         strcpy( _string, tmp._string );
         strcpy( _string + tmp._size, s );
      }
      return *this;
   }

                                                           ++   718

                         15.1.1.     

         String  . 
     ,      C-
:

   #include "String.h"

   int main() {
      String flower;

      // -    flower
      if ( flower == "lily" ) // 
         // ...
      else
        if ( "tulip" == flower ) // 
           // ...
   }

         main()  
operator==(const char *)  String.     if
    .   ?
    ,    ,  
,       .    
      String,    
 ,       C-,   
  String. ,   ,    
.
         String  C-    .
      :

   if ( String( "tulip" ) == flower ) //:  -

      .    ,  
      .  ,   Text  
 :

   class Text {
   public:
      Text( const char * = 0 );
      Text( const Text & );

      //    
      bool operator==( const char * ) const;
      bool operator==( const String & ) const;
      bool operator==( const Text & ) const;
      // ...
   };

   main()   :


                                                           ++   719

   if ( Text( "tulip" ) == flower ) //  Text::operator==()

   ,       ,
        ,
       .    
         
,  ,        .   
  ,       
 (  )       !
       ,   
 C++  .     
 ,       ( 
 ,      19).
   , ,   ,   
.     main(),   , 
   .  , ,   C-
   ,   ,    ,
   String,   ,   
  :

   bool operator==( const String &, const String & );
   bool operator==( const String &, const char * );

    ,        
 ,  -.     , 
     this.    -


   flower == "lily"

   :

   flower.operator==( "lily" )

    flower    - 
   this. ( this    13.4.)   
  ,   ,   
.
    

   flower == "lily"

 

   bool operator==( const String &, const char * );

   ,        
:

                                                           ++   720

   "tulip" == flower

         :

    bool operator==( const char *, const String & );

     .       
,    ,       (   
)   , .. 
     

  operator==( String("tulip"), flower );

       :

   bool operator==( const String &, const String & );

          :

   bool operator==( const String &, const char * );

      C-   String      
.  main()    ,    
   ,    String:

   bool operator==( const String &, const String & );

          :

   bool operator==( const char *, const String & );
   bool operator==( const String &, const char * );

  ,       C-  String 
 ,        
,    String.     
   C-   ,     
. (      ,  .
              
 15.9;   15.10        
 ,    15.12     .)
   ,     ,      
  ?        :

        ,    
     ,       .   
       ,      ;
    ,    ("="),   ("[]"),
     ("()")       ("->")    
    .        :

                                                           ++   721

     // :    
     char& operator[]( String &, int ix );

(      15.3,    
 15.4,     15.5,          
15.6.)
         . 
,   ,     , 
      (  String).
       ,     
String   :

   bool operator==( const String &str1, const String &str2 )
   {
      if ( str1.size() != str2.size() )
         return false;
      return strcmp( str1.c_str(), str2.c_str() ) ? false : true ;
   }

   inline bool operator==( const String &str, const char *s )
   {
      return strcmp( str.c_str(), s ) ? false : true ;
   }

                       15.1.2.   

         C++ (. . 15.1).

    15.1.  

	+ 	- 	* 	/ 	% 	^ 	& 	| 	~
	! 	, 	= 	< 	> 	<= 	>= 	++ 	--
	<< 	>> 	== 	!= 	&& 	|| 	+= 	-= 	/=
	%= 	^= 	&= 	|= 	*= 	<<= 	>>= 	[] 	()
	-> 	->* 	new 	new[] delete delete[]

            .
,     **      
  .
       C++    :

   //  
   :: .* . ?:

                                                           ++   722

          .
,        
,      .

   // :      int
   int operator+( int, int );

           ,
      operator+   
.
           
           ,
         ( 
   ).
      (.  4.13)  .
        

x == y + z;

   operator+,   operator==;   
  .
         .  ,
          
  String.       
:

   // : ! -   
   bool operator!( const String &s1, const String &s2 )
   {
      return ( strcmp( s1.c_str(), s2.c_str() ) != 0 );
   }

         ("+", "-", "*"  "&")
   ,   .       
 .
      ,   operator(), 
  .

                  15.1.3.   

    ,       
,      .     .
   ,      ,
    .     
  .

                                                           ++   723

         .   -
    ,    
.   ,      
 .
        ,   
    :

    isEmpty()    Ŕ, operator!().
    isEqual()   , operator==().
    copy()   , operator=().

         . ,  + 
  ,        
     . ,    
       .
         
operator+()   ,  :   
  .
          .
     ,   operator+(),  
   String, ,     
.     ,    
.
        
      (,
  +,    =,    +=) 
     . ,  String  
operator+(),   operator=()      
:

   String s1( "C" );
   String s2( "++" );
   s1 = s1 + s2; // s1 == "C++"

          

   s1 += s2;

     , ,     .

                                      15.1

           
operator==(const String&, const String&):

   "cobble" == "stone"

                                     15.2

                                                           ++   724

      ,     
 :

   String != String
   String != -
   C- != String

   ,        .

                                     15.3

     -  Screen,    13 ( 13.3, 13.4
 13.6),   .

                                     15.4

   ,      ,   
String   3.15,    ,   -.

                                    15.5

           Screen   13.

                                     15.2. 

           String,
     .    
 String   :

   bool operator==( const String &str1, const String &str2 )
   {
      if ( str1.size() != str2.size() )
         return false;
      return strcmp( str1.c_str(), str2.c_str() ) ? false : true;
   }

            -:

   bool String::operator==( const String &rhs ) const
   {
      if ( _size != rhs._size )
         return false;
     return strcmp( _string, rhs._string ) ? false : true;
   }

            String.
       ,   -, 
       String.    
String      C-   - size()
 c_str().

                                                           ++   725

         
  String.       , 
    .
     (     friend)   
 .      , 
 ,  ,      public, private 
protected   .        
    :

   class String {
      friend bool operator==( const String &, const String & );
      friend bool operator==( const char *, const String & );
      friend bool operator==( const String &, const char * );
   public:
      // ...    String
   };

          , 
  ,    String,  , 
         :

   //       
   //  String
   bool operator==( const String &str1, const String &str2 )
   {
      if ( str1._size != str2._size )
          return false;
      return strcmp( str1._string, str2._string ) ? false : true;
  }
   //  ..

    ,         _size  _string
,     c_str()  size()    
   ,  ,     
   String  .
    ,    ,    ,   
  ?       
     ,    
  .    , 
 ,     ,   
       ,    
  .         
 ,          
,      .

                                                           ++   726

         ,   
,    ,     .   
      ,  
   -    .
           , 
,     , -    
     .     
,   -       
.     ,   .
           , 
     :

   extern ostream& storeOn( ostream &, Screen & );
   extern BitMap& storeOn( BitMap &, Screen & );
   // ...

   class Screen
   {
      friend ostream& storeOn( ostream &, Screen & );
      friend BitMap& storeOn( BitMap &, Screen & );
      // ...
   };

               
 ,         ,
      .
          :

   class Window; //    
   class Screen {
      friend bool is_equal( Screen &, Window & );
      // ...
   };

   class Window {
      friend bool is_equal( Screen &, Window & );
      // ...
   };

              , 
    :

                                                           ++   727

   class Window;

   class Screen {
      // copy() -   Screen
      Screen& copy( Window & );
      // ...
   };

   class Window {
      // Screen::copy() -   Window
      friend Screen& Screen::copy( Window & );
      // ...
   };

   Screen& Screen::copy( Window & ) { /* ... */ }

   -        ,  
     .    . ,
 Screen    - Window  , 
Window       - Screen.   
  Window   Screen:

   class Window;

   class Screen {
      friend class Window;
      // ...
   };

       Screen      -
Window.

                                  15.6

       ,    Screen  
15.5,        ,   
   .   ?  .

                                  15.3.  =

             
  . (     
 14.7.)
           .  
    ,    ,  
  ,   . , 
  C-  String:

   String car ("Volks");
   car = "Studebaker";

                                                           ++   728

  ,    const char*.  
     :

   class String {
   public:
      //    char*
      String& operator=( const char * );
      // ...
   private:
      int _size;
      char *string;
   };

       .   String 
 ,   .     
 C-:

   String& String::operator=( const char *sobj )
   {
      // sobj -  
      if (! sobj ) {
        _size = 0;
       delete[] _string;
       _string = 0;
     }
     else {
        _size = strlen( sobj );
        delete[] _string;
        _string = new char[ _size + 1 ];
        strcpy( _string, sobj );
      }
      return *this;
   }

   _string     C-,    sobj.  
?     sobj  _string :

   _string = sobj; // :  

sobj     const , ,      
-const (.  3.5).    :

   String& String::operator=( const *sobj ) { // ... }

    _string    C-,  sobj.   
  . ,  C-   const char*.
     -const   :

   car = "Studebaker"; //    operator=( char *) !

   ,  .   C-   String,  
  const char*.

                                                           ++   729

     _string    C-,  sobj,   
.   ,     sobj.    
,   ,   String. :

   char ia[] = { 'd', 'a', 'n', 'c', 'e', 'r' };
   String trap = ia; // trap._string   ia
   ia[3] = 'g'; //      :
                //   ia,  trap._string

    trap._string    ia,   trap  
 :       -
 String.   ,       
 C-  .
    ,      delete.  _string
    ,   .  
, ,    ,    delete 
   .  _string   , 
  delete   (.  8.4).
        .     
    String.   ?   ,   
    :

   //   
   int iobj, jobj;
   iobj = jobj = 63;

      , ..    
 :

   iobj = (jobj = 63);

           String: ,  ,
 :

   verb = noun = "count";

             
const char*.      ,    
        String.
,       const char *,  
   String.
      . ,    String
  :

                                                           ++   730

   //    
   String& operator=( const String & );
   String& operator=( const char * );

          , 
   String.      
  - .

                         15.4.   

      operator[]()    , 
 ,     .  
     String,  IntArray,   
2,    vector,     C++. 
    - .
     String       
  _string.       
 :

   String entry( "extravagant" );
   String mycopy;

   for ( int ix = 0; ix < entry.size(); ++ix )
      mycopy[ ix ] = entry[ ix ];

         ,     
.     ,    l-
 .     :

   #include <cassert>

   inine char&
   String::operator[]( int elem ) const
   {
      assert( elem >= 0 && elem < _size );
      return _string[ elem ];
   }

         color   'V':

   String color( "violet" );
   color[ 0 ] = 'V';

    ,         
.     C- assert().  
 , ,   elem  0    C-

                                                           ++   731

,    _string. (   
   11.)

                          15.5.   

            . ( 
,   ,   -   12.3.) 
 ,   ,     
 . ,       int
   absInt:

   class absInt {
   public:
      int operator()( int val ) {
         int result = val < 0 ? -val : val;
         return result;
     }
   };

     operator()     - 
  .      
 ,    (.  7.2, 7.3  7.4). operator()
        ,   
.  ,        ,
   12.      transform()
     absInt    
 ivec, ..      .

   #include <vector>
   #include <algoritm>

   int main() {
      int ia[] = { -0, 1, -1, -2, 3, 5, -5, 8 };
      vector< int > ivec( ia, ia+8 );

      //      
      transform( ivec.begin(), ivec.end(), ivec.begin(), absInt() );
      // ...
   }

       transform()   ,  
  absInt.     ,   
  .
          absInt,   
  .    transform(),
  main(),    :

                                                           ++   732

   typedef vector< int >::iterator iter_type;

   //  transform()
   //  absInt     int

   iter_type transform( iter_type iter, iter_type last,
                                iter_type result, absInt func )
   {
       while ( iter != last )
           *result++ = func( *iter++ ); //  absInt::operator()
       return iter;
   }

   func    ,    absInt,  
 int   .     
 operator()  absInt.     *iter,
    ,      
.

                              15.6.  

    ,    ,    
.      -   
.       ,  
  (smart pointer),    , 
    .
   ,          
Screen (.  13):

   class ScreenPtr {
      // ...
   private:
      Screen *ptr;
   };

    ScreenPtr   ,     
   Screen:     ,    
.       ScreenPtr, 
,     -  Screen.   
  ScreenPtr  ,     
(     14.2):

   class ScreenPtr {
   public:
      ScreenPtr( const Screen &s ) : ptr( &s ) { }
       // ...
   };

                                                           ++   733

        ScreenPtr  
    Screen,      ScreenPtr:

   ScreenPtr p1; // :   ScreenPtr   
                           
    Screen myScreen( 4, 4 );
   ScreenPtr ps( myScreen ); // 

     ScreenPtr     ,  
     (*)     
:

   //      
   class ScreenPtr {
   public:
      Screen& operator*() { return *ptr; }
      Screen* operator->() { return ptr; }
      // ...
   };

       ,     . 
          
. ,  

   point->action();

  point.       ,  
     .       
,  ,        . 
   ,     point, 
 ,        (   
)    .
            ,
  ,    .   ,   
    .    
 ,        .
,     ps  ScreenPtr   
 Screen:

   ps->move( 2, 3 );

           ScreenPtr, 
    ,    
 Screen.       
   - move().
          ScreenPtr. 
 ScreenPtr    ,     Screen*:

                                                           ++   734

   #include <iostream>
   #include <string>
   #include "Screen.h"

   void printScreen( const ScreenPtr &ps )
   {
      cout << "Screen Object ( "
              << ps->height() << ", "
              << ps->width() << " )\n\n";

      for ( int ix = 1; ix <= ps->height(); ++ix )
      {
         for ( int iy = 1; iy <= ps->width(); ++iy )
            cout << ps->get( ix, iy );
         cout << "\n";
     }
   }

   int main() {
      Screen sobj( 2, 5 );
      string init( "HelloWorld" );
      ScreenPtr ps( sobj );

      //   
      string::size_type initpos = 0;
      for ( int ix = 1; ix <= ps->height(); ++ix )
         for ( int iy = 1; iy <= ps->width(); ++iy )
         {
            ps->move( ix, iy );
            ps->set( init[ initpos++ ] );
         }

      //   
      printScreen( ps );

      return 0;
    }

   ,         
,     .  
    ,  
,     .

                          15.7.    

       ScreenPtr,    ,
   ,      
       :  (++) 
 (--).    ScreenPtr     
 Screen,      .
        size,     (  
,   ScreenPtr    ),   ,
  ScreenPtr.     offset, 
    :

                                                           ++   735

   class ScreenPtr {
   public:
      // ...
   private:
      int size; //  : 0,   
      int offset; //  ptr   
      Screen *ptr;
   };

      ScreenPtr      
 ,.      
 ,      :

   class ScreenPtr {
   public:
      ScreenPtr( Screen &s , int arraySize = 0 )
         : ptr( &s ), size ( arraySize ), offset( 0 ) { }
   private:
      int size;
      int offset;
      Screen *ptr;
   };

         .   
,      ,  . 
,     ,   size   0 ,
,        Screen. 
  ScreenPtr    :

   Screen myScreen( 4, 4 );
   ScreenPtr pobj( myScreen ); // :    
    const int arrSize = 10;
   Screen *parray = new Screen[ arrSize ];
   ScreenPtr parr( *parray, arrSize ); // :   

        ScreenPtr    
.     :   .  ,
   .      
 :

   class ScreenPtr {
   public:
      Screen& operator++();
      Screen& operator--();
      // ...
   };

         . 
   ,  ,  :

                                                           ++   736

   const int arrSize = 10;
   Screen *parray = new Screen[ arrSize ];
   ScreenPtr parr( *parray, arrSize );
   for ( int ix = 0;
      ix < arrSize;
      ++ix, ++parr ) //  parr.operator++()
   }

  printScreen( parr );

        :

   Screen& ScreenPtr::operator++()
   {
      if ( size == 0 ) {
            cerr << "      \n";
         return *ptr;
      } 

      if ( offset >= size - 1 ) {
         cerr << "   \n";
         return *ptr;
      }
       
      ++offset;
      return *++ptr;
   }

   Screen& ScreenPtr::operator--()
   {
      if ( size == 0 ) {
         cerr << "      \n";
         return *ptr;
     }
    if ( offset <= 0 ) {
       cerr << "   \n";
       return *ptr;
   }
   --offset;
   return *--ptr;
   }

        ,   
    int.    
         

   class ScreenPtr {
   public:
      Screen& operator++(); //  
      Screen& operator--();
      Screen& operator++(int); //  
      Screen& operator--(int);
      // ...
   ScreenPtr:
   };

                                                           ++   737

        :

   Screen& ScreenPtr::operator++(int)
   {
      if ( size == 0 ) {
         cerr << "      \n";
         return *ptr;
     }
     if ( offset == size ) {
        cerr << "      \n";
        return *ptr;
     }

     ++offset;
      return *ptr++;
   }

   Screen& ScreenPtr::operator--(int)
   {
      if ( size == 0 ) {
         cerr << "      \n";
         return *ptr;
     }
     if ( offset == -1 ) {
        cerr << "      \n";
        return *ptr;
     }
     --offset;
      return *ptr--;
   }

    ,       ,
      .  
     ,   .  
  :

   const int arrSize = 10;
   Screen *parray = new Screen[ arrSize ];
   ScreenPtr parr( *parray, arrSize );
   for ( int ix = 0; ix < arrSize; ++ix)
      printScreen( parr++ );

              . 
   ScreenPtr   ,   
:

   parr.operator++(1024); //   operator++

          
 .     
ScreenPtr:

                                                           ++   738

   class ScreenPtr {
      //   
      friend Screen& operator++( Screen & ); //  
      friend Screen& operator--( Screen & );
      friend Screen& operator++( Screen &, int); //  
      friend Screen& operator--( Screen &, int);
   public:
      //  
   };

                                        15.7

           
ScreenPtr, ,      .

                                        15.8

     ScreenPtr        Screen.
  operator*()  operator->() (.  15.6) ,
            
 . :        size
 offset.

                        15.8.  new  delete

              
     new()  delete(),  
  C++. (      8.4.)  
      , 
 -.     ,   
          
.
     new()  delete()    Screen.
   - new()     void*    
    size_t,  size_t   typedef,  
   <cstddef>.   :

   class Screen {
   public:
      void *operator new( size_t );
      // ...
   };

          new(),  ,
      .  ,      
  ,       new(). ,
 

    Screen *ps = new Screen;

                                                           ++   739

  Screen  ,        new(), 
 .  size_t    ,
  Screen .
     new()         
 .  new      ,
   -.     Screen    new(), 
   ,   -  
 .
           
 new(),     Screen   :

   Screen *ps = ::new Screen;

    delete(),   ,    void,  
    void*.     
 Screen:

   class Screen {
   public:
      void operator delete( void * );
   };

     delete      ,  ,
      delete().  ,    
  ,       . 


   delete ps;

 ,    Screen,    ps.
  Screen  - delete(),    . 
  void*    ps.
    delete()          
 .  delete     
,    -.     Screen   
 delete(),     ,   -
    .
           
 delete(),    Screen   :

   ::delete ps;

        delete()   
 new(),      . ,  ps
   ,   new(),    
    delete().

                                                           ++   740

    delete(),    ,    
 .   -    void*,   
  size_t (     <cstddef>):

   class Screen {
   public:
      // 
      // void operator delete( void * );
      void operator delete( void *, size_t );
   };

      ,     ,
       . ( 
   ,   delete()   
.      17.)
      new()  delete()   Screen  . 
         
Screen,      freeStore.    
- new()     .   delete()
   .      , 
freeStore, ,     new(),   
,    screenChunk   Screen.
    screenChunk,   freeStore     Screen, 
    .  ,     
      ,  , 
  .      
Screen,     next:

   class Screen {
   public:
       void *operator new( size_t );
       void operator delete( void *, size_t );
       // ...
   private:
       Screen *next;
       static Screen *freeStore;
       static const int screenChunk;
   };

         new()   Screen:

                                                           ++   741

   #include "Screen.h"
   #include <cstddef>

   //   
   //    ,     
   Screen *Screen::freeStore = 0;
   const int Screen::screenChunk = 24;

   void *Screen::operator new( size_t size )
   {
      Screen *p;

      if ( !freeStore ) {
         //   :   
         //    new
         size_t chunk = screenChunk * size;
         freeStore = p =
               reinterpret_cast< Screen* >( new char[ chunk ] );

         //     
         for ( ;
            p != &freeStore[ screenChunk - 1 ];
            ++p )
            p->next = p+1;
         p->next = 0;
      }

      p = freeStore;
      freeStore = freeStore->next;

      return p;
   }

       delete():

   void Screen::operator delete( void *p, size_t )
   {
      //  ""  ,
      //   

      ( static_cast< Screen* >( p ) )->next = freeStore;
      freeStore = static_cast< Screen* >( p );

   }

    new()        delete().  
       .
     delete()  new():   
    .    
 ,    ,   , 
,  .
       ,       
,       -: 
   this,  ,     
   . (.   -  
13.5.) ,      ,   ,

                                                           ++   742

        (new()),  
  (delete()).
        new(), :

   Screen *ptr = new Screen( 10, 20 );

    :

   //   C++
   ptr = Screen::operator new( sizeof( Screen ) );
   Screen::Screen( ptr, 10, 20 );

    ,       new(), 
   ,      .
 new()   ,     bad_alloc 
  .
        delete(), :

   delete ptr;

    :

   //   C++
   Screen::~Screen( ptr );
  Screen::operator delete( ptr, sizeof( *ptr ) );

    ,       ,  
    delete()   .   ptr
 0,   ,  delete()  .

                        15.8.1.  new[ ]  delete [ ]

    new(),    ,   
    . ,     new()
 Screen:

   //  Screen::operator new()
   Screen *ps = new Screen( 24, 80 );

      new[]()     
    Screen:

                                                           ++   743

   //  Screen::operator new[]()
   Screen *psa = new Screen[10];

         new[]()  delete[]()   
.
   - new[]()     void*    
    size_t.     Screen:

   class Screen {
   public:
      void *operator new[]( size_t );
      // ...
   };

      new     ,  ,
     new[]().  ,      
  ,      new[]().  
        Screen:

   Screen *ps = new Screen[10];

        new[](),       .
  size_t   ,  
  ,      Screen.
        - new[](),    
   new[](),   
  :

   Screen *ps = ::new Screen[10];

    delete(),   ,    void,   
   void*.       Screen:

   class Screen {
   public:
      void operator delete[]( void * );

   };

       , delete    :

   delete[] ps;

     delete      , 
,       delete[]().  ,  
    ,      
.   void*     
 ,    .

                                                           ++   744

        - delete[](),   
 delete[](),     
:

   ::delete[] ps;

     new[]()  delete[]()       
   :    ,  
-  .
        new[]()    ,
         . 
      ,     ,  
 new[]()  .     
        
   .
           
,    delete[]()     .  
   .   

   delete ps;

ps     ,      
     ,    
.
    - delete[]()    ,   ,   
   size_t:

   class Screen {
   public:
      // 
      // void operator delete[]( void* );
      void operator delete[]( void*, size_t );
   };

      ,     
,        .

            15.8.2.   new()   delete()

   - new()     ,    
  .      size_t:

   class Screen {
   public:
      void *operator new( size_t );
      void *operator new( size_t, Screen * );
      // ...
   };


                                                           ++   745


       ,  
 new:

   void func( Screen *start ) {
      Screen *ps = new (start) Screen;
      // ...
   }

     ,      new   
 ,   .    
 new(),   .   
,    Screen  ,     
 start.
       - delete().     
   delete.  delete()  
,  ,     new ( 
,      new),  . 
 delete()  .
       

   Screen *ps = new ( start ) Screen;

:

  1.      new(size_t, Screen*).
  2.      Screen  
      .

    ps     Screen.
   ,    new(size_t, Screen*)    
 new().    ,   
,     2   ? 
     ,   
 delete(),      .
          ,  
   new(),      
 . ,     
 new:

   Screen *ps = new (start) Screen;

        Screen  ,  
 delete()    Screen.     , 
       new(). 
  new()    size_t,   delete()  void*, 
     .     Screen
 delete()  :

                                                           ++   746

   void operator delete( void*, Screen* );

       ,        ,
 new()  . (   .)
      ,   delete(), 
 new(),    ,     new() 
    .    delete() 
   ,    ;   
 .
        new[]()   delete[]() 
:

   class Screen {
   public:
      void *operator new[]( size_t );
      void *operator new[]( size_t, Screen* );
      void operator delete[]( void*, size_t );
      void operator delete[]( void*, Screen* );
      // ...
  };

    new[]()   ,   ,  new 
 ,    :

   void func( Screen *start ) {
      //  Screen::operator new[]( size_t, Screen* )
      Screen *ps = new (start) Screen[10];
      // ...
   }

       new   ,  
  delete[]().

                                      15.9

,     :

   class iStack {
   public:
      iStack( int capacity )
         : _stack( capacity ), _top( 0 ) {}
      // ...
   private:
      int _top;
      vatcor< int > _stack;
   };

                                                           ++   747

   (a) iStack *ps = new iStack(20);
   (b) iStack *ps2 = new const iStack(15);
   (c) iStack *ps3 = new iStack[ 100 ];

                                  15.10

       ,  new  delete?

   class Exercise {
   public:
      Exercise();
      ~Exercise();
   };
   Exercise *pe = new Exercise[20];
   delete[] ps;

      ,     new() 
delete().

                                 15.11

   ,       delete().

              15.9.   

     ,        :
  4.14        ,
   9.3        
     .     
   :

   char ch; short sh;, int ival;

   /*     
    *    */

   ch + ival; ival + ch;
   ch + sh; ch + ch;
   ival + sh; sh + ival;

    ch  sh    int.    
   int.       
 .
       ,     
    .   
       .
 ,   ,     SmallInt,  
 10.9.

                                                           ++   748

   ,  SmallInt   ,    
  ,  unsigned char, ..  0  255,    
  .           , 
unsigned char.
        SmallInt     
     ,    ,  
 :

   class SmallInt {
      friend operator+( const SmallInt &, int );
      friend operator-( const SmallInt &, int );
      friend operator-( int, const SmallInt & );
      friend operator+( int, const SmallInt & );
   public:
      SmallInt( int ival ) : value( ival ) { }
      operator+( const SmallInt & );
      operator-( const SmallInt & );
      // ...
   private:
      int value;
   };

   -        SmallInt.
  -      
      .  
 ,       
   int. , 
   
   SmallInt si( 3 );
   si + 3.14159

   :

  1.  3.14159  double     3.
  2.  operator+(const SmallInt &,int),    6.

          ,    
   ,     
?    .    
  SmallInt    int.
     C++  ,      
,    .  SmallInt   
   int.   :

                                                           ++   749

   class SmallInt {
   public:
      SmallInt( int ival ) : value( ival ) { }

      // 
      // SmallInt ==> int
      operator int() { return value; }

      //    
   private:
      int value;
   };

    int()   ,   
,          int.
  ,      
     .   SmallInt 
  int   ,     int,  
 value.
      SmallInt   ,  
 int.  ,       
SmallInt    int,  

   SmallInt si( 3 );
   si + 3.14159

  :

   1.    SmallInt,     3.
   2.   3   3.0      
   3.14159,   6.14159.

           
     .   
int     double,      
double (  int   double)      
.
         SmallInt:

                                                           ++   750

   #include <iostream>
   #include "SmallInt.h"

   int main() {
      cout << " SmallInt, : ";
      while ( cin >> si1 ) {
          cout << "  "
                 << si1 << "\n ";
         // SmallInt::operator int()  
         cout << ( ( si1 > 127 )
                ? ",  "
                : ( ( si1 < 127 )
               ? ",  "
               : " ") ) << "127\n";

         cout << "\ SmallInt,  \
                        (ctrl-d  ): ";
      }
      cout <<" \n";
   }

       :

    SmallInt, : 127
     127
     127
    SmallInt,  (ctrl-d  ): 126
    ,  127
    SmallInt,  (ctrl-d  ): 128
    ,  127
    SmallInt,  (ctrl-d  ): 256
   ***   SmallInt: 256 ***

   SmallInt    :

   #include <iostream>

   class SmallInt {
      friend istream&
      operator>>( istream &is, SmallInt &s );
      friend ostream&
      operator<<( ostream &is, const SmallInt &s )
          { return os << s.value; }
   public:
       SmallInt( int i=0 ) : value( rangeCheck( i ) ){}
       int operator=( int i )
       { return( value = rangeCheck( i ) ); }
       operator int() { return value; }
   private:
      int rangeCheck( int );
      int value;
   };

                                                           ++   751

      -,    :

   istream& operator>>( istream &is, SmallInt &si ) {
      int ix;
      is >> ix;
      si = ix; // SmallInt::operator=(int)
      return is;
   }

   int SmallInt::rangeCheck( int i )
   {
      /*      ,   ,
       *    ;     */

      if ( i & ~0377 ) {
         cerr << "\n***   SmallInt: "
               << i << " ***" << endl;
         exit( -1 );
      }
      return i;
   }

                               15.9.1. 

        - ,  
      .   
      operator,     
.
   ,    ,       
 .     Token   .
         typedef tName,     
 SmallInt.

   #include "SmallInt.h"

   typedef char *tName;

   class Token {
   public:
      Token( char *, int );
      operator SmallInt() { return val; }
      operator tName() { return name; }
      operator int() { return val; }
      //   
   private:
      SmallInt val;
      char *name;
   };

    ,      SmallInt  int .
 Token::operator int()    val.  val
  SmallInt,    SmallInt::operator int() 
 val   int.  Token::operator int()  
     Token    int. ,

                                                           ++   752

        t1  t2
 Token   int    print():

   #include "Token.h"

   void print( int i )
   {
      cout << "print( int ) : " << i << endl;
   }

   Token t1( "integer constant", 127 );
   Token t2( "friend", 255 );

   int main()
   {
      print( t1 ); // t1.operator int()
      print( t2 ); // t2.operator int()
      return 0;
   }

          :

   print( int ) : 127
   print( int ) : 255

      :

  operator type();

 type    ,     typedef. ,
  type     ,  .   
-.         
,   :

   operator int( SmallInt & ); // :  

   class SmallInt {
   public:
      int operator int(); // :    
      operator int( int = 0 ); // :   
      // ...
   };

         .  
   ,    ,     
  ,    :

                                                           ++   753

   #include "Token.h"

   Token tok( "function", 78 );
   //  :  Token::operator SmallInt()
   SmallInt tokVal = SmallInt( tok );
   // static_cast:  Token::operator tName()
   char *tokName = static_cast< char * >( tok );

     Token::operator tName()     .
      Token::name  
 :

   char *tokName = tok.name; // : Token::name -  

     ,     Token::name,
   ,     .  ,   . ,
,      :

   #include "Token.h"

   Token tok( "function", 78 );
   char *tokName = tok; // :  
   *tokname = 'P'; //     name  Punction!

           Token 
 . ,     const char*:

   typedef const char *cchar;

   class Token {
   public:
      operator cchar() { return name; }
      // ...
   };

   // :  char*  const char*  
   char *pn = tok;
   const char *pn2 = tok; // 

         Token  char*   string 
  C++:

                                                           ++   754

   class Token {
   public:
      Token( string, int );
      operator SmallInt() { return val; }
      operator string() { return name; }
      operator int() { return val; }
      //   
   private:
      SmallInt val;
      string name;
   };

     Token::operator string()      (
   ) ,   .  
    name  Token.
          ? ,   
    int(),    Token?

   extern void calc( double );
   Token tok( "constant", 44 );
   //    int()? 
   //    int --> double
   calc( tok );

      (   double)      (
  int),        ,  
  ,      
. (     9.3.)    
calc()  Token::operator int()   tok   Token 
 int.       int   double 
 .
         
.         
,      . ,
   Token   operator int(),    
:

   extern void calc( int );
   Token tok( "pointer", 37 );
   //  Token::operator int()  ,
   //       
   calc( tok );

     Token::operator int()  ,   tok   int
      . 
  tok       Token   SmallInt 
 

                                                           ++   755


   Token::operator SmallInt()

         int      

   Token::operator int()

    calc(tok)    ,     
   Token   int.
            , 
     :

   class Date {
   public:
      //  ,    !
      operator int();
   private:
      int month, day, year;
   };

        int()  Date?   
       ,    
 ,     Date,    
     .      
 .

                             15.9.2.   

     ,   , ,
SmallInt(int)  SmallInt,     
  SmallInt. ,  SmallInt(int)   
int    SmallInt.

   extern void calc( SmallInt );
   int i;
   //   i    SmallInt
   //    SmallInt(int)
   calc( i );

     calc(i)  i     SmallInt  
 SmallInt(int),     
  .       calc(),    
    :

                                                           ++   756

   //   C++
   //     SmallInt
   {
      SmallInt temp = SmallInt( i );
      calc( temp );
   }

            : 
    .
          :

   class Number {
   public:
      //    Number    SmallInt
      Number( const SmallInt & );
      // ...
   };

        SmallInt   ,  
  Number:

   extern void func( Number );
   SmallInt si(87);

   int main()
   { //  Number( const SmallInt & )
      func( si );
      // ...
   }

         ,   
        ?
   ,       SmallInt(int),   
SmallInt,   dobj   SmallInt?

   extern void calc( SmallInt );
   double dobj;

   //   SmallInt(int)? 
   // dobj    double  int
  //  
   calc( dobj );

    ,     
   ,   , 
  .    
calc()   dobj   double   int. 
      SmallInt  SmallInt(int).

                                                           ++   757

          
     ,    . 
 ,   Number(const SmallInt&)   
     Number   SmallInt,    
     .   
 ,    (explicit):

   class Number {
   public:
      //      
      explicit Number( const SmallInt & );
      // ...
   };

           
 :

   extern void func( Number );
   SmallInt si(87);

   int main()
   { // :      SmallInt  Number
      func( si );
      // ...
   }

            , 
       :

   SmallInt si(87);

   int main()
   { // :      SmallInt  Number
      func( si );
      func( Number( si ) ); // :  
      func( static_cast< Number >( si ) ); // :  
   }

                          15.10.   A

          
.    ,  ,  ,
      
   . ,  ,  
        
  .

                                                           ++   758

         
    ,   
    .    :

      ->
         ->
           

      
.
    ,          
   ,   
    . ,   .
        . ,    Number
 : operator int()  operator float(),    
  Number    float. ,  
 Token::operator float()   .  
Token::operator int()  ,       
int , ,      float   
.    ,   
 ?  -     ?

   class Number {
   public:
      operator float();
      operator int();
      // ...
   };

   Number num;
   float ff = num; //  ? operator float()

          
     , 
  .       
:

  1. operator float() ->  
  2. operator int() ->  

        9.3,     .
    ,  ,  
Token::operator float().
     ,         
 .      
,   :

                                                           ++   759

   class SmallInt {
   public:
      SmallInt( int ival ) : value( ival ) { }
      SmallInt( double dval )
         : value( static_cast< int >( dval ) );
      { }
   };

   extern void manip( const SmallInt & );

   int main() {
      double dobj;
      manip( dobj ); // : SmallInt( double )
   }

      SmallInt     SmallInt(int) 
SmallInt(double),        double 
  SmallInt: SmallInt(double)  double  SmallInt
,  SmallInt(int)     
double  int.  ,    
 :

  1.   -> SmallInt( double )
  2.   -> SmallInt( int )

        ,  
 SmallInt(double).
      ,   .  ,  
  ,    ,   .  
      . ,  
 Number   :

   class Number {
   public:
      operator float();
      operator int();
      // ...
  };

      Number   long. 
   ,    
   :
 
   // :    float(),   int()
   long lval = num;

     num    long    :

   1. operator float() ->  
   2. operator int() ->  

                                                           ++   760

           
 ,      
       .
            :

   // :   
   long lval = static_cast< int >( num );

        Token::operator int(),  
    long.
          
,        . :

   class SmallInt {
   public:
      SmallInt( const Number & );
      // ...
   };

   class Number {
   public:
      operator SmallInt();
      // ...
   };

   extern void compute( SmallInt );
   extern Number num;

   compute( num ); // :   

   num    SmallInt   :  
 SmallInt::SmallInt(const Number&)    
Number::operator SmallInt().     , 
 .
           
Number:

   // :    
   compute( num.operator SmallInt() );

           
,    ,    ,
  ,   :

   compute( SmallInt( num ) ); // : - 

    ,       
,  .    . 

                                                           ++   761

      ( ,
   )     .

               15.10.1.      

     9  ,     . 
      ,     
   ,        
. ,        
      -.
          .  
       
 .       ,   
      
 ,    .
        ,     
      -   
       
 .

                               15.10.2. -

   -      ,   .
,    :

   SmallInt si(15);
   add( si, 566 );

   -    add.    add()  
? ,     .
   ,   add(),     , 
   :

   const matrix& add( const matrix &, int );
   double add( double, double );

   int main() {
      SmallInt si(15);
      add( si, 566 );
      // ...
   }

    ,      ,   
     .      
    :

          ,     
               

                                                           ++   762

 ,    -  ,
         ,   :

   namespace NS {
      class SmallInt { /* ... */ };
      class String { /* ... */ };
      String add( const String &, const String & );
   }

   int main() {
      // si   class SmallInt:
      //      NS
      NS::SmallInt si(15);

      add( si, 566 ); // NS::add() - -
      return 0;
   }

    si   SmallInt, ..  ,    
NS.    -  add(const String &,
const String &),     ;

          ,     
              ,    ,  
     ,      -:

     namespace NS {
        class SmallInt {
          friend SmallInt add( SmallInt, int ) { /* ... */ }
        };
     }
  
     int main() {
        NS::SmallInt si(15);
        add( si, 566 ); // - add() - 
        return 0;
     }

      si   SmallInt. -  SmallInt
    add(SmallInt, int)     NS,    
       .     NS -  
    .    add()     SmallInt
             
     ,     .

    ,        , 
   ,      ,   -
    ,    ,   
   ,    ,    
.
     :

                                                           ++   763

   namespace NS {
      class SmallInt {
         friend SmallInt add( SmallInt, int ) { /* ... */ }
      };

      class String { /* ... */ };
      String add( const String &, const String & );
   }

   const matrix& add( const matrix &, int );
   double add( double, double );

   int main() {
      // si   class SmallInt:
      //      NS
      NS::SmallInt si(15);
      add( si, 566 ); //  -
      return 0;
   }

     :

    :

   const matrix& add( const matrix &, int )
   double add( double, double )

      :


    NS::add( const String &, const String & )

   -:

    NS::add( SmallInt, int )

       -  SmallInt NS::add(
SmallInt, int )    :    
   .
   ,        , 
        .    
    .  -    
 ,   ,   - . 
        
       -,   
.

            15.10.3. -     
                                 

     

                                                           ++   764


   calc(t)

     (,  -),  
  ,     (.. ,
  ,    ),    
- .      
. (      13.9  13.12.)
 :

   namespace NS {
      struct myClass {
         void k( int );
         static void k( char* );
         void mf();
   };

   int k( double );
   };

   void h(char);
   void NS::myClass::mf() {
   h('a'); //   h( char )
   k(4); //  myClass::k( int )
   }

       13.11,  NS::myClass::  
 :      ,  
 - mf(),    myClass,    
 NS.   :

   h( 'a' );

      h()   - mf() 
 - myClass.  -    
    ,        NS.
 h()  ,       .
    h(char),  -,  
 .
       ,  . ,
    ,     
,     .    
     

   k( 4 );

          myClass.    
- k(int)  k(char*).     
,    ,    , 
  NS     k(double)    
.

                                                           ++   765

    ,   ,     
 ,      .  ,
   ,     
.

           15.10.4.   
                          

            
      .
     ? ,    
calc(),     ?

   class SmallInt {
   public:
      SmallInt( int );
   };

   extern void calc( double );
   extern void calc( SmallInt );
   int ival;

   int main() {
      calc( ival ); //  calc() ?
   }

    ,       
 .       
 .      , 
  ,  .   
 ,        ,   
 ,        ,    
.
        
  . ,   calc()   
  calc()  . calc(double)  , 
      int  
  double,  calc(SmallInt)  ,   
   int  SmallInt,   
SmallInt(int). ,      calc(double).
         
?         ,
      :

                                                           ++   766

   class Number {
   public:
      operator SmallInt();
      operator int();
      // ...
   };

   extern void calc( int );
   extern void calc( SmallInt );
   extern Number num;
   calc( num ); // : 

      calc(int),  calc(SmallInt);    
Number::operator int()    Number 
  int,   ,   Number::operator SmallInt()
    Number    
SmallInt.      
   ,     ,    .
 ,         .
   ,   :

   //     
   calc( static_cast< int >( num ) );

          num   int 
  Number::operator int().    
  int,     calc(int),    
 .
   ,   Number    Number::operator int().  
 

   //   Number::operator SmallInt()
   calc( num ); // - ?

- ? ,   SmallInt   , 
   SmallInt  int.

   class SmallInt {
   public:
      operator int();
      // ...
   };

    ,   calc() ,   
  num   Number   SmallInt   

                                                           ++   767

Number::operator SmallInt(),       int  
SmallInt::operator SmallInt().    . ,  
     
  ,    .  
Number::operator int()  ,   calc(int)   ,
         num 
   int.
       Number::operator int()  
  calc(SmallInt),      .
      
     ,     
  ,    :

   class SmallInt {
   public:
      operator int();
      // ...
   };

   void manip( int );
   void manip( char );
   SmallInt si ( 68 );

   main() {
      manip( si ); //  manip( int )
   }

    manip(int),   manip(char)   ;   ,
  SmallInt::operator int()    
SmallInt     int,    ,    
 SmallInt  int,      
    char.  
   :

   manip(int) : operator int()-> 
   manip(int) : operator int()-> 

            ,  
       
.      ,   
   manip(int).
   ,       ,   
      
  .          15.9, 
 ,      
    :      , 
      

                                                           ++   768

    .      
    ,    .   
      
,          ,  
       .    ,  
      , 
  . :

   class SmallInt {
   public:
      operator int();
      operator float();
      // ...
   };

   void compute( float );
   void compute( char );
   SmallInt si ( 68 );

   main() {
      compute( si ); // 
   }

    compute(float),  compute(int)   . compute(float)  ,
  SmallInt::operator float()   SmallInt  
 float,  compute(char)  ,  SmallInt::operator int()
   SmallInt   int,    
   char.  ,  :

   compute(float) : operator float()-> 
   compute(char) : operator char()-> 

        ,   ,  
     .    
      . 
   .

                                    15.12

       C++   ,  
,   ,  .  
  .   ,   
   ?

                                    15.13

         SmallInt,   
 ,   :

                                                           ++   769

   istream& operator>>( istream &is, SmallInt &si )
   {
      return ( is >> is.value );
   }

                                    15.14

        
  .     ?

   class LongDouble {
      operator double();
      operator float();
   };

   (a) int ex1 = ldObj;
     extern LongDouble ldObj;
   (b) float ex2 = ldObj;


                                15.15

      -,   
   ,         .

                                15.16

      calc()        
?   ,    
,  ,      .

   class LongDouble {
   public:
      LongDouble( double );
      // ...
   };

   extern void calc( int );
   extern void calc( LongDouble );
   double dval;
 
   int main() {
      calc( dval ); //  ?
   }

                                                           ++   770

                    15.11.    - A

   -    ,      
       . 
           
   :

  1.  -.
  2.   .
  3.     .

           
  -.        .

                 15.11.1.   -

  -   :

   class myClass {
   public:
      void f( double );
      char f( char, char ); //  myClass::f( double )
      // ...
   };

       ,    , -  
   ,        
,    .     - 
   ,     
:

   class myClass {
   public:
      void mf();
     double mf(); // :   
     // ...
   };

         , -   
  .         
- ,       
 :

   class myClass {
   public:
      void mf();
      void mf(); // :  
      // ...

                                                           ++   771

   };

               
 .  -    ,
   .  ,      
, ,    ,    .
  -    ,  
 :

   class myClass {
   public:
      void mcf( double );
      static void mcf( int* ); //  myClass::mcf( double )
      // ...
   };

     -         
  .    ,   
,    ,      .

                            15.11.2. -

       -:

   mc.mf( arg );
   pmc->mf( arg );

 mc    myClass,  pmc       myClass.
       ,   
  myClass    mf().
       

   myClass::mf( arg );

     ,    
 myClass    mf(). :

                                                           ++   772

   class myClass {
   public:
      void mf( double );
      void mf( char, char = '\n' );
      static void mf( int* );
      // ...
   };

   int main() {
      myClass mc;
      int iobj;
      mc.mf( iobj );
  }

        main()    - mf(),
  myClass:

   void mf( double );
   void mf( char, char = '\n' );
   static void mf( int* );

      myClass      -   mf(), 
    . (      
   .  ,      ,   
 19.3.)       ,  
  .

                             15.11.3.  

        ,     
  .   ,   
       .
:

   class myClass {
   public:
      void mf( double );
      void mf( char, char = '\n' );
      static void mf( int* );
      // ...
   };

   int main() {
      myClass mc;
      int iobj;
      mc.mf( iobj ); //   - mf()? 
   }

        mf()  main()    :

                                                           ++   773
   void mf( double );
   void mf( char, char = '\n' );

   mf(double)  ,        
       iobj  int    double;
   mf(char,char)  ,      
           iobj  int
      char   .

          ,  
  , .   ,   
   ,      , 
       ,    
.
              
       
.   ,    - 
  .
       ,       
,    :

   class myClass {
   public:
      static void mf( int );
      char mf( char );
   };

   int main() {
      char cobj;
      myClass::mf( cobj ); //   -?
   }

    - mf()        
  myClass::mf().      (  ),
    (  ).   , 
- mf(char)        
  mf(int).
       :   
 ,    ,  
   .  cobj  char  
  mf(char)       
 mf(int).     ,   
mf(char).
     -    , ,  
       myClass     
.   ,     , ,   
(   ),    .

                                                           ++   774

      -,      
   ,     const 
volatile   . (    13.3.)  
    ?    myClass  
-:

   class myClass {
   public:
      static void mf( int* );
      void mf( double );
      void mf( int ) const;
      // ...
   };

      - mf(int*),    mf(int), 
  mf(double)     
  .        ?

   int main() {
      const myClass mc;
      double dobj;
      mc.mf( dobj ); //   - mf()?
   }

    ,      , 
,    mf(double)  mf(int).  double 
 dobj      mf(double)  
     mf(int)    .
      -      ,
           
 ,    .
   mc    ,      
 -. ,  - mf(double)
   ,       mf(int),
  .
           -? 
      const  volatile,    
   ?

   class myClass {
   public:
      static void mf( int );
      char mf( char );
   };

   int main() {
      const myClass mc;
      int iobj;
      mc.mf( iobj ); //     -?

                                                           ++   775

   }

    -       .
        . ,
    mc   mf(int).  
    -    
    .
    ,  -      
   const  volatile  ,    .
 -      
    .
      mc   ,  - mf(char) 
  .  - mf(int)   ,   
.     ,   
.

                      15.12.     A

           . ,
    :

   SomeClass sc;
   int iobj = sc + 3;

     ,   :    
 SomeClass    sc   ,   
  ?
          ,  
SomeClass.        
  .     ,   
   ,      .
             ,
   9.2:

  1.  -.
  2.   .
  3.     .

       .
       ,     
.       .
(         4.)
:

                                                           ++   776

   class SmallInt {
   public:
      SmallInt( int );
   };

   SmallInt operator+ ( const SmallInt &, const SmallInt & );

   void func() {
      int i1, i2;
      int i3 = i1 + i2;
   }

     i1  i2   int,    ,   
   +.  operator+(const SmallInt &,
const SmallInt &) ,       SmallInt 
      
SmallInt(int).         
.
    ,        
  :

   void func() {
      SmallInt si(98);
      int iobj = 65;
      int res = si + iobj; //   
  }

         :

   int res = operator+( si, iobj ); //   

          (.
 15.10).      -:

   //   -
   int res = si.operator+( iobj );

     - (.  15.11).

                        15.12.1.  -

      ,      ,   .
    

   SmallInt si(98);
   int iobj = 65;
   int res = si + iobj;

                                                           ++   777

 -  operator+.   operator+
  ?
          , 
 ,    .     ,    
     :

    ,    .  
    operator+(),     ,  .
    , operator+(),     , 
        operator+()  main():

    SmallInt operator+ ( const SmallInt &, const SmallInt & );

    int main() {
       SmallInt si(98);
       int iobj = 65;
      int res = si + iobj; // ::operator+() - -
    }

    ,    ,   
     .          
      ,   ,  
           ,    ,
     :

    namespace NS {
       class SmallInt { /* ... */ };
       SmallInt operator+ ( const SmallInt&, double );
   }

   int main() {
      // si   SmallInt:
      //       NS
      NS::SmallInt si(15);
      // NS::operator+() - -
      int res = si + 566;
      return 0;
    }

     si    SmallInt,     NS.
      operator+(const SmallInt, double),  
      ,    ;

    ,   ,   
    .           
        -,   
      :

                                                           ++   778

    namespace NS {
       class SmallInt {
          friend SmallInt operator+( const SmallInt&, int )
          { /* ... */ }
       };
    }
 
    int main() {
       NS::SmallInt si(15);
       // - operator+() - 
       int res = si + 566;
       return 0;
    }

    si   SmallInt.   operator+(const SmallInt&,
int),    ,     NS, 
      .     NS 
    .    operator+() 
  SmallInt -,     
,        .
       -    ,  
       .   
     :

    -,     . 
       operator+()   ,    -
       operator+(),   
    :

    class myFloat {
       myFloat( double );
    };

    class SmallInt {
    public:
       SmallInt( int );
       SmallInt operator+ ( const myFloat & );
    };

    int main() {
       SmallInt si(15);
       int res = si + 5.66; // - operator+() - 
   }

    - SmallInt::operator+(const myFloat &),   SmallInt,
       -    operator+() 
    main();

     .  ,  
       operator+(),   :

                                                           ++   779

    T* operator+( I, T* );
    int operator+( int, int );
    double operator+( double, double );
    T* operator+( T*, I );
   T* operator+( I, T* );

            
      ,         
     .      
      ,     
      .      
          ,   
            .

          . ,  
  SmallInt     operator+(),   
 .
      -   
,  :

   namespace NS {
         class myFloat {
            myFloat( double );
      };

      class SmallInt {
         friend SmallInt operator+( const SmallInt &, int ) { /* ... */ }
      public:
         SmallInt( int );
         operator int();
         SmallInt operator+ ( const myFloat & );
         // ...
      };
      SmallInt operator+ ( const SmallInt &, double );
   }

   int main() {
      //  si - class SmallInt:
      //       NS
      NS::SmallInt si(15);
      int res = si + 5.66; //  operator+()?
      return 0;
   }

          -  
operator+()  main():

     .    ,    
     operator+()   main(),   
     operator+();
        ,     NS,
       SmallInt.      :

    NS::SmallInt NS::operator+( const SmallInt &, double );

                                                           ++   780

      ,   
    SmallInt.  

    NS::SmallInt NS::operator+( const SmallInt &, int );

      ,   SmallInt.
      :

    NS::SmallInt NS::SmallInt::operator+( const myFloat & );

        :

    int operator+( int, int );
    double operator+( double, double );
   T* operator+( T*, I );
   T* operator+( I, T* );

   ,      ,  
  , .      ,
      ,   ,  
,     .

                       15.12.2.  

          
    ,      
. ,       ? 
   :

   NS::SmallInt si(15);
   si + 5.66;

       SmallInt,    double.
          
operator+():

   NS::SmallInt NS::operator+( const SmallInt &, double );

      SmallInt     
 -   . ,  
double,      .
 -  :

NS::SmallInt NS::operator+( const SmallInt &, int );

  si  SmallInt     
 -  .    int 

                                                           ++   781

          
.
      -:

   NS::SmallInt NS::SmallInt::operator+( const myFloat & );

     si   SmallInt, ..   ,   
 .    int      myFloat 
      
myFloat(double).
          :

   int operator+( int, int );
   double operator+( double, double );

    SmallInt  ,      SmallInt 
 int.         
     int.    double
   int    .  
  ,        SmallInt
  int,       double.   
 double    .
          , operator+(),  
  NS:

   NS::SmallInt NS::operator+( const SmallInt &, double );

        .

                              15.12.3. 

          ,    
 ,        
  . ,     String  
:

   class String {
      // ...
   public:
      String( const char * = 0 );
      bool operator== ( const String & ) const;
      //   operator== ( const char * )
   };

    operator==:

                                                           ++   782

   String flower( "tulip" );
   void foo( const char *pf ) {
      //    String::operator==()
      if ( flower == pf )
         cout << pf << " is a flower!\en";
      // ...
  }

     

   flower == pf

    String:

   String::operator==( const String & ) const;

       pf   const char*   String 
operator==()    , 
 :

   String( const char * )
  
        String    const char*:

 class String {
      // ...
   public:
      String( const char * = 0 );
      bool operator== ( const String & ) const;
      operator const char*(); //  
   };

   operator==()  :

      //      !

   if (flower == pf)

   -   operator const char*()   

   bool operator==( const char *, const char * )

   .      flower  String
     const char *.
      operator==()  foo()    
.   

   String::operator==( const String & ) const;

                                                           ++   783

       pf
  const char*   String. 

   bool operator==( const char *, const char * )

      flower  

   String   const char*.

    ,       ,    
.     ,   
  .
      ,   
,   ,    .
     . 
   ,       
      .

                                15.17

      -,   
     .

                                15.18

      operator+()       
    main()?   -,  
   ,       
 .

   namespace NS {
      class complex {
         complex( double );
         // ...
      };
 
      class LongDouble {
         friend LongDouble operator+( LongDouble &, int ) { /* ... */ }
      public:
         LongDouble( int );
         operator double();
         LongDouble operator+( const complex & );
         // ...
      };
   
      LongDouble operator+( const LongDouble &, double );
   }

   int main() {
      NS::LongDouble ld(16.08);
      double res = ld + 15.05; //  operator+?
      return 0;
   }
