                                                            C++   379

                          8.     

          ,    ++. 
      ?      
     , ..      ?   
            ,  
          .  
       :   ,    
        ,     .
       ,  ,     
      (,       ),
     (    )  
      (    ). 
        ,     
    .

                               8.1.  

      ++       (,
,   ).   ,        
:        , 
   ,       
 . ,    ,   . 
++    :   ,  
     .
          ,  
  (  ).      , 
   ( )     
  .
           , 
        .
        
   .
   , ,         
.      ,
        .
      . 
,   ,      ,
,   ,      .
(    8.5  8.6.)
           .
(       13.)
            . 
    s1     :

++   380

#include <iostream>
#include <string>

//  s1  s2 
int lexicoCompare( const string &sl, const string &s2 ) { ... }

//   s1  s2
int sizeCompare( const string &sl, const string &s2 ) { ... }

typedef int ( PFI)( const string &, const string & );
//   
void sort( string *s1, string *s2, PFI compare =lexicoCompare )
{ ... }

string sl[10] = { "a", "light", "drizzle", "was", "falling",
"when", "they", "left", "the", "school" };

int main()
{
//  sort()      compare
// s1 -  
sort( s1, s1 + sizeof(s1)/sizeof(s1[0]) - 1 );

//   
for ( int i = 0; i < sizeof(s1) / sizeof(s1[0]); ++i )
cout << s1[ i ].c_str() << "\n\t";
}

      lexicoCompare(), sizeCompare()  sort()
          , 
         s1.
   ,    ,      
   (  ). ,  s1 
 lexicoCompare()       , 
    .
      s1         ,
  , ,    main().
              .
       s1   ,
    :

void s1(); // :   s1

        :   
     ,    
. (     9.)
    ++           .
       .  
,   ,     . 
      .    
       .     
 . (      
  ;   10.9     ; 
  13     ,    16.12    .)

++   381

           .  
    .    
       .

8.1.1.   

           ,  
  (    ).    
  .    ( )  
     .    
. ,       
(        ):

const int notFound = -1; //   
int binSearch( const vector<int> &vec, int val )
{ //   :  #1
int low = 0;
int high = vec.size() - 1;
while ( low <= high )
{ //   :  #2
int mid = ( low + high ) / 2;
if ( val < vec[ mid ] )
high = mid - 1;
else low = mid + 1;
}
return notFound; //   :  #1
}

          binSearch().   
  vec  val,    low  high.  while 
    ,     
mid.  vec  val   low  high    .
       .    
  notFound.
      vec  val     
  ,           .
:

