                                                         ++   665

      14. ,    

          ,  
    .   
      (,
),       
   .      ,
,        
      ,  
  ,     .
      ,      
  , ..    
.    ,    
  .    
      
.      -  ,  
  .

                         14.1.  

      :

   class Data {
   public:
      int ival;
      char *ptr;
   };

       ,  
  .       
. ,   ival     ?
      ?     
,   ,  .    
  ,  ptr, ,    , 
ival    .      .
       ,   
 ival.  ,   Data    
:    ival         ptr.
   ival   1;   
  0,   .
           , ,  
    ,      

                                                         ++   666

 .     ,  
       
.       
 ,    . ,  
       
Data:

   Data dat01( "Venus and the Graces", 107925 );
   Data dat02( "about" );
   Data dat03( 107925 );
   Data dat04;

     (    dat04),     ,  
     . ,    . 
   ,   ,  ,  
    .  ,   
  ,  ,     .  
    ,    
  .  ,     ,
    ,      .
       Data  ? ,     .
   C    ,
    :

   int main()
   {
      // local1.ival = 0; local1.ptr = 0
      Data local1 = { 0, 0 };

      // local2.ival = 1024;
      // local3.ptr = "Anna Livia Plurabelle"
      Data.local2 - { 1024, "Anna Livia Plurabelle" };
      // ...
   }

     ,   ,    -
.      ,   ival  
ptr:

   // : ival = "Anna Livia Plurabelle";
   // ptr = 1024
   Data.local2 - { "Anna Livia Plurabelle", 1024 };

        . -,   
    ,     (.. 
          
   C,   ).  -,   

                                                         ++   667

 ,      (
        
).
          ? . 
       
   .  ,    
         
       .   
    ,      
,      .      
 1.
          
,        
   .       
.

                               14.2.  

  -   ,     
 .       2:

   class Account {
   public:
      //    ...
      Account();
      // ...
   private:
      char *_name;
      unsigned int _acct_nmbr;
      double _balance;
   };

     ,   ,   ,
       ,  void.  
 :

   // :        
   void Account::Account() { ... }
   Account* Account::Account( const char *pc ) { ... }

   1         
       .  [LIPPMAN96a].
   2        _name    string.
         C-,     
          14.4.

                                                               ++   668

          ,     
   .
     ,     ?  , 
    ,    . ,
    ,     , 
  . ,     .
        _name  _balance:

   Account( const char *name, double open_balance );

     Account,  ,   
:

   Account newAcct( "Mikey Matz", 0 );

       ,      0,   
,       
_balance .         :

   Account( const char *name );

              ,
 :

   Account( const char *name, double open_balance = 0.0 );

        , 
  .      ,
        .
          
 ?        . 
   ,       ,
         
Account,     :

   class Account {
   public:
       //    ...
       Account();
       //      
       Account( const char*, double=0.0 );
       const char* name() { return name; }
       // ...
   private:
      // ...
   };

           Account, 
     :

                                                         ++   669

   int main()
   {
      // :     
      //   
      Account acct( "Ethan Stern" );
      Account *pact = new Account( "Michael Lieberman", 5000 );

      if ( strcmp( acct.name(), pact->name() ))
          // ...

   }

   C++ ,         
.  ,    acct,    ,   
pact,        if.
      ,   .  , 
 ,    acct  main():

   //   C++,
   //    

   int main()
   {
      Account acct;
      acct.Account::Account("Ethan Stern", 0.0);

      // ...
}


   ,     ,     
.
     new  .    ,
    .   pact  
   :

   //   C++,
   //       new

   int main()
   {
      // ...
      Account *pact;
      try {
         pact = _new( sizeof( Account ));
         pact->Acct.Account::Account(
         "Michael Liebarman", 5000.0);
      }
      catch( std::bad_alloc ) {
         //  new  :
         //   
      }

      // ...
   }

                                                         ++   670

            :

   //      
   Account acct1( "Anna Press" );
   Account acct2 = Account( "Anna Press" );
   Account acct3 = "Anna Press";

    acct3       . 
   ,     acct1,  
 acct2.
         , 
  :

   // !   ,  
   Account newAccount();

    .      
 :

   //   ...
   if ( ! newAccount.name() ) ...

          .
   

   //   newAccount,
   //    
   Account newAccount();
 
      , 
   Account.    ,
   ,    :

   // :    ...
   Account newAccount;

     ,     ,   
,        ,   

                                                            ++   671

 .        ,  
   ,      .  ,  
  ,     ,  
   ,       
   .  ,    
      Account,    
  .     Account  
