15
        15.      

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

                          15.1.  

                        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()   :

   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 * );

,        
:

   "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     .)
   ,     ,      
  ?        :

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

   // :    
   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++    :

   :: .* . ?:

          .
,        
,      .

   // :      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.   

    ,       
,      .     .
   ,      ,
    .     
  .

                                        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().
         
  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;
   }

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

   }
   //  ..


         ,   
,    ,     . 

                                                                         
      ,  
   -    .


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

   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 & );
   // ...
   };

              , 
    :

   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.3.  =

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

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

  ,    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*.
     _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: ,  ,
 :

   String ver, noun;
   verb = noun = "count";

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

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

          , 
   String.      
  - .

                           15.4.   

      operator[]()    , 
 ,     .   
   - .

         ,     
.     ,    l-
 .     :

   #include <cassert>

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

   String color( "violet" );

         color   'V':

   color[ 0 ] = 'V';


                                15.5.   

     ,   ,     
 . ,       int
   absInt:

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

        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() );
      // ...
   }

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

   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;
   }


                               15.6.  

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

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

     ScreenPtr   ,     
   Screen:     ,    
.       ScreenPtr, 
,     -  Screen.   
  ScreenPtr  ,     

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

        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*:

   #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, 
    :

   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--();
      // ...
   };

         . 
   ,  ,  :
 
   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.    
         
ScreenPtr:

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

        :

   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:

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

          
                         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;

  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().
    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:

   #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.) ,      ,   ,
        (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:

   //  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*     
 ,    .
        - 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 * );
      // ...
   };

       ,  
 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()  :

   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.   

       ,     
    .   
       .
 ,   ,     SmallInt,  
 10.9.
        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.   :

   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:

   #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    :

   #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;
   };

     -,    :

   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. ,
        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 ); // :   
      // ...
   };

         .  
   ,    ,     
  ,    :

   #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 );

752

752

     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++:

   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 
 

   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(),    
    :

   //   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).


          
     ,    . 
 ,   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

          
.    ,  ,  ,
      
   . ,  ,  
        
  .
         
    ,   
    .    :

      ->
         ->
          

      
.
    ,          
   ,   
    . ,   .
        . ,    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().

     ,         
 .      
,   :

   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() ->  

           
 ,      
       .
            :

   // :   
   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 ) ); // : - 

    ,       
,  .    . 
      ( ,
   )     .

               15.10.1.      

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

760
                          15.10.2. -
       15.10.3. -       
   15.10.4.     

                15.11.    - A

   -    ,      
       . 
           
   :

  1.  -.
  2.   .
  3.     .

            
  -.        .

                       15.11.1.   -
                             15.11.2. -
                            15.11.3.  
                    15.12.     A
                      15.12.1.  -
                           15.12.2.  
                             15.12.3. 

783