int binSearch( const vector<int> &vec, int val )
{ //   :  #1
int val; // :   val
// ...

       binSearch(),   
    while.  vec  val   
 binSearch().
           :
  ,   .   , 
.  ,   ,  . 

++   382

    ,        
   .     ,   
.
   -          
           .
      low     
    binSearch(),   low 
    while      
,  :

int low;
int binSearch( const vector<int> &vec, int val )
{
//   low
//   
int low = 0;
// ...
// low -  
while ( low <= high )
{//...
}
// ...
}

       C++    
 . ,   for    
 :

for ( int index = 0; index < vecSize; ++index )
{
//  index   
if ( vec[ index ] == someValue )
break;
}
// :  index  
if ( index != vecSize ) //  

            for  
  (    ++,      
).      ,      
 :

//  
{ //  
int index = 0;
for ( ; index < vecSize; ++index )
{
// ...
}
}

++   383

          
   .    index,  ,
   ,       :

int index = 0;
for ( ; index < vecSize; ++index )
{
// ...
}
// :  index 
if ( index != vecSize ) //  

    ,      for, 
  ,          
 ,      :

void fooBar( int *ia, int sz )
{
for (int i=0; i<sz; ++i) ... // 
for (int i=0; i<sz; ++i) ... // ,  i
for (int i=0; i<sz; ++i) ... // ,  i
}

           if  switch,
     while  for. :

if ( int *pi = getValue() )
{
// pi != 0 -- *pi   
int result = calc(*pi);
// ...
}
else
{
//  pi  
// pi == 0
cout << ": getValue()  " << endl;
}

   ,     if,   pi,  
 if    else,     . 
    ,     
.  pi  0 ( ),    
 else.  pi    ,   
  if. ( if, switch, for  while    5.)

 8.1

         .  
  ?

++   384

int ix = 1024;
int ix() ;

void func( int ix, int iy ) {
int ix = 255;
if (int ix=0) {
int ix = 79;
{
int ix = 89;
}
}
else {
int ix = 99;
}
}

 8.2

          ix  iy 
 :

int ix = 1024;
void func( int ix, int iy ) {
ix = 100;
for( int iy = 0; iy < 400; iy += 100 ) {
iy += 100;
ix = 300;
}
iy = 400;
}

8.2.    

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

8.2.1.   

        7,     ,   
    .  ,  

++   385

,       ,    .
        . :

//   calc()
//     
void calc(int);
int main()
{
int loc1 = get(); // : get()  
calc(loc1); // : calc() 
// ...
}

       :

type_specifier object_name;
type_specifier object_name = initializer;

   , ,  obj1.  obj1   97:

int obj1 = 97;

      obj2,     :

int obj2;

   ,        ,
   .  ,    
 var1,  var2   :

int var1 = 0;
int var2;

           .  
       ,   ,
   ,    , 
 .   ?
       extern,   :  ,
              .
:

extern int i;

     ,     , 

int i;

++   386

extern-     .      
           .  
    ,     , 
   :

//  
extern int obj1;
extern int obj2;
//  
int obj1 = 97;
int obj2;

          extern   
  .     ,  
  :

extern const double pi = 3.1416; // 
const double pi; // :   pi

     extern          
   :    . :


extern void putValues( int*, int );

8.2.2.     

     ,         
,          
 .  ++  ,   
.
   ,    token.C  addToken()    
  unsigned char.   lex.C,    ,  
    char.

// ----   token.C ----
int addToken( unsigned char tok ) { /* ... */ }
// ----   lex.C ----
extern int addToken( char );

    addToken()   lex.C      .
     ,    
:        Sun
Sparc,     IBM 390.     :  
   .  ?
      :

++   387

const unsigned char INLINE = 128;
const unsigned char VIRTUAL = 129;

    addToken()  :

curTok = INLINE;
// ...
addToken( curTok );

    char           . 
 addToken()      ,   char 
,  ,       127.  
       ,   
   .
    ++          
      (type-safe linkage).  
       .  
 unsigned char  char ,     
  addToken(),    lex.C,  
.      token.C   .
           
   .      
 . (       9.)
            
       . 
    ,      
 .      ,
,   ,      (
,     -   ).

//  token. C
unsigned char lastTok = 0;
unsigned char peekTok() { /* ... */ }
//  lex.C
extern char lastTok;
extern char peekTok();

          
 .      .

8.2.3.     

         extern- ,
     .   

++   388

.   ,       ,
   .
        . -, ,  
           
. -,        
 ,        -  
.
     addToken()    :

// ----- token.h -----
typedef unsigned char uchar;
const uchar INLINE = 128;
// ...
const uchar IT = ...;
const uchar GT = ...;
extern uchar lastTok;
extern int addToken( uchar );
inline bool is_relational( uchar tok )
{ return (tok >= LT && tok <= GT); }
// ----- lex.C -----
#include "token.h"
// ...
// ----- token.C -----
#include "token.h"
// ...

          . 
      .    
         ,  
  ,    .   
    ++  
  .     , 
    .      
 ,      
.
      ,      
 (inline)   .     
 , ,       :

extern int ival = 10;
double fica_rate;
extern void dummy () {}

     i     extern,  
    .      dummy(),  
   extern,   :    
 .  fica_rate     :  

++   389

    extern.     
          
  .
     token.h,  ,  INLINE   
is_relational()   .    .
          
 :         .
           .
    . ,   128
 INLINE ,       .   
   ,   (,  
)      ,   . 
        ,   
        .
     , ,   .   
     .     
   extern. :

// -----   -----
const int buf_chunk = 1024;
extern char *const bufp;
// -----   -----
char *const bufp = new char[buf_chunk];

    bufp   const,        
 (     new,   
 ).       , 
   ,     .
     ,    const.
   ,   ,    ,
  ,        ?

// :      
const char* msg = "?? oops: error: ";

     ,  msg  .   , 
.     (  
 .   3):

const char *const msg = "?? oops: error: ";

         .
         .   
      ,     .
(      7.6.)

++   390

   ,  ,     ,
     .   inline  
 .         
 ,    .   
 inline,       . 
        ,   
  .
           
(      ):

         . ,  
    .        
     inline       ;
           . , 
      ++  AT&T (cfront)  
              .  
       ,    .

        inline,     
. ,     .   
         , 
       .

 8.3

   ,       ,   
,  :

(a) extern int ix = 1024;
(b) int iy;
(c) extern void reset( void *p ) { /* ... */ }
(d) extern const int *pi;
(e) void print( const matrix & );

 8.4

              
?   ? ?

(a) int var;
(b) inline bool is_equal( const SmallInt &, const SmallInt & ){ }
(c) void putValues( int *arr, int size );
(d) const double pi = 3.1416;
(e) extern int total = 255;

8.3.  

           .
    : ,   ,
      .

++   391

         ,   
,    .      , 
       . 
     ,    
  .        
.

8.3.1.  

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

#include "Matrix.h"
Matrix* trouble( Matrix *pm )
{
Matrix res;
// - 
//   res
return &res; // !
}

int main()
{
Matrix m1;
// ...
Matrix *mainResult = trouble( &m1 );
// ...
}

   mainResult      res.  ,
,   res,     trouble(). 
  main() mainResult    ,   
. (          ,
       trouble()    ,
,   .)     . 
 mainResult       .
      trouble()  m1    main()
. ,  main(),    trouble()  , 
 m1    trouble().

++   392

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

8.3.2.   

    ,    ,   
  register,       
.    ,     . 
  ,   ,     
.

for ( register int ix =0; ix < sz; ++-ix ) // ...
for ( register int *p = array ; p < arraySize; ++p ) // ...

      :

bool find( register int *pm, int Val ) {
while ( *pm )
if ( *pm++ == Val ) return true;
return false;
}

           .
      register    . 
   ,    
        .
       ,    
,         
 .

8.3.3.   

            
 , , ,      
 .       
 ,      :   
    .
           static ( 
 ).      
 ,    ,     
.       

++   393

 ,   . , ,  
gcd(),     :

#include <iostream>

int traceGcd( int vl, int v2 )
{
static int depth = 1;
cout << " #" << depth++ << endl;
if ( v2 == 0 ) {
depth = 1;
return vl;
}
return traceGcd( v2, vl%v2 );
}

   ,      depth, 
  traceGcd().         
   .     traceGcd():

#include <iostream>
extern int traceGcd(int, int);

int main() {
int rslt = traceCcd( 15, 123 );
cout << " (15,123): " << rslt << endl;
return 0;
}

  :

 #1
 #2
 #3
 #4
 (15,123): 3

         0. 
       .
       
     ,   
     .

++   394

#include <iostream>

const int iterations = 2;
void func() {
int value1, value2; //  
static int depth; //   
if ( depth < iterations )
{ ++depth; func(); }
else depth = 0;
cout << "\nvaluel:\t" << value1;
cout << "\tvalue2:\t" << value2;
cout << "\tsum:\t" << value1 + value2;
}

int main() {
for ( int ix = 0; ix < iterations; ++ix ) func();
return 0;
}

      :

valuel: 0 value2: 74924 sum: 74924
valuel: 0 value2: 68748 sum: 68748
valuel: 0 value2: 68756 sum: 68756
valuel: 148620 value2: 2350 sum: 150970
valuel: 2147479844 value2: 671088640 sum: -1476398812
valuel: 0 value2: 68756 sum: 68756

   value1  value2    .  
,      ,  , 
   .  depth,   
 ,    0,   func()
     .

8.4.   

          . 
  -  .     ,
    .      
     . , 
          ,  
  .      , 
        , ..  
,     .
           
 .      , 
,  .      
,  .       new, 
    delete.    
 ,    .  ,   ,
    .

++   395

     ,   ,    
 new:    ,     
,    new (placement new expression).  
,    . (   
   11.   15     new  delete
  .)

8.4.1.     


    new     new,     .
          .
:

new int;

      int.    


new iStack;

     iStack.
       new   .    
 ?          , 
      .  new    , 
  .        
:

int *pi = new int;

     new     int,     pi.
         
.  ,  ,   pi,  .
    ,    ,   ,    
.   ,    .
 :

   if ( *pi == 0 )

,  false,  ,    pi,  
 . , ,    
new,  .    
 int     :

int *pi = new int( 0 );

           ;  pi
    int,   0.    
.      (  ),
 ,    int.

++   396

    new    :   
  ,    ,   . 
     new().  
    :

int ival = 0; //    int    0
int *pi = &ival; //     

 , , ,  ,  pi,   
new()    . 

iStack *ps = new iStack( 512 );

   iStack  512 .      
     ,   
   . (    
    15.8.     
   .)
     new    : ,  , 
 ,         
.   new()      , 
  bad_alloc. (     11.)
     ,    pi,   
,    .  ,  pi  

delete. ,
delete pi;

 ,    pi,      int.
    ,   delete 
  .      delete(),
     .   ,  
  .
      ,   :   ,   pi 
-   ?        :

//   ?
if ( pi != 0 )
delete pi;

   .  ++ ,   delete     delete()
   . ,   0 . (  
  ,       
.)
          pi  ,  
.   pi       

++   397

. ,        
     .      
  pi ,      new 
 .  ,    pi,  ,
, pi        int.
     delete,    . 
,    pi,  ,    
.   delete  pi   ,  
   ,   .   
   ,     
   0,   ,       
.
    delete       , 
   ,      new.
  delete  ,     ,  
  . ,    ,  
    .
          delete:

void f() {
int i;
string str = "dwarves";
int *pi = &i;
short *ps = 0;
double *pd = new doub1e(33);
delete str; // : str    
delete pi; // : pi    
delete ps; // 
delete pd; // 
}

      ,     :

      .       
    .      ;
      delete       . 
    ,          
     .        
     . , ,     
      ,        
    .   delete    , 
     ,     ;
       .   , 
    ,     delete,  .

             ,
   .     , 
 ++   auto_ptr.     
.    ,     
,     new  delete.

++   398

8.4.2.  auto_ptr 

      ++ auto_ptr   , 
    ,   
 new. ( ,     
 .  auto_ptr    ,   
 .)
    auto_ptr    ,  
  new.    ,  
  auto_ptr.     ,   auto_ptr 
  .
       auto_ptr    :

#include <memory>

     auto_ptr   :

auto_ptr< type_pointed_to > identifier( ptr_allocated_by_new );
auto_ptr< type_pointed_to > identifier( auto_ptr_of_same_type );
auto_ptr< type_pointed_to > identifier;

 type_pointed_to     . 
    .  ,   
  auto_ptr  ,    
new.     :

   auto_ptr< int > pi ( new int( 1024 ) );

      pi    , 
 1024.  ,    auto_ptr,   
:

if ( *pi != 1024 )
// , -  
else *pi *= 2;

   ,    pi,      
 pi.   pi  ,  ,   , 
    ,   .   pi ,  , 
  ,     .
    ,    auto_ptr   , ,
  string? :

++   399

   pstr_auto( new string( "Brontosaurus" ) );

   ,     -   .  
     :

   string *pstr_type = new string( "Brontosaurus" );
   if ( pstr_type->empty() )
     // , -  

        empty(),   auto_ptr?   :

   auto_ptr< string > pstr_auto( new string( "Brontosaurus" ) );
   if ( pstr_type->empty() )
      // , -  

      auto_ptr      
 ,    ,   
    ,   
auto_ptr.      .  
 (    )   
 auto_ptr   ,   
.
    ,    pstr_auto2  pstr_auto,
   auto_ptr,   ?

   //      ?
  auto_ptr< string > pstr_auto2( pstr_auto );

   ,        
:

   string *pstr_type2( pstr_type );

            ,    
,     .
        auto_ptr   .
   pstr_auto,    ,   
,        .
     ,    ,    pstr_auto2
,     ,   pstr_auto? ,  
      :      
,          auto_ptr.
      auto_ptr       
 ,       
.  auto_ptr,     ,  

++   400

   auto_ptr,  .   
     pstr_auto2,   pstr_auto. pstr_auto
        .
        .       auto_ptr:

auto_ptr< int > p1( new int( 1024 ) );
auto_ptr< int > p2( new int( 2048 ) );

        auto_ptr      :

p1 = p2;

     ,    p1, .
     p1    int   2048. p2  
      .
       auto_ptr  ,   
      . :

//       
auto_ptr< int > p_auto_int;

    p_auto_int    - , 
     0.     
  :

// :   
if ( *p_auto_int != 1024 )
*p_auto_int = 1024;

         0:

if ( pi ! = 0 ) ...;

  ,  auto_ptr -   ?  get()
  ,    auto_ptr. , 
   :

   // ,   p_auto_int  
   if ( p_auto_int.get() != 0 &&
        *p_auto_int != 1024 )

++   401

      *p_auto_int = 1024;

    auto_ptr     ,      -?
 ,        
auto_ptr?      reset(). :

   else
      // ,   
      p_auto_int.reset( new int( 1024 ) );

    auto_ptr    ,    
new:

   void example() {
      //    
      auto_ptr< int > pi;
      {
           //  
           pi = new int( 5 ) ;
      }
   }

         reset(),    
 0,      auto_ptr.  auto_ptr    
  ,       
   auto_ptr. :

auto_ptr< string >
pstr_auto( new string( "Brontosaurus" ) );
// "Brontosaurus"   
pstr_auto.reset( new string( "Long-neck" ) );

      ,   assign(),   
 ,       :

   //      
   //   assign()
   pstr_auto->assign( "Long-neck" );

         ,   
   .     . 
,       ,  
 assign()      
 .        , 
       .

++   402

     auto_ptr     
   .      
,      :

      auto_ptr ,   
      new,     .   
          delete  
    ;
   auto_ptr          .

            
.       get().  :

auto_ptr< string >
pstr_auto( new string( "Brontosaurus" ) );
// :      
//     
auto_ptr< string > pstr_auto2( pstr_auto.get() );

    release() ,     
     . release()    
,    auto_ptr,     .
        :

// :     ,
//  pstr_auto     
auto_ptr< string >
pstr_auto2( pstr_auto.release() );

8.4.3.     

    new        .   
        .  
     . new    
 . :

//     int
//    1024
int *pi = new int( 1024 );
//    1024 
//   
int *pia = new int[ 1024 ];
//     4x1024 
int (*pia2)[ 1024 ] = new int[ 4 ][ 1024 ];

   pi      int,  
1024; pia       1024 ; pia2   

++   403

,     1024 , .. pia2  4096
.
      ,   ,    . (
 15.8  ,       
      .) 
    new     .
   ,   ,  
    for:

   for (int index = 0; index < 1024; ++index )
      pia[ index ] = 0;

         ,   
       , ..     
 .  ,      
,   :    .
   ,          C-,
           
     .  ,   , 
   ,     :
       .  ,  
 ,          .
    new        
 ,    . ,   
 C-:

   const char *noerr = "success";
   // ...
   const char *err189 = "Error: a function declaration must "
      "specify a function return type!";

        new     ,
   :

   #include <cstring>

   const char *errorTxt;

   if (errorFound)
      errorTxt = errl89;
   else
      errorTxt = noerr;

   int dimension = strlen( errorTxt ) + 1;
   char *strl = new char[ dimension ];

   //     strl
   strcpy( strl, errorTxt );

++   404

dimension   :

//   ++ ,
//    
char *strl = new char[ str1en( errorTxt ) + 1 ];

   ,   ,   strlen(),   
    C-.     
 ,    ,  
  :   -   .
?  ,   ,  
- ,   ,     .
        ,        
.      string   
++.
   ,     ,     new,
   ,    .  
  ,    . :

int getDim();
//   
int (*pia3)[ 1024 ] = new int[ getDim() ][ 1024 ]; // 
// :     
int **pia4 = new int[ 4 ][ getDim() ];

    delete      :

   delete[] str1;

      .   ,  
 ,    .   str1    char, 
    ,     .
        ,   
   (    ,  
 ,  ,       14.4).
     ,      
 ,      
, ,  vector, list  string.   
. ( string     3.4,  vector    3.10.
    .   6.)

++   405

8.4.4.     


              
.   ,   .  
    new:

const int *pci = new const int(1024);

        . -,  
 ,      ( , 
    ,    ;  
   ).
   -, ,   new,   . 
  pci    const int.
       ,  ,
  ,      .  
 ,      delete. :

delete pci;

      delete     const int,  
     ,    pci.
          
, ,    ,    
   new.     
:

   const int *pci = new const int[100]; // 

8.4.5.   new 

       new,       
,    ,    .    
 new.     ,   
:

   new (place_address) type-specifier

   place_address   .   (  
 <new>)      
,      . :

++   406

#include <iostream>
#include <new>
const int chunk = 16;
class Foo {
public:
int val() { return _val; }
FooQ(){ _val = 0; }
private:
int _val;
};
//  ,     Foo
char *buf = new char[ sizeof(Foo) * chunk ];

int main() {
//   Foo  buf
Foo *pb = new (buf) Foo;
// ,     buf
if ( pb.val() == 0 )
cout << " new !" << endl;
//    pb
delete[] buf;

return 0;
}

     :

    new !

      new    delete:   , 
    .      ,
  buf,   pb.     ,  
  .  buf    ,  delete
 

delete[] buf;

     buf    ,   . 
  pb        Foo.

 8.5

   ,    new :

(a) const float *pf = new const float[100];
(b) double *pd = new doub1e[10] [getDim()];
(c) int (*pia2)[ 1024 ] = new int[ ][ 1024 ];
(d) const int *pci = new const int;

 8.6

       pa?

++   407

   int *pa = new arr;

 8.7

       delete    
  :

int globalObj;
char buf[1000];

void f() {
int *pi = &global0bj;
double *pd = 0;
float *pf = new float(O);
int *pa = new(buf)int[20];

delete pi; // (a)
delete pd; // (b)
delete pf; // (c)
de1ete[] pa; // (d)
}

 8.8

       auto_ptr     
?   .

int ix = 1024;
int *pi = & ix;
int *pi2 = new int ( 2048 );

(a) auto_ptr<int> p0(ix);
(b) auto_ptr<int> pl(pi);
(c) auto_ptr<int> p2(pi2);
(d) auto_ptr<int> p3(&ix);
(e) auto_ptr<int> p4(new int(2048));
(f) auto_ptr<int> p5(p2.get());
(9) auto_ptr<int> p6(p2.release());
(h) auto_ptr<int> p7(p2);

 8.9

       :

   int *pi0 = p2.get();
   int *pi1 = p2.release() ;

           ?

 8.10

++   408

     :

auto_ptr< string > ps( new string( "Daniel" ) );

          assign()?   
 ?

ps.get()->assign( "Danny" );
ps->assign( "Danny" );

8.5.    

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

class cplusplus_primer_matrix { ... };
void inverse( cplusplus_primer_matrix & );

        . ,   ++, 
   ,   ,    
.       
.
           
.         
        :

namespace cplusplus_primer {
class matrix { /*...*/ };
void inverse ( matrix & );
}

++   409

   cplusplus_primer     (  
 ,       
).
          . 
     ,    
 , ,   .  ,  
  ,   .    
,    ,      
.
           
 .
        ,  , 
 . ,   matrix,   
cplusplus_primer,  cplusplus_primer::matrix,    inverse()
  cplusplus_primer::inverse().
    cplusplus_primer       
:

void func( cplusplus_primer::matrix &m )
{
// ...
cplusplus_primer::inverse(m);
return m;
}

         (, DisneyFeatureAnimation)
   matrix   inverse()     
     cplusplus_primer,   func()
   :

void func( DisneyFeatureAnimation::matrix &m )
{
// ...
DisneyFeatureAnimation::inverse(m);
return m;
}

   ,      

   namespace_name::member_name

.   ,   
   .    , using- 
using-. (     8.6.)

++   410

8.5.1.   

          
namespace,    .      
 ,     ;  
      . ,   ,  
    ,     
.
           , 
 .  ,     
,     : ,  ( 
),  (   ), .
       ,     .
    ,  ,   ,
    ,    . :

namespace cplusplus_primer {
class matrix { /* ... */ };
void inverse ( matrix & );
matrix operator+ ( const matrix &ml, const matrix &m2 )
{/* ... */ }
const double pi = 3.1416;
}

    ,    cplusplus_primer, 

   cplusplus_primer::matrix

    

   cplusplus_primer::inverse()

    

   cplusplus_primer::pi

    ,      ,   
.    .
          . ,
      :

++   411

namespace cplusplus_primer {
class matrix { /* ... */ };
const double pi = 3.1416;
}
namespace cplusplus_primer {
void inverse ( matrix & );
matrix operator+ ( const matrix &ml, const matrix &m2 )
{/* ... */ }
}

      :    
cplusplus_primer,   matrix,  inverse(),  pi 
operator+().       
 .
   

   namespace namespace_name {

  ,   namespace_name      
.         .
            
.          .
:

//    
//   
namespace cplusplus_primer {
class matrix { /* ... */ };
const double pi = 3.1416;
matrix operator+ ( const matrix &ml, const matrix &m2 );
void inverse ( matrix & );
}
//    
//   
namespace cplusplus_primer {
void inverse ( matrix &m )
{ /* ... */ }
matrix operator+ ( const matrix &ml, const matrix &m2 )
{ /* ... */ }
}

          , 
 :  , ,  .  
   ,    .
            
       :  
 .       :

++   412

// ---- primer.h ----
namespace cplusplus_primer {
class matrix { /*... */ };
const double pi = 3.1416;
matrix operator+ ( const matrix &m1, const matrix &m2 );
void inverse( matrix & );
}
// ---- primer.C ----
#include "primer.h"
namespace cplusplus_primer {
void inverse( matrix &m )
{ /* ... */ }
matrix operator+ ( const matrix &m1, const matrix &m2 )
{ /* ... */ }
}

   ,   ,  :

// ---- user.C ----
//   
#include "primer.h"
void func( cplusplus_primer::matrix &m )
{
//...
cplusplus_primer: :inverse( m );
return m;
}

        , 
    ,       
    primer.C  user.C   .

8.5.2.    

          
       (::). 
 ,  matrix,  .  
,      :

//   
#include "primer.h"
// :    matrix
void func( matrix &m );

          .    
,    ,      
    ,  . ,  
  :

++   413

//   
#include "primer.h"
class matrix { /*   */ };
// :   matrix 
void func( matrix &m );

   matrix       
   .   matrix  
  cplusplus_primer    ,    
,     .
      ,      
 :    ,     
,      .   
,         .
 using-  using-.      .
   ,          , 
     .    
 , 

   ::member_name

   .       
,      ,  
   .
          
      .  
  .      max.
      ,
    ,     
    . (,   
     .)     
 .    max   
  .    , 
     ::max.   :

++   414

#include <iostream>
const int max = 65000;
const int lineLength = 12;

void fibonacci( int max )
{
if ( max < 2 ) return;
cout << "0 1 ";

int v1 = 0, v2 = 1, cur;
for ( int ix = 3; ix <= max; ++ix ) {
cur = v1 + v2;
if ( cur > ::max ) break;
cout << cur << " ";
vl = v2;
v2 = cur;
if (ix % "lineLength == 0) cout << end"!;
}
}

   main(),  fibonacci():

#include <iostream>
void fibonacci( int );
int main() {
cout << " : 16\n";
fibonacci( 16 );
return 0;
}

     :

    : 16
   0 1 1 2 3 5 8 13 21 34 55 89
   144 233 377 610

8.5.3.   

     ,       .
       
. :

++   415

// ---- primer.h ----
namespace cplusplus_primer {
//    :
//   
namespace MatrixLib {
class matrix { /* ... */ };
const double pi = 3.1416;
matrix operators+ ( const matrix &ml, const matrix &m2 );
void inverse( matrix & );
// ...
}
//    :
//   
namespace AnimalLib {
class ZooAnimal { /* ... */ };
class Bear : public ZooAnimal { /* ... */ };
class Raccoon : public Bear { /* ... */ };
// ...
}
}

     cplusplus_primer   : MatrixLib 
AnimalLib.
   cplusplus_primer        
     .  
   ,       
  . MatrixLib  ,    
matrix,  AnimalLib    ZooAnimal.
          .  
        
 .
   , ,     MatrixLib,  

   cplusplus_primer::MatrixLib::matrix

 

   cplusplus_primer::MatrixLib::inverse

   ,    
cplusplus_primer::MatrixLib,  :

#include "primer.h"

// ,  ...
//    , 
//    !
void func( cplusplus_primer::MatrixLib::matrix &m )
{
// ...
cplusplus_primer::MatrixLib::inverse( m );
return m;

++   416

   }

          
,  .      
   ,   .     
 ,       
.      Type   
:       MatrixLib,   cplusplus_primer 
    :

typedef double Type;
namespace cplusplus_primer {
typedef int Type; //  ::Type
namespace MatrixLib {
int val;
// Type:    cplusplus_primer
int func(Type t) {
double val; //  MatrixLib::val
val = ...;
}
// ...
}
}

          ,  
     .
       Type      
Type   cplusplus_primer.    Type,  
MatrixLib,     cplusplus_primer,    func()
   int.
    ,    ,  
     .     val
 MatrixLib    val.    val 
func()        ,  
  func()     .

8.5.4.    

    ,        
  . ,  matrix   pi 
    MatrixLib,    operator+()
 inverse()  -     :

++   417

// ---- primer.h ----
namespace cplusplus_primer {
//    :
//   
namespace MatrixLib {
class matrix { /* ... */ };
const double pi = 3.1416;
matrix operators+ ( const matrix &ml, const matrix &m2 );
void inverse( matrix & );
// ...
}
}

           . 
        ,  
 . ,    operator+()  
  ,      :

// ---- primer.C ----
#include "primer.h"

//     
cplusplus_primer::MatrixLib::matrix
cplusplus_primer::MatrixLib::operator+
( const matrix& ml, const matrix &m2 )
{ /* ... */ }

    operator+()      
cplusplus_primer  MatrixLib.      matrix  
 operator+():   .   
?
      operator+()    
   ,      
.      operator+() 
MatrixLib. , ,         
 ,      , 
 :

cplusplus_primer::MatrixLib::operator+

     operator+()      
        . ,
   operator+()     matrix:

++   418

// ---- primer.C ----
#include "primer.h"

cplusplus_primer::MatrixLib::matrix
cplusplus_primer::MatrixLib::operator+
( const matrix &ml, const matrix &m2 )
{
//    
// cplusplus_primer::MatrixLib::matrix
matrix res;

//     matrix
return res;
}

           ,  
    .      ,
 . ,  operator+()   
  ,    cplusplus_primer  
 MatrixLib.      :

// ---- primer.C --
#include "primer.h"
namespace cplusplus_primer {
MatrixLib::matrix MatrixLib::operator+
( const matrix &ml, const matrix &m2 ) { /* ... */ }
}

        ,    
 .    operator+()  
,         primer.h:

namespace cplusplus_primer {
namespace MatrixLib {
class matrix { /*...*/ };
//      
matrix operator+ ( const matrix &ml, const matrix &m2 );
// ...
}
}

8.5.5.     

      ,       
     . ,   
   . :

++   419

// primer.h
namespace cplusplus_primer {
// ...
void inverse( matrix & );
}

// usel.C
#include "primer.h"
//  cplusplus_primer::inverse()  use1.C

// use2.C
#include "primer.h"
//  cplusplus_primer::inverse()  use2.C

    cplusplus::inverse()  primer.h        
   use1.C  use2.C.
        ,   
.   (  , .  8.2)
   .    , , 
   ,    :

  1.    ,    ,
        ,      , 
      .

     // ---- primer.h ----
     namespace cplusplus_primer {
         class matrix { /* ... */ };
         //  
        extern matrix operator+ ( const matrix &m1, const matrix &m2 );
        extern void inverse( matrix & );

        //  
        extern bool error_state;
}

  2.       ,  :

++   420

// ---- primer.C ----
#include "primer.h"

namespace cplusplus_primer {
//  
void inverse( matrix & )
{ /* ... */ }
matrix operator+ ( const matrix &ml, const matrix &m2 )
{ /" ... */ }

//  
bool error_state = false;
}

            extern,  
       .

8.5.6.   

       , ,   
  ,         .
         .
  ,     ,   
    .      
 ,       .     
  ,    ?
   ,         
double:

// ----- SortLib.h -----
void quickSort( double *, double * );
void bubbleSort( double *, double * );
void mergeSort( double *, double * );
void heapSort( double *, double * );

           swap()  ,   
 .         , 
    .     SortLib.C.
     .   , ?

// ----- SortLib.C -----
void swap( double *dl, double *d2 ) { /* ... */ }
//     swap()
void quickSort( double *d1, double *d2 ) { /* ... */ }
void bubbleSort( double *d1, double *d2 ) { /* ... */ }
void mergeSort( double *d1, double *d2 ) { /* ... */ }
void heapSort( double *d1, double *d2 ) { /* ... */ }

++   421

     swap()    SortLib.C     
 SortLib.h,      , 
    . ,   
,        .
    ++      
  ,     .  
    namespace. ,    
  ,        ,  
. :

// ----- SortLib.C -----
namespace {
void swap( double *dl, double *d2 ) { /* ... */ }
}
//     

    swap()     SortLib.C.      
    swap(),    . 
  swap()   ,   . 
    :     
       .
    swap()        SortLib.C
   .    
      .

void quickSort( double *d1, double *d2 ) {
// ...
double* elem = d1;
// ...
//       swap()
swap( d1, elem );
// ...
}

          . 
 swap()      .    
    .
        ++    ,  
       static,
  .     , 
 ,   static.       
,   . ,   SortLib.C    ,
  swap():

// SortLib.C
// swap()     
static void swap( double *d1, double *d2 ) { /* ... */ }

++   422

//     ,   

       ++      static.
,         
 ,         .

 8.11

          ?

 8.12

      operator*(),    
cplusplus_primer::MatrixLib:

namespace cplusplus_primer {
namespace MatrixLib {
class matrix { /*...*/ };
matrix operator* ( const matrix &, const matrix & );
// ...
}
}

          ?  
.

 8.13

   ,     .

8.6.     

           
  ,      .
     ,         .
         , 
,        .
    ,  ,    
  .   , using-  using-
        .

8.6.1.   

           
. ,  

   namespace International_Business_Machines
   { /* ... */ }

      :

++   423

   namespace IBM = International_Business_Machines;

        namespace,   
 ,          .
       ,  .
          .  
   func() :

#include "primer.h"

//  !
void func( cplusplus_primer::MatrixLib::matrix &m )
{
// ...
cplusplLis_primer::MatrixLib::inverse( m );
return m;
}

        
cplusplLis_primer::MatrixLib,      
:

#include "primer.h"
//   
namespace mlib = cplusplus_primer::MatrixLib;

//  !
void func( mlib::matrix &m )
{
// ...
mlib::inverse( m );
return m;

}

          .
,   Lib   cplusplus_primer,  
 func()    :

//  alias     cplusplus_primer
namespace alias = Lib;
void func( cplusplus_primer::matrix &m ) {
// ...
alias::inverse( m );
return m;
}

++   424

8.6.2. Using-

    ,      ,  
  , ..   namespace_name::.   

   using-.

   Using-    using,   
   . :

namespace cplusplus_primer {
namespace MatrixLib {
class matrix { /* ... */ };
// ...
}
}
// using-   matrix

using cplusplus_primer::MatrixLib::matrix;

   Using-      ,    . ,
 using-   matrix  .
          ,   matrix 
           
  .     :

void func( matrix &m );

      func()    cplusplus_primer::
MatrixLib::matrix.
   Using-      :   
,  ,  ,        
  . Using-     
,         . 
    . ,  using-,   
,   :

           ;
          ;
           .

   :

++   425

namespace blip {
int bi = 16, bj = 15, bk = 23;
//  
}
int bj = 0;

void manip() {
using blip::bi; // bi   manip()   blip::bi
++bi; // blip::bi == 17
using blip::bj; //   bj
// bj   manip()  blip::bj
++bj; // blip::bj == 16
int bk; //   bk
using blip::bk; // :   bk  manip()
}

int wrongInit = bk; // : bk 
//   blip::bk

   Using-   manip()      blib
   .      manip(), 
       .  
   .
   Using-     .   
 . Using-      , ,
,    ,        
    .
       ,      
    .

8.6.3. Using-

        ++.   ++  
, , ,     
   .    ++    
,      .  
   ,     ,
   :      
, ..       
  .  ,      
 ,  .
       ,    ,  
 using-. ,   primer.h  
  ,       
 cplusplus_primer.       
.  using-     matrix  
inverse()   cplusplus_primer:

++   426

#include "primer.h"
using cplusplus_primer::matrix;
using cplusplus_primer::inverse;

// using-  
//  matrix  inverse  
void func( matrix &m ) {
// ...
inverse( m );
return m;

}

              , 
          using-
.      ,     
,    .     using-
,      ,   
  .
   Using-    using,     
namespace,      .     
  ,    . Using-
         
.
   ,       :

#include "pnmer.h"
// using-:   cplusplus_primer
//  
using namespace cplusplus_primer;
//  matrix  inverse    
void func( matrix &m ) {
// ...
inverse( m );
return m;
}

   Using-         , 
 ,   . ,  using- 
 ,    cplusplus_primer    
   func().       
 ,        . 

namespace A {
int i, j;
}

 

++   427

   int i, J;

  ,      using-
:

   using namespace A;

    ,     using-
(   ,      
 )  using- (   
 ).

   namespace blip {
      int bi = 16, bj = 15, bk = 23;
      //  
   }

   int bj = 0;

   void manip() {
      using namespace blip; // using- -
                                        //   ::bj and blip::bj
                                        //   
                                       //  bj
      ++bi; // blip::bi == 17
      ++bj; // : 
              //  bj  blip::bj?
      ++::bj; // :  bj == 1
      ++blip::bj; // : blip::bj == 16
      int bk = 97; //  bk  blip::bk
      ++bk; //  bk == 98
}

   -, using-   .    
manip()      .  manip()   
blip  ,        , 
,     .   
  .
   -,  ,   using-,
       ,      
  . ,  bj,   blib,  
manip()      ,  blip.  
     .    bj 
 manip():      ,    
blip.      bj   manip().   
     manip(),    .
   -, using-     .
  manip()  ::bj,       
,  blip::bj      blip.
       blip    manip() ,   
    .  ,   
 manip()      blip. 

++   428

 bk  blip::bk.   bk  manip()  
      .
   Using-   :     ,  
     .    
        :

namespace cplusplus_primer {
class matrix { };
//   ...
}
namespace DisneyFeatureAnimation {
class matrix { };
//   ...
using namespace cplusplus_primer;
using namespace DisneyFeatureAnimation;
matrix m; //, :
// cplusplus_primer::matrix  DisneyFeatureAnimation::matrix?

    ,  using-,   
 .        matrix. 
,   ,   :     
       .   
,        .
   Using-         ,
  .     using-
       .  
   ,   using-   using-
.  ,  ,   
.    using-,   using-,
        .

8.6.4.    std

       ++     std.
 ,    ,    
, ,  <vector>  <iostream>,    .
         std,      
:

++   429

#include <vector>
#include <string>
#include <iterator>

int main()
{
//  istream_iterator   
istream_iterator<string> infile( cin );
// istream_iterator,  end-of-stream
istream_iterator<string> eos;
//  svec ,   cin
vector<string> svec( infile, eos );
// ...
}

   ,     ,     
std       .   
 ,       :

       std    
     ;
    using-,     
     std;
    using-,      std.

      std    :  
istream_iterator,    cin,  string   
vector.
       using-  
 #include:

   using namespace std;

      using-     std .
     .   using-,
         
 .
   Using-,     , :

using std::istream_iterator;
using std::string;
using std::cin;
using std::vector;

      ?       , 
    ,    using-, 
         
.

++   430

         using-.  , -,  ,
   ,  -, ,   
   ++,    .
,  using-       std,
  .

 8.14

      using-  using-.

 8.15

      using-     6.14.

                                               8.16

      :

   namespace Exercise {
      int ivar = 0;
      double dvar = 0;
      const int limit = 1000;
   }
   int ivar = 0;

   //1
   void manip() {
      //2
      double dvar = 3.1416;
      int iobj = limit + 1;
      ++ivar;
      ++::ivar;
   }

        ,   using- 
    Exercise   //1?   //2?    using-
  using-?