:

   class Account {
   public:
      //      
      Account( const char*, double=0.0 );
      const char* name() { return name; }
      // ...
   private:
      // ...
   };

        Account    
     C-,     .
?   (, vector) ,   
         ,  
 .      
   . ,     
    Account:

   // :       Account
   Account *pact = new Account[ new_client_cnt ];

          ,   -
  .
           ? ,  Account
       .    
    ,   ,    
  :

   //      Account
   inline Account::
   Account() {
      _name = 0;
      _balance = 0.0;
     _acct_nmbr = 0;
   }

     -  Account    
   .

                                                            ++   672

      :   ,   
     . ,  
    :

   //     Account  
   //   
   inline Account::
   Account()
      : _name(0),
         _balance( 0.0 ), _acct_nmbr( 0 )
   {}

        ,     . 
         .
          
  :

   inline Account::
   Account( const char* name, double opening_bal )
      : _balance( opening_bal )
   {
      _name = new char[ strlen(name)+1 ];
      strcpy( _name, name );
      _acct_nmbr = get_unique_acct_nmbr();
   }

get_unique_acct_nmbr()      -, 
      .
         const  volatile (. 
13.3.5),    :

   class Account {
   public:
      Account() const; // 
      Account() volatile; // 
      // ...
   };

     ,       
 .     
,       . 
    ,      ,
     .  ,   
 const      
    .       
volatile.
      :

                                                            ++   673

   //  -  
   extern void print( const Account &acct );

   // ...

   int main()
   {
      //   "oops"    Account
      //    Account::Account( "oops", 0.0 )
      print( "oops" );
      // ...
   }

         (     , 
 ,  ,    )   
.      Account  
        Account 
 print(),        .
      ,   "oops"
   Account,     .
   C++     explicit, 
,     :

   class Account {
   public:
      explicit Account( const char*, double=0.0 );
   };

        . (  
 explicit    15.9.2.)

                         14.2.1.   

       ,   , 
 .   ,      
;         
:

   //     
   Account::Account() { ... }
   iStack::iStack( int size = 0 ) { ... }
   Complex::Complex(double re=0.0, double im=0.0) { ... }

  :
   
                                                         ++   674
   int main()
   {
      Account acct;
      // ...
   }

   ,     Account  
.     :

  1.   .     acct.
  2.  ,    .     acct
        :   main()   .
  3.     ,      ,
       .  acct   : 
        .
  4.     ,  - .  
     , acct  ,   .

    1  3      (   , 
 )      2  4.
   ,     Account      
:

   class Account {
   public:
      char *_name;
      unsigned int _acct_nmbr;
      double _balance;
   };

          Account   
.         , 
  . ,    ,
      (   ,   
):

   //   
   //      
   Account global_scope_acct;
   static Account file_scope_acct;

   Account foo()
   {
      static Account local_static_acct;
      // ...
   }


                                                            ++   675

    ,     ,  
     ,    :

   //        
   //      

   Account bar()
   {
      Account local_acct;
      Account *heap_acct = new Account;
      // ...
   }

     ,     ,  
 ,       .  Account   , 
   ,  .      
.    ,  ,   
,   ,   :  
   ,       
    , ,    .
     ,    ,   
  ,     .  
        , 
      ,3  .

               14.2.2.     

      ,      . 
        , 
     .   
    Account  ,     
:

   class Account {
   friend class vector< Account >;
   public:
      explicit Account( const char*, double = 0.0 );
      // ...
   private:
      Account();
      // ...
   };

   3  ,     C:   
      Account  C   :

      typedef struct {
         char *_name;
         unsigned int _acct_nmbr;
         double _balance;
     } Account;

                                                            ++   676

          Account,   
  ,    .  - Account 
   vector   ,  
.
   ,   ,    C++  
 :

            
    (     );
     ,       , 
            ,   
     ,      (.
       -  
     17).

                        14.2.3.  

           
  .       
    .  
   ,    .
  ,    ,    
    .
          . 
    .    Account 
,        ,  
 .
           
  (    const).  
:

   inline Account::
   Account( const Account &rhs )
      : _balance( rhs._balance )
   {
      _name = new char[ strlen(rhs._name) + 1 ];
      strcpy( _name, rhs._name );

     //  rhs._acct_nmbr 
    _acct_nmbr = get_unique_acct_nmbr();
   }

     :

   Account acct2( acct1 );

 ,       
Account.     ,    ;   , 

                                                            ++   677

 acct2  .  ,    
,     .  
      ,  
     .     
. (       14.6.)

                                14.1

       ? ?

  1.        .
  2.          .
  3.        ,    
       .
  4.       ,    
             
      .

                                 14.2

           .
  :

   class NoName {
   public:
      //    
      // ...
   protected:
      char *pstring;
      int ival;
      double dval;
   };

                                  14.3

        (   ). ,
  ( )    
 .    .  
.

    
    
    
     
    
    

                              14.4

      :

                                                        ++   678

   class Account {
   public:
      Account();
      explicit Account( const char*, double=0.0 );
      // ...
   };

,      :

   (a) Account acct;
   (b) Account acct2 = acct;
   (c) Account acct3 = "Rena Stern";
   (d) Account acct4( "Anna Engel", 400.00 );
   (e) Account acct5 = Account( acct3 );

                            14.5

          ,   
.    :

   Account::Account( const Account rhs );

                         14.3.  

     ,   ,    
.        Account ,   
 new        
 .    ,    
        .   
 ,     
      ,  .   
   -,  
,            
  delete.        
   (~).      
  ,  ,    . 
    -,     
    . , ,    
Account:

                                                            ++   679

   class Account {
   public:
      Account();
      explicit Account( const char*, double=0.0 );
      Account( const Account& );
      ~Account();
      // ...
   private:
      char *_name;
      unsigned int _acct_nmbr;
      double _balance;
   };

   inline
   Account::~Account()
   {
      delete [] _name;
      return_acct_number( _acct_nnmbr );
   }

    ,        :

   inline
   Account::~Account()
   {
      // 
      delete [] _name;
      return_acct_number( _acct_nnmbr );
      // 
     _name = 0;
     _balance = 0.0;
     _acct_nmbr = 0;
   }

     ,        
 .   :

   class Point3d {
   public:
      // ...
   private:
      float x, y, z;
   };

        ,  
.   ? .    Point3d   
:          
  .
     ,      , ,  ,
   .      ,    
    .     

                                                            ++   680

,    ,     , 
   ,   new.
          .  
  ,      
      . , 
      
  Timer,        
 .       .
         ,
   ,  :

   {
      //    
   #ifdef PROFILE
      Timer t;
   #endif
      //  
      // t  
     //    ...
   }

      ,      (  
),   :

   (1) #include "Account.h"
   (2) Account global( "James Joyce" );
   (3) int main()
   (4) {
   (5) Account local( "Anna Livia Plurabelle", 10000 );
   (6) Account &loc_ref = global;
   (7) Account *pact = 0;
   (8)
   (9) {
   (10) Account local_too( "Stephen Hero" );
   (11) pact = new Account( "Stephen Dedalus" );
   (12) }
   (13)
   (14) delete pact;
   (15) }

      ? :     global
  (2);        local  local_too  
(5)  (10) ,    ,   ,   (11). 
  loc_ref     (6),    pact 
 (7)     .      
 ,     global.   
 ,   (     ,  (11)),
     ( (7)).
      :    global,
   (2),           
delete   (14).     ,    

                                                            ++   681

 .       
 ,      .
          
    .    
      ,   ,    
,      ,   C++ 
.4
     ,         
 (    ).
   ++        delete 
,    ,     
:

   // :   
   if (pact != 0 ) delete pact;

    ,         ,
  ,     auto_ptr,   
 (.   auto_ptr   8.4).    ,
   delete (,  ,   )
     ,      .  
 ,    auto_ptr ( 
,     auto_ptr     
       auto_ptr):

   #include <memory>
   #include "Account.h"

   Account global( "James Joyce" );

   int main()
   {
      Account local( "Anna Livia Plurabelle", 10000 );
      Account &loc_ref = global;
      auto_ptr<Account> pact( new Account( "Stephen Dedalus" ));
      {
         Account local_too( "Stephen Hero" );
      }

     //  auto_ptr  

   }

  4 .     [LIPPMAN96b],    
        ,   
    .

                                                            ++   682

                         14.3.1.   

          .  
       new (.  8.4). 
.   :

   char *arena = new char[ sizeof Image ];

    ,       Image,  
    .   :

   Image *ptr = new (arena) Image( "Quasimodo" );

     .    ptr 
,    arena.  ,    ptr,
     Image,    
  .  ,   new() 
      .
       Quasimodo,    - 
  Esmerelda,      arena :

   Image *ptr = new (arena) Image( "Esmerelda" );

     Quasimodo    ,     
    .      
Image,      delete:

   // :    ,    
   delete ptr;

,   ,      ,    
.        Image:

   ptr->~Image();

        
 new.
   , ,  ptr  arena         ,
  delete  arena

   //   
   delete arena;

      Image,   arena   char*, 
    ,    delete 
   ,  .

                                                            ++   683


                 14.3.2.    

          
,          
   . ,   

   Account acct( "Tina Lee" );

   int swt;
   // ...
   switch( swt ) {
      case 0:
         return;
      case 1:
        // - 
        return;
     case 2:
        //  - 
        return;
    //   
   }

      return.  
Account ,          . 
      , 
 .     return    case
   break  ,      
:

   //      
   switch( swt ) {
      case 0:
         break;
     case 1:
        // - 
        break;
     case 2:
        //  - 
        break;
        //   
   }
   //   
   return;

                                14.6

          , 
 pstring     :

                                                            ++   684
   class NoName {
   public:
      ~NoName();
      // ...
   private:
      char *pstring;
      int ival;
      double dval;
   };

                                   14.7

       ,      14.3?  ,
 .     .

                                   14.8

         :

   void mumble( const char *name, fouble balance, char acct_type )
   { 
      Account acct;
      if ( ! name )
         return;

   if ( balance <= 99 )
      return;

   switch( acct_type ) {
      case 'z': return;
      case 'a':
      case 'b': return;
   }
   // ...
   }


                         14.4.    

         ,    
. :

   Account table[ 16 ];

   16  Account.    
   .     
        .
:

   Account pooh_pals[] = { "Piglet", "Eeyore", "Tigger" };

    ,  :

                                                            ++   685

   Account( "Piglet", 0.0 ); //   ()
   Account( "Eeyore", 0.0 ); //   (-)
   Account( "Tigger", 0.0 ); //   ()

       ,    .    
 ,      :

   Account pooh_pals[] = {
      Account( "Piglet", 1000.0 ),
      Account( "Eeyore", 1000.0 ),
      Account( "Tigger", 1000.0 )
   };

           , 
      :

   Account pooh_pals[] = {
      Account( "Woozle", 10.0 ), // 
      Account( "Heffalump", 10.0 ), // 
      Account();
   };

           :

   Account pooh_pals[3] = {
      Account( "Woozle", 10.0 ),
      Account( "Heffalump", 10.0 )
   };

    ,      
   .  ,     
,    .   ,   
        .
            
 ,         . :

   pooh_pals[0];

  Piglet, 

   pooh_pals[1];

 Eeyore  ..     ,    
,         :

   pooh_pals[1]._name != pooh_pals[2]._name;

                                                            ++   686

           ,  
   .       
  new,       ,  
  .         .
   

   Account *pact = new Account[ 10 ];

  ,   ,      Account,
     .
     ,   pact,  
 delete.  

   // !    
   delete pact;

,   pact       . 
   Account      .
     ,      
  delete    :

   // :
   // ,  pact  
   delete [] pact;

        ,  pact   . 
,    ,       .

            14.4.1.  ,    A

       ,   ,  
 :    ,     
  ,   ,    
 .
        ,    
  :       
         
,     .     
 ,     new:

                                                            ++   687

   #include <utility>
   #include <vector >
   #include <new>
   #include <cstddef>
   #include "Accounts.h"

   typedef pair<char*, double> value_pair;

   /* init_heap_array()
    *    -
    *       
    *  
    * init_values:     
    * elem_count:    
    *  0,      
    * init_values
    */
   Account*
   Account::
   init_heap_array(
      vector<value_pair> &init_values,
      vector<value_pair>::size_type elem_count = 0 )
  {
      vector<value_pair>::size_type
          vec_size = init_value.size();

      if ( vec_size == 0 && elem_count == 0 )
         return 0;

      //     elem_count,
      // ,  elem_count == 0,   ...
      size_t elems = elem_count
         ? elem_count : vec_size();

      //      
     char *p = new char[sizeof(Account)*elems];

      //      
      int offset = sizeof( Account );
      for ( int ix = 0; ix < elems; ++ix )
      {
         //  ix- 
         //     ,
         //   ;
         //       

         if ( ix < vec_size )
            new( p+offset*ix ) Account( init_values[ix].first,
                                                    init_values[ix].second );
         else new( p+offset*ix ) Account;
      }

      // :    ;
      //     
      return (Account*)p;
   }

       ,    
,   ,       
 .     :

                                                            ++   688

   char *p = new char[sizeof(Account)*elems];

         ,     
p          , 
   ,    :

   for ( int ix = 0; ix < elems; ++ix )
   {
      if ( ix < vec_size )
         new( p+offset*ix ) Account( init_values[ix].first,
                                                 init_values[ix].second );
      else new( p+offset*ix ) Account;
   }

     14.3 ,    new  
      .     
new      Account   
  .     
     ,    
  .  delete   :

   delete [] ps;

   ?   ps ( ,     
 init_heap_array())    ,    
  new,      
.       :

   void
   Account::
   dealloc_heap_array( Account *ps, size_t elems )
   {
      for ( int ix = 0; ix < elems; ++ix )
         ps[ix].Account::~Account();

      delete [] reinterpret_cast<char*>(ps);
   }

           
    :

   new( p+offset*ix ) Account;

     ,     ps:

   ps[ix].Account::~Account();

     ps,  p       , ps    
  Account,  p     char.  p   ix- ,
  ix-   Account.   p    ,  ,

                                                            ++   689

     
.
         :

   class Account {
   public:
      // ...
      static Account* init_heap_array(
      vector<value_pair> &init_values,
      vector<value_pair>::size_type elem_count = 0 );
      static void dealloc_heap_array( Account*, size_t );
      // ...
   typedef pair<char*, double> value_pair;
   };


                              14.4.2.  

         , :

   vector< Point > vec( 5 );

      5:

  1.          ,
       . .
  2.       ,  
           .
  3.   .

          ,      
 :

Point pa[ 5 ];

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

   5      .
             
     .       ,  
        :

      explicit vector( size_type n, const T& value=T(), const Allocator&=Allocator());

                                                            ++   690

   vector< Point > cvs; // 
   int cv_cnt = calc_control_vertices();

   //     cv_cnt   Point
   // cvs    ...
   cvs.reserve( cv_cnt );
   //        
   ifstream infile( "spriteModel" );
   istream_iterator<Point> cvfile( infile ),eos;

   //     

   copy( cvfile, eos, inserter( cvs, cvs.begin() ));

( copy(),   inserter    
istream_iterator    12.)   list () 
deque ( )    vector ().
          
.

                               14.9

       ?  .

   (a) Account *parray[10] = new Account[10];
       "Roy", "Elena" };
   (b)    Account iA[1024] = {
     "Nhi", "Le", "Jon", "Mike", "Greg", "Brent", "Hank"

   (c) string *ps=string[5]("Tina","Tim","Chyuan","Mira","Mike");

                              14.10

          :   (, 
Account pA[10]),    ?   .
     Lut()    256     
Color.   .
           Account. 
   .
    gen_words(elem_size)      
  elem_size .

                              14.11

                                                            ++   691

         
    , ,    , ..
 

   // :  ,  parray  
   delete parray;



   // :   ,  parray
   delete [] parray;

          .   
     ( size ).    ,
   .      , 
.
        ++    ,  
       (   
)       :

   //        
       
   delete p[10] parray;

     ,      ,     
 ( ,      ),  ,   , 
 delete  (     , 
    )?      ?

                   14.5.   

      Account,   _name  string:

   #include <string>

   class Account {
   public:
      // ...
   private:
      unsigned int _acct_nmbr;
      double _balance;
      string _name;
   };

                                                            ++   692

       .   : 
        
   .
     Account   


   Account( const char*, double = 0.0 );

     string. :

   string new_client( "Steve Hall" );
   Account new_acct( new_client, 25000 );

  ,        
string   char*. 

   Account new_acct( new_client.c_str(), 25000 );

,      .     
  :

   Account( string, double = 0.0 );

    :

   Account new_acct( new_client, 25000 );

   ,    

   Account *open_new_account( const char *nm )
   {
      Account *pact = new Account( nm );
      // ...
      return pacct;
   }

-         .
       string     char*   string
(      ),    
  ,        string. 
 ,   :

   Account myAcct( "Tinkerbell" );

"Tinkerbell"      string.   
     .
           
  Account      

                                                            ++   693

 char* -    .   
    .   
 Account  :

   #include <string>

   class Account {
   public:
      Account();
      Account( const char*, double=0.0 );
      Account( const string&, double=0.0 );
      Account( const Account& );
      // ...
   private:
      // ...
   };

      ,     
  ?      :

  1.     ?    
      Account;
  2.    ?    
     Account      ,   
       string;
  3.     ,    ?
          Account   ,
          char*.

          (  
   14.2). ,  ,    
 ,       /. 
       (,  _name   ,
   string):

   inline Account::
   Account( const char* name, double opening_bal )
      : _name( name ), _balance( opening_bal )
  {
     _acct_nmbr = het_unique_acct_nmbr();
   }

             
.     ,     , 
   .     ,  
  ,   , 
  .     name   string,
    _name.  _balance  
opening_bal.
         :

                                                            ++   694

   inline Account::
   Account( const string& name, double opening_bal )
      : _name( name ), _balance( opening_bal )
   {
      _acct_nmbr = het_unique_acct_nmbr();
   }

         string,  
_name   name  string.
       :      
       ? ,  
 

   inline Account::
   Account( const char* name, double opening_bal )
      : _name( name ), _balance( opening_bal )
   {
      _acct_nmbr = het_unique_acct_nmbr();
   }



   Account( const char* name, double opening_bal )
   {
      _name = name;
      _balance = opening_bal;
      _acct_nmbr = het_unique_acct_nmbr();
   }

              .
  ,       , 
  .         
,  .     ,    .
            : 
      ,    
 .         
,   .       
 .
             ,   
 .      
        ,    
  ,   . (   
   17   -
.) ,  :

                                                            ++   695
   inline Account::
   Account()
   {
      _name = "";
      _balance = 0.0;
      _acct_nmbr = 0;
   }

    .     
     string,   
_name.  ,   _name   .
           .
,   ,     
,        .   
      Account:

   inline Account::
   Account() : _name( string() )
   {
      _balance = 0.0;
     _acct_nmbr = 0;
   }

       _name   .   
   string .   ,  
 :

   inline Account::
   Account()
   {
      _balance = 0.0;
      _acct_nmbr = 0;
   }

               .
,   ,    _balance:  
    ?    , 
  ,      ,   
   (  ).  
 :

   //   
   inline Account::
   Account() : _balance( 0.0 ), _acct_nmbr( 0 )
   {}

                                                            ++   696

           - 
 .       ,   
  :

   class ConstRef {
   public:
      ConstRef(int ii );
   private:
      int i;
      const int ci;
      int &ri;
   };

   ConstRef::
    ConstRef( int ii )
    { // 
       i = ii; // 
       ci = ii; // :    
       ri = i; // : ri  
   }

            
-   .       
.     :

   // :     
   ConstRef::
   ConstRef( int ii )
      : ci( ii ), ri ( i )
   { i = ii; }

             . 
       ,  
 .       Account:

   class Account {
   public:
      // ...
   private:
      unsigned int _acct_nmbr;
      double _balance;
      string _name;
   };

        

   inline Account::
   Account() : _name( string() ), _balance( 0.0 ), _acct_nmbr( 0 )
   {}

                                                            ++   697

 : _acct_nmbr, _balance, _name.  ,   
(    - ),  
,       . , 
 :

   inline Account::
   Account( const char* name, double bal )
      : _name( name ), _balance( bal )
   {
      _acct_nmbr = get_unique_acct_nmbr();
   }

  : _balance, _name, _acct_nmbr.
        
        , 
      :

   class X {
       int i;
       int j;
   public:
      //  ?
      X( int val )
         : j( val ), i( j )
      {}
      // ...
   };

   ,      i  j  
 val,     i  ,    
  j.     
  (    )   :

   //  
   X::X( int val ) : i( val ) { j = i; }

                                 14.12

        ?    
 ?

                                                            ++   698


   (a) Word::Word( char *ps, int count = 1 )
         : _ps( new char[strlen(ps)+1] ),
           _count( count )
      {
         if ( ps )
            strcpy( _ps, ps );
         else {
            _ps = 0;
            _count = 0;
       }
   }

   (b) class CL1 {
        public:
           CL1() { c.real(0.0); c.imag(0.0); s = "not set"; }
           // ...
       private:
          complex<double> c;
          string s;
   };

   (c) class CL2 {
        public:
           CL2( map<string,location> *pmap, string key )
              : _text( key ), _loc( (*pmap)[key] ) {}
            // ...
       private:
          location _loc;
          string _text;
   };

   Account oldAcct( "Anna Livia Plurabelle" );


                         14.6.   A

           , , :

                                                            ++   699

   Account newAcct( oldAcct );

    .    ,  
 ,   ,     .
  ,      
,       .
       ,  ,   
   ,  ,  
,    .   
   Account:

   class Account {
   public:
      // ...
   private:
      char *_name;
      unsigned int _acct_nmbr;
      double _balance;
   };

  ,       :

   inline Account::
   Account( const Account &rhs )
   {
      _name = rhs._name;
      _acct_nmbr = rhs._acct_nmbr;
      _balance = rhs._balance;
   }

           
:

       :

      extern bool cash_on_hand( Account acct );
      if ( cash_on_hand( oldAcct ))
    Account newAcct( oldAcct );

         :

    // ...

          :

                                                            ++   700

    extern Account
    consolidate_accts( const vector< Account >& )
    {
       Account final_acct;
       //   
       return final_acct;
   }

      :

    //      string
    vector< string > svec( 5 );

    (      string    
      ,        
       string.)

       :

    svec.push_back( string( "pooh" ));

          
    .    ,   
  ,      
, , ,   Account.
       newAcct._name  oldAcct._name   
   C-.  oldAcct        
,  newAcct._name     .  
,  newAcct  ,  _name,    
 oldAcct.     .
          ,   
      newAcct._name   .
,       Account 
      ,  
  .
           
 .    ,     Account  
   .    ,  
       Account.  
  ,    :

                                                            ++   701

   inline Account::
   Account( const Account &rhs )
  {
      //    
      _name = new char[ strlen(rhs._name)+1 ];
      strcpy( _name, rhs._name );

      //     
     _acct_nmbr = get_unique_acct_nmbr();

      //      
     _balance = rhs._balance;
   }

         
 .     :

  1.     .   
      ,  -   .
  2.     -   ,
           (
       ,     1,   ).    
              -
       .    ,    
      ,     ,   
        ,      .

      ,  Account   :

   class Account {
   public:
       Account();
       Account( const char*, double=0.0 );
       // ...
   private:
      Account( const Account& );
      // ...
   };

                  14.6.1.  ,   

    ,    _name  C-    string? 
      ?    
  ?        .
          .  
     ,    
. ,     Account  _name
 ,    :

   newAcct._name = oldAcct._name;

                                                            ++   702

   ,   ,  -.  

   Account newAcct( oldAcct );

     Account.      
 ,        , 
      .
    ,   - ,    
 .      ?  ,
      - .   
     .     
     ,   
     .     
  ,      ,  
 ,     .
        string    ,  _name
    .     
 Account    (    ):

   inline Account::
   Account( const Account &rhs )
   {
      _acct_nmbr = rhs._acct_nmbr;
      _balance = rhs._balance;

      //   C++
      //    
      //  ,   
      _name.string::string( rhs._name );
   }

          Account 
      _name,     
 ,      . 
     .    , ?

   //   ...
   inline Account::
   Account( const Account &rhs )
   {
      _name = rhs._name;
      _balance = rhs._balance;
      _acct_nmbr = get_unique_acct_nmbr();
   }

     ,       
.       string 
  string       
   string    .  
:

                                                            ++   703

   inline Account::
   Account( const Account &rhs )
      : _name( rhs._name )
   {
      _balance = rhs._balance;
      _acct_nmbr = get_unique_acct_nmbr();
   }

      ,    . (   
,   _name    rhs._name,        
 .)      ,  
   -    
.

                                   14.13

           ?

  1.  Point3w,      .
  2.  matrix,         
         .
  3.  payroll ( ),    
      .
  4.  word (),    string  ,   
      ( ,   ).

                                   14.14

          ,  
  .

  (a) class BinStrTreeNode {
       public:
          // ...
       private:
          string _value;
          int _count;
          BinStrTreeNode *_leftchild;
          BinStrTreeNode *_rightchild;
   };

   (b) class BinStrTree {
       public:
          // ...
       private:
          BinStrTreeNode *_root;
   };

                                                            ++   704

   (c) class iMatrix {
        public:
           // ...
        private:
           int _rows;
           int _cols;
           int *_matrix;
       };

   (d) class theBigMix {
       public:
          // ...
       private:
         BinStrTree _bst;
         iMatrix _im;
         string _name;
         vectorMfloat> *_pvec;
     };


                                  14.15

         ,     
14.3   14.2?  ,  .  ,  .

                                 14.16

         ,  
 :

   Point global;

   Point foo_bar( Point arg )
   {
      Point local = arg;
      Point *heap = new Point( global );
      *heap = local;
      Point pa[ 4 ] = { local, *heap };
      return *heap;
   }

                        14.7.   A

            
    .    
      
   :

newAcct = oldAcct;

                                                            ++   705

      newAcct 
  oldAcct.    
 :

   inline Account&
   Account::
      operator=( const Account &rhs )
   {
      _name = rhs._name;
      _balance = rhs._balance;
      _acct_nmbr = rhs._acct_nmbr;
   }

    ,         , 
      . ,  
  Account,   _name    char*, 
     _name,   _acct_nmbr.
      ,      ,
      :

   //     
   className&
   className::
   operator=( const className &rhs )
   {
      //     
      if ( this != &rhs )
      {
         //     
      }
      //  ,   
      return *this;
   }

     

   if ( this != &rhs )

     ,    
,       
,      ,     ,
     .   
   Account:

                                                            ++   706

   Account&
   Account::
   operator=( const Account &rhs )
   {
      //     
      if ( this != &rhs )
      {
         delete [] _name;
         _name = new char[strlen(rhs._name)+1];
         strcpy( _name,rhs._name );
         _balance = rhs._balance;
        _acct_nmbr = rhs._acct_nmbr;
      }
      return *this;
   }

        , , ,  :

   newAcct = oldAcct;

  :

  1. ,        .
  2.  ,     ,  ,    
        .
  3.     ;    ,
         .
  4.    ,     .
  5.         
              
      .
  6.   ,   ,    1-
     6,         .

         Account ,  _name  
 string,     

   newAcct = oldAcct;

   ,      
:

   inline Account&
   Account::
   operator=( const Account &rhs )
   {
      _balance = rhs._balance;
      _acct_nmbr = rhs._acct_nmbr;

      //        
      name.string::operator=( rhs._name );
   }

                                                            ++   707

           Account 
 - _acct_nmbr.     
   ,  _name     string:

   Account&
   Account::
   operator=( const Account &rhs )
   {
      //     
      if ( this != &rhs )
      {
         //  string::operator=( const string& )
         _name = rhs._name;
         _balance = rhs._balance;
      }
      return *this;
   }

      ,    ,     
:        .
          
.   , ,  ,   .  
, , ,    .

                                     14.17

          ,  
 14.14   14.6.

                                    14.18

          ,    
 14.3   14.2?  ,  .   
,    .

                        14.8.   A

              
,   . ,     :

   bool sufficient_funds( Account acct, double );

         
 acct   -  Account.  
     :

   bool sufficient_funds( Account *pacct, double );
   bool sufficient_funds( Account &acct, double );


                                                            ++   708

     Account.    
    (.     
    7.3).
             , 
 ,      .  
 :

   //  ,      
   //   
   Matrix
   operator+( const Matrix& m1, const Matrix& m2 )
   {
      Matrix result;
      //    ...
      return result;
   }

        

    Matrix a, b;
    // ...
    //     operator+()
    Matrix c = a + b;
   a = b + c;

            
  ,  Matrix      .  
  ,  , ,   .
        :

   //  ,      
   //      
   Matrix&
   operator+( const Matrix& m1, const Matrix& m2 )
   {
      Matrix result;
      //   ...
      return result;
   }

      .   ,   
result      ,    . (
    ,     .)
           
.       :

                                                            ++   709

   //      
   //     ,    
   Matrix&
   operator+( const Matrix& m1, const Matrix& m2 )
   {
      Matrix *result = new Matrix;
      //   ...
      return *result;
   }

     :    ,      
      delete     
.
         ,   
   ,    :

   //    ,
   //       
   void
   mat_add( Matrix &result,
                  const Matrix& m1, const Matrix& m3 )
   {
         //  
   }

    ,   ,     
  ,     


   //   
   Matrix c = a + b;

    :

   //   
   if ( a + b > c ) ...

          ++.    
   ,     :

                                                            ++   710

   Matrix&
   operator+( const Matrix& m1, const Matrix& m2 )
   name result
   {
      Matrix result;
      // ...
      return result;
   }

         ,    
-:

   //   
   //      
   void
   operator+( Matrix &result, const Matrix& m1, const Matrix& m2 )
   name result
   {
      //  
   }

     ,    
,     . :

   Matrix c = a + b;

   

   Matrix c;
   operator+(c, a, b);

          ,    .
   ,      
       .    
:

   classType
   functionName( paramList )
   {
      classType namedResult;
      //  -  ...
      return namedResult;
   }

      ,     
:

                                                            ++   711

   void
   functionName( classType &namedResult, paramList )
   {
      //        namedResult
   }

         
 .     ,   
           .
            C++. 
  

Matrix c = a + b;

  . ,    
 ,     :

c = a + b;

     .   :

   for ( int ix = 0; ix < size-2; ++ix ) {
      Matrix matSum = mat[ix] + mat[ix+1];
      // ...
   }



   Matrix matSum;
   for ( int ix = 0; ix < size-2; ++ix ) {
      matSum = mat[ix] + mat[ix+1];
      // ...
   }

   ,      ,   , 
         
 .  ,     

   Point3d p3 = operator+( p1, p2 );

  :

                                                            ++   712

   //   C++
   Point3d p3;
   operator+( p3, p1, p2 );



   Point3d p3;
   p3 = operator+( p1, p2 );



   //   C++
   //    
   operator+( p3, p1, p2 );

.
     ,      
  . ?      
,        . 
    ,        
 .
      ,       
 .         
 (      ),  ,  
     ,     
 .
            
  ,   ,    
   ,     
. ,     ,     
. ,  

   Point3d p3;
   p3 = operator+( p1, p2 );

  :

   //   C++
   Point3d temp;
   operator+( temp, p1, p2 );
   p3.Point3d::operator=( temp );
   temp.Point3d::~Point3d();

                                                            ++   713

     (Michael Tiemann),   GNU C++,   
     (return value language
extension).       [LIPPMAN96b].    Inside the
C++ Object Model ([LIPPMAN96a])      
 .
