Академический Документы
Профессиональный Документы
Культура Документы
Голуб Правила программирования на C CPP PDF
Голуб Правила программирования на C CPP PDF
++
2001
, , , ,
++!
, ,
, .
" , "
100 ,
, .
,
.
,
.
.
.
,
,
, :
.
-
.
, ++.
, ,
.
,
.
27.
............................................................................................................40
29. ..........................................42
30. , ............................................44
31.
....................................................................................................................45
32. -,
.......................................................................................................................45
33. .......................................................46
34. ........................................47
35. "" .........47
36. .....................................................48
37. ...........48
38. ....49
39. ..................................50
40. ................................................................51
41. ...................................52
41.1. ,
...............................................................................................................53
42. .........................................54
43. , ,
.......................................................................................................................54
4. ................................................................56
44. ,
, , ..............................................57
44.1. ...................................................58
45. _ ..............59
45.1. ............................59
45.2. ,
typedef .................................................................................................................59
46. ANSI C ..................................................60
47. Microsoft........................................................................60
48. ................................................................62
49. ...................62
5. ................................66
50. ...............................................................67
51. ............................................................69
52. ,
................................................................................................69
53. , ........................................70
53.1. , , .....71
54. ................................................71
54.1. ..........72
55. ............................................................................73
56. ............................................................74
56.1. ............................................................74
6 ++
56.2.
................................................................................................................... 75
56.2.1. ,
....................................... 76
56.3.
................................................................................................................... 77
56.4. if return,
else ........................................................................................................................ 78
57. if/else ....... 80
58. ............ 80
59. ........................... 81
60. do/while ................................................................................ 83
60.1. do/while .......................... 84
61. ..... 84
62. .................................. 84
63. for, ,
................................................. 85
64. , ,
for......................................................................................................... 87
65. , ...................... 87
66. ....................................................................... 88
66.1. ........................................................................... 89
66.2. ......................................................... 90
67. ............................................................... 90
68. ................................................................................... 91
69. ................................................................ 91
70. ( ) ........................................ 93
71. ......................................................... 95
72. lint ................................................................................ 97
73. , ,
.......................................................................................... 98
74. .................................................. 99
75. ............................ 100
76. ,
............................................................................................................... 101
77. , ................................. 102
78. - .. 102
6. ......................................................................................... 104
79. .h , ,
.c ..................................................................................................... 105
80. #include .............................................. 105
81. ....................... 106
81.1. ?: , if/else ................................. 111
81.2. ......................... 112
82. enum const , ...................................................................... 113
7
83.
........................................................................................114
83.1. .........................115
84. , .........................116
7 . , .........................................119
85. ( 2)...........................................................120
85.1. .......................................................................................120
85.2. ; ....................................121
85.3. ...................................................................123
85.4. , ......................................................124
85.5. , (#define TRUE) ..124
86. 1 unsigned..126
87. , , ..126
88. ..............................................127
89. goto, ....................................................................128
8. ++ .....................................132
8. ..........................133
90. - ""
.....................................................................................................133
90.1. , .........................133
91.
..............................................................................................................135
92. ++
........................................................................136
93. ..............................................................137
94. ,
..........................................................................................................139
95.
-.................................................................................139
96. ....................141
97. - ...141
98. ...........................................................................144
99. ........................................................144
99.1. .............145
100. , ,
............................................................................145
101. ++ Smalltalk: object...........................145
102. .......................................151
103. ...........................151
104. ,
..............................................................................151
105. ,
...................................................152
106. , , ..........................152
107.
.......................................................................................152
8 ++
137. ,
.............................................................................199
138. ........................203
139. ............................................203
140. , ,
, .............................204
141. , ,
..................................................205
142. ..........................205
143. ( ++) .................................206
144. operator=( )...........................208
8. .......................................................................211
145. ( ) ..................................................211
146. ,
( )...............................................................212
147. ,
................................................................................................................213
148. ,
........................................................................................................214
149. (inline)
...........................................................215
150. ..................................217
151. ,
.......................................................................................................217
8. .....................................................................219
152. new/delete malloc()/free()................................219
153. , ,
............................................................................................................219
154. new delete .............................219
8. ...................................................................................................220
155.
................................................................................................................220
156. .....................................221
157. .............224
158. ; ......................224
8. ...........................................................................................228
159. ............................................228
160. error ........................................231
161. ...................................233
..............................................................................................................240
....................................................................................................................241
,
McGraw-Hill,
: ,
-. ,
,
.
.
,
++, : ,
,
, .
,
.
++ , , ,
, .
, ++, ,
++
.
, ++,
.
1979
. ,
, "". ,
.
" ",
,
" ".
.
, .
,
.
, .
, , ,
12 ++
, , , ,
. ,
- , ,
, ,
,
.
++ -
,
.
,
.
,
-
. , ,
.
,
,
.
, , .
, ,
: Microsoft Foundation Classes (MFC)
Microsoft. ,
,
- .
,
" MFC"; MFC ,
. MFC ,
.
.
, ++. ,
++ , ,
. ,
++. , ++,
"C+C++" (New York: McGraw-Hill,1993).
-
. "Object-
13
.
++, 2 ./. ..; .: " "
" ", 1998.560 .. .
, ,
.
,
++,
.
.
,
. ,
,
. ,
; ,
, , .
15
1. : ,
( )
( ), .
" " .
,
. ,
.
. , ,
, . . (
,
). ++:
,
.
, ,
, .
.
,
. ,
.
,
++ . (- )
,
( ) .
,
. ++
-.
; ,
( ,
, ),
. "".
, ,
"" , ,
.
,
"", , .
;
- , ,
16 ++
, .
15 ,
.
2. ( 1)
( Mach UNIX)
Microsoft. ,
,
,
. ,
,
Microsoft OLE 2.0
, - .
(OLE " ". OLE 2.0
,
.
).
,
Microsoft Foundation Classes (MFC), , OLE MFC
" 20000 ,
OLE 2.0". MFC,
, OLE 2.0
20000 . .
OLE
, ,
Microsoft .
2.1. ,
2.2. ,
OLE 2.0 ,
.
OLE. -,
.
++ OLE 2.0. OLE
17
++ ,
OLE. OLE
( ++)
++,
++, (
,
). , ,
OLE ++;
++, ++. ,
.
.
, ,
MFC ,
OLE, ,
,
OLE 2.0. ,
OLE, MFC, .
.
MFC ,
?
. OLE
:
?
?
, ,
, - .
, ,
,
, ,
, . (,
-
, ).
- -
.
-
,
.
,
18 ++
, ,
, ,
.
.
,
,
, ,
, . , ""
. , -
.
,
.
, , .
, ,
,
. -
, , .
3.
( )
, - ,
- .
,
, .
.
, , , .
,
, , :
. ,
, (, ,
) . , :
,
.
,
.
Windows . ,
" ".
" ",
19
Windows: ;
; ,
"172" 17 ;
? ,
.
,
.
,
. ,
, ,
,
. ,
(,
). 30
. 5 ,
, 10
. , , , .
, ,
,
.
Visual Basic (
, ). ,
, Visual Basic ( Power Builder,
Delphi, ) ,
,
.
,
.
, ,
.
, ,
( ) , ,
++,
,
.
21
4.
- Macintosh,
Windows . Mac
, .
, -
,
.
.
,
. - ,
100 ,
. , ,
.
, ,
.
.
( vi 15-
). , ,
, , -
. ,
, ?
.
.
. ( , ,
, ). ,
, , .
,
, .
"" ;
, :
"" , . .
22 ++
5.
, (
), ,
,
.
,
,
,
(
). .
. ,
, (, ),
. ,
- (, ).
.
6. -,
/++
, ""
,
.
, ,
,
. ,
.
, ,
.
.
, ,
.
, ,
,
, . ,
,
, .
23
,
, ,
,
.
,
.
,
, . ,
, ,
. ,
, ,
, .
,
.
(computer science)
, .
, ,
. (
,
,
, ).
- ,
, (
) , "
". ,
" "
, "".
, , -
, .
, .
,
. ,
,
,
,
.
24 ++
,
,
.
.
6.1.
,
. ,
,
, ,
. " ,
" "
".
, ?
7.
. , ,
. , ++
. . , ,
, .
, , ,
.
" ",
: ,
.
, -
. , ,
.
, . (
, ,
. ,
,
). - ,
, .
, ;
.
25
7.1.
. , ,
, , ,
, ,
, , ,
, . (
).
8.
.
,
.
.
, ,
, ,
- .
,
. ,
( ). ,
,
.
" " "
, ,
" :
tree t; //
node = postorder_first( t ); //
while( node ) // ?
node = postorder_successor( t ); // -
9.
9.1.
"
", ,
. ,
26 ++
++ ( ),
"".
, , -
, , -
. , , ,
.
.
, , ,
,
, .
, ,
, .
, .
"" , ,
. , ,
,
++ 3- ,
,
. " ++,
,
++". ,
"" .
, ,
.
.
.
, ,
. , ,
, .
,
, .
10.
,
. , , , ,
, .
.
27
.
,
. ,
" ,
, ". , , ,
; "".
,
( ),
, .
.
,
- .
, .
, ,
" ",
, (
) .
11.
,
,
"" ( )
, .
, :
.
, .
,
.
,
, .
,
.
, ,
, ,
, .
28 ++
, :
, . ,
: , , ,
.
, -
, , . !
-
"", . : " ,
, ". : ",
".
12.
13.
,
.
, (" "),
20
. - (),
, .
, , ,
.
.
, ,
.
,
, , , .
,
, .
,
, ,
.
, : " , ,
,
, ;
". ,
. ,
29
,
. ,
- . ,
, .
.
,
, ,
, , ,
, ,
. ,
, , , .
"" , , ,
( ) .
14. ( == )
.
350
,
. CP/M
1,2 . UNIX
16- PDP-11 c 64
( ).
32- 16
, . ,
.
. .
,
, ,
. ,
, . ,
, , .
.
UNIX " ".
,
, 5
, . ..
30 ++
,
. (
Microsoft (OLE) Windows, OpenDoc
Macintosh).
, ,
.
, .
,
. , ,
.
, ,
. ,
,
.
, ,
.
++.
.
32 ++
15. ,
. ,
- ,
,
,
. ,
- -
. -
, , (
) ,
,
.
,
, , .
. ,
( ), .
,
.
-
( ),
.
, .
16.
17.
18.
, ,
, ,
, . ,
, .
,
. (, ""
"".)
,
, , ,
33
( ,
).
,
, ,
, - .
, , -
,
. ", ".
,
,
. , ,
.
, , , ,
-. .
19.
"The Psychology of Computer
Programming" (New York: Van Nostrand Reinhold, 1971)
.
,
. ,
.
, .
, :
, ,
.
-
,
, , ,
.., .
,
, -
, ; .
,
.
34 ++
20.
,
- .
, ,
, ,
" ".
,
Apple I, -
S-100, ,
. (
, ,
). PDP 11/70,
UNIX ( 16-
64 ! ).
.
40 80
, . ,
,
,
, .
. ,
,
, .
.
, ,
,
, ,
, . , ,
, ,
, . ( ,
, 12
8).
, , ,
,
,
. ,
, .
35
: ,
. ,
, ,
, , , .
21.
,
.
.
, .
,
, .
,
, ,
.
, ,
, -
,
. ,
, .
:
. .
21.1.
,
"", , ,
,
, .
,
,
,
. ,
,
. ,
, ,
. ,
, .
,
36 ++
,
.
, .
; ++
, .
, ,
: ,
, .
.
,
, .
"", ,
(.. ,
). ,
.
,
. ,
, .
. , -
, " ,
, ".
, , ,
. , .
38 ++
22.
, ,
10 .
, ,
. "" ,
, , , ,
, . -
10
, , .
, - (
, ,
), ,
.
, ,
. ,
, ,
,
(, ..).
,
; ,
,
.
, . ,
" ",
.
23.
,
. ,
, .
, -
Web ( ) CWeb ( ++)
TEX1. arachne,
1
Web ""The WEB System of Structured
Documentation" (Palo Alto: Stanford University Dept. of Computer Science, Report
No.STAN-CS-83-980, 1983). CWeb .
"The CWeb System of Structured Documentation" (Reading: Addison
Wesley, 1994). ,
39
, "Compiler
Design in C". (Arachne ++,
troff).
.
,
,
.
,
("
"), .
,
, ,
.
24.
,
.
,
.
.
25.
,
,
, . . .
26.
.
:
++x; // x
:
.
, .
TEX - .
.
40 ++
/*-------------------------------------------
* :
*-------------------------------------------
*/
, .
27.
.
, . ,
, , .
, ,
( ) , ,
..
, , ,
, -
.
1,
. , 1,
. ,
,
,
, .
-,
.
1.
1 /*---------------------------------------------**
2 ** **
3 ** : 29 2000 . **
4 ** : **
5 ** equal **
6 ** **
7 ** : **
8 ** **
9 ** **
10 ** : **
11 ** **
12 ** . **
13 ** **
14 ** : **
15 ** Unicode. **
16 ** **
41
17 ** : **
18 ** . **
19 ** **
20 ** : **
21 ** char *s1; **
22 ** char *s2; **
23 ** **
24 ** : **
25 ** true, - **
26 ** . **
27 ** **
28 ** : **
29 ** . **
30 ** **
31 ** : **
32 ** . **
33 ** **
34 ** : **
35 ** **
36 ** : , **
37 ** : 12, , 1743 **
38 ** : **
39 ** **
40 ** : , **
41 ** : 13, , 1743 **
42 ** : str1, str2. **
43 ** **
44 ** **
45 ** . **
46 ** Copyright (c) **
47 ** . **
48 ** **
49 ** **
50 ** **
51 ** **
52 ** . **
53 ** . **
54 **------------------------------------------------------**
55 */
56 inline equal ( char *s1, char *s2 )
57 {
58 return !strcmp( s1, s2 ); // ,
59 } // .
,
, : " ",
" " . ,
,
, .
,
.
42 ++
28.
,
,
.
, .
,
, .
, , ,
:
// ,
// .
// :
//
// 1. ,
// 1
//
// 2. ,
// 2
//
// 1 ...
//
here_is_the_code();
while( some_condition )
{
this_code_is_rather_obscure(); /* 1 */
}
more_stuff_here();
while( some_condition )
{
this_code_is_also_obscure(); /* 2 */
}
29.
.
/* ,
* ,
* .
*/
,
++:
43
// ,
// ,
// .
,
:
/**********************************************************
void the_function( void )
, ,
.
***********************************************************
code_goes_here();
}
/**********************************************************/
-, , /
? .
-, ,
. :
/***********************************************************
* void the_function( void )
*
* , ,
* .
*
*
*
***********************************************************
*/
code_goes_here();
}
44 ++
30. ,
,
.
, ,
, -
, :
int x; // , x.
unsigned long int (*pfi)(); // , pfi.
const char *the_variable; // , the_variable.
int z; // , z.
x = 10; // .
the_variable = x; // .
z = x; // A .
:
int x; // , x.
unsigned long int ( *pfi )(); // , pfi.
int z; // , z.
"", "", ""
"".
++,
:
class derived : public base
{
string str;
const int x;
public:
derived( char *init_str, int init_x ) {}
}
31.
:
foo( int x )
/*
*
* . */
{
//...
}
,
.
, .
,
:
/*
** .
*/
foo( int x )
{
/* ,
** , .
*/
}
32.
-,
, :
while( a < b )
{
for( i = 10; --1 >= 0; )
{
f( i );
} // for
} // while
, , .
, ,
( 40 )
, .
, :
46 ++
:
while( a < b )
{
while( something_else() )
{
for( i = 10; --1 >= 0; )
{
for( j = 10; --j >= 0; )
{
//
- :
} // for
} // for
} // while
} // while
, .
.
:
} // for( j = 10; --j >= 0; )
} // for( i = 10; --1 >= 0; )
} // while( something_else() )
} // while( a < b )
#ifdef
#endif, #endif:
#ifndef __SOMEFILE_H_
#define __SOMEFILE_H_
// 1000
#endif // __SOMEFILE_H_
#else.
33.
, ,
. ,
- . - ( ,
) .
for, .
47
34.
. (
) ,
, ,
.
: ,
.
, . ,
,
, .
:
some_function( int, int, int, int, int );
.
35. ""
""
,
. , ,
: .
( ++ ). , ,
: ,
, .
:
, (,
) .
,
("").
, ,
,
. ():
if( its_thursday_and_the_moon_is_in_scorpio()
||its_friday_afternoon_and_close_to_quitting_time()
||i_just_cant_bear_to_look_at_this_computer_for_another_minute()
)
{
go_home();
}
48 ++
, ,
- ,
.
,
.
, ,
, if.
, ,
if :
if( its_thursday_and_the_moon_is_in_scorpio()
||its_friday_afternoon_and_close_to_quitting_time()
||i_just_cant_bear_to_look_at_this_computer_for_another_minute())
go_home();
36.
/++ ;
inline, .
,
.
.
,
.
, , ( ,
)
;
.
37.
, ,
.
, .
. -
,
300 .
, .
( 60
), ,
( 79 ,
49
),
. ,
,
.
38.
:
//---------------------------------------------------------
. ( ,
).
,
.
.
, .
, :
//==========================================================
//
//==========================================================
, .
, , ,
. :
//---------------------------------------------------------
void descriptive_name( type descriptive_name )
{
// -
// , , ,
// . ,
// . ( :
// " ").
//
// .
// ,
// .
//
// , , ,
// , .
// ,
// .
code_goes_here();
}
50 ++
39.
,
. ,
, ,
. (
) .
, ,
-. . :
(.. ),
.
, .
.
. "+",
"". ,
. (: a + b "a b",
a+b "ab").
,
(*p, a--,
f(arg,arg) ..).
. -> /++ .
: p->msg(), obj.msg().
, , -
. :
int *p;
y=(x/*p++);
f(int /* */);
, :
int *p;
y=(x
);
/* y=(x/*p++)
, */
f(). ( ,
, . ,
,
).
51
.
:
int* x;
, :
int* x, y;
, .
"
".
, ,
, .
int *x, y;
, x , y .
40.
, -2,
, .
. ,
, ;
.
.
. :
void f( void )
{
if( x )
yyy();
more_code();
even_more_code();
}
.
:
void f( void )
{
if( x )
yyy();
more_code();
even_more_code();
}
52 ++
41.
:
if( by_land )
one();
else
two();
:
if( by_land ) one() else two();
:
if( by_land )
{
one();
}
else if( by_sea )
{
two();
}
else if( by_air )
{
three();
}
. -, -
,
:
if( by_land )
one(); if(debug) printf("...");
else if ( by_sea )
:
if( by_land )
one();
if (debug)
printf("...");
else
if( by_sea )
.
, ,
, .
:
53
if ( by_land ) one();
else if ( by_sea ) two();
else if ( by_tunnel ) three();
:
if (by_land)one();
else if(by_sea)two();
else if(by_tunnel)three();
:
for ( a ; b ; c );
while ( ++i < 10 );
:
while ( i < 10 );
++i;
41.1. ,
,
.
, .
:
f()
{
/*
**
*/
code();
}
:
f()
{
int local_var;
int another_local_var;
code();
}
, ,
54 ++
( ). ,
, :
f()
{
int local_var;
int another_local_var;
code();
}
42.
. , ,
:
while ( some_condition )
{
//
}
:
if( condition ){
code();
}else{
more_code();
}
,
,
, .
43. ,
,
,
. ,
. ,
, :
if( something() )
{
*/ Quo usque tandem abutere Gatesalina, patientia nostra.
" , , ..."
. . .
55
*/
somethig_else();
}
.
,
.
(, state ?)
.
.
57
44.
, ,
,
; .
,
,
.
UNIX creat(); , create() .
lnghth length mt empty.
. , :
col ;
cur ;
i j ;
max ( );
min ( );
obj ( ,
);
p ptr ;
s str ( char*),
,
. , i
for,
:
for( i = 10; --i >= 0; ) // 10
putchar('-');
, -
. :
for( i = 0; i < imax; ++i )
for( j = 0; j < jmax; ++j )
move_cursor( i,j );
:
for( row = 0; row < max_row; ++row )
for( col = 0; col < max_col; ++col )
move_cursor( row, col );
x y row column.
.
58 ++
, .
44.1.
Microsoft ,
Microsoft.
Microsoft Windows .
, :
const char *str;
:
LPCSTR lpszstr;
2
,
, Microsoft Windows .
3
, . ,
,
. ,
. ,
59
, ,
p.
.
int i, double d,
f? ,
:
char str[128], *pstr = str;
c , , , .
:
char str[128], *first_nonwhite = str;
while ( isspace(*first_nonwhite) )
++first_nonwhite;
// *first_nonwhite
// , , "*pstr".
45.
_
,
.
, . ,
- ,
.
45.1.
,
, const.
, . ,
( ),
.
.
45.2.
, typedef
,
typedef, -
. , :
,
.
60 ++
:
(ptr_to_funct)( p ); // p
ptr_to_funct f(long); // f
:
#define PTR_TO_FUNCTION void (*) (int )
:
(PTR_TO_FUNCTION) ( p );
:
PTR_TO_FUNCTION f(long);
:
void (*) (int ) f(long);
:
void (*f(long))(int );
,
,
.
46. ANSI C
, ,
, _t, ANSI
C .
. , ANSI C
ISO/ANSI ++.
47. Microsoft
, Microsoft,
( Microsoft
). , ,
,
Microsoft,
Microsoft. MFC,
1998 . "ISO/IEC 14882, Standard for
the C++ Programming Language". : .
++, 3 . /. ..; .: " "
" ", 1999.991 . . .
61
, Macintosh
UNIX/Motif , , ,
.
Windows (API) 1200 .
MFC,
, 80 . ,
Microsoft
. Microsoft
,
- , , ?
Microsoft
ANSI C, ,
,
,
Microsoft:
Microsoft
(),
.
, ,
Microsoft(). - MFC
.
Microsoft ""
(, CString, CWnd, CDialog
..). "" , ,
Microsoft.
-
- . ,
MFC .
m_, ,
. ,
,
m_
MFC.
62 ++
48.
. ,
, ;
, FALSE. :
enum { INSERT_ERROR, DELETE_ERROR };
insert()
{
//...
return INSERT_ERROR;
}
delete()
{
//...
return DELETE_ERROR;
}
0
1.
49.
.
, :
int nwords(const char *str)
{
typedef enum { IN_WORD, BETWEEN_WORDS } wstate;
int word_count = 0;
wstate state = BETWEEN_WORDS;
for(; *str ; ++str )
{
if( isspace(*str) )
state = BETWEEN_WORDS;
else
if( state != IN_WORD )
{
++word_count;
state = IN_WORD;
}
}
return word_count;
}
state
63
: IN_WORD BETWEEN_WORDS.
:
int nwords2(const char *str)
{
int word_count = 0;
int in_word = 0;
state -,
,
IN_WORD
BETWEEN_WORDS. .
. :
enum child_type { I_AM_A_LEFT_CHILD, I_AM_A_RIGHT_CHILD };
struct tnode
{
child_type position;
struct tnode *left,
*right;
} t;
//...
t.position = I_AM_LEFT_CHILD;
if( t.position == I_AM_LEFT_CHILD )
//...
:
struct tnode
{
unsigned is_left_child ;
struct tnode *left,
*right;
} t;
t.is_left_child = 1;
if( t.is_left_child )
ISO/IEC 14882 bool.
is_left_child bool. .
64 ++
//...
.
:
enum { SOME_BEHAVIOR, SOME_OTHER_BEHAVIOR, SOME_THIRD_BEHAVIOR };
f( SOME_BEHAVIOR, x);
f( SOME_OTHER_BEHAVIOR, x);
f( SOME_THIRD_BEHAVIOR, x);
(
). , ,
:
some_behavior(x);
some_other_behavior(x);
some_third_behavior(x);
.
:
create_window( int has_border, int is_scrollable,
int is_maximized );
. ,
:
create_window( TRUE, FALSE, TRUE );
,
, .
:
enum { UNBORDERED =0; BORDERED =1}; // ,
enum { UNSCROLLABLE=0; SCROLLABLE =1}; // create_window()
enum { NORMAL_SIZE =0; MAXIMIZED =1}; // .
//...
create_window( BORDERED, UNSCROLLABLE, MAXIMIZED );
.
create_window(). ,
,
, :
if( has_border == BORDERED )
//...
:
if( has_border )
//...
65
. , -
BORDERED,
if . , ,
,
, .
,
, ,
.
.
67
50.
( " ,
").
,
- .
:
#define begin {
#define end }
while ( ... )
begin
// ...
end
, ,
-, ,
.
, .
. ,
:
typedef const char *LPCSTR;
LPCSTR str;
, -
, ,
typedef, , .
++,
, ++
LPCSTR. ++ ,
LPCSTR .
. (,
word 16-
, int,
ANSI , ANSI ++).
, (?:)
, . ,
,
, . , :
68 ++
, :
if ( str == NULL )
printf( "<>" );
else
printf( "%s", str );
printf().
++ --.
. :
while ( *p )
{
putchar ( *p );
++p;
}
:
for( ; *p ; ++p )
putchar ( *p );
:
while( *p )
putchar ( *p++ );
,
.
- ,
, .
:
struct tree_node
{
struct tree_node *lftchld;
};
,
, - ,
69
->.
left_child .
,
, ,
, ++.
51.
,
, . .
:
UpdateAllViews( CView *sender, long lhint, CObject *phint )
{
// sender lhint phint
// NULL xx xx ,
//
// Cview* 0 Crect* ,
// . phint
// ,
//
//
// Cview* 1 Crect* , CView*
// ("sender" - ). phint
// CView* .
}
: initial_update(),
update_embedded_object() update_view().
, - ,
. "".
.
52.
,
,
( ,
struct class), .
, . ,
-
:
struct tree_node;
struct child_ptr
{
unsigned is_thread;
70 ++
struct tree_node
{
struct child_ptr left,
right;
};
tree_node *p;
if( !p->left.am_a_thread )
p = p->left.child;
, ,
,
:
struct tree_node
{
struct tree_node *left_child;
unsigned left_is_thread : 1;
struct tree_node *right_child;
unsigned right_is_thread : 1;
};
if( !p->left_is_thread )
p = p->left_child;
53. ,
, (..
), ,
- .
, ,
, ,
- ,
"
". , ,
,
, ,
.
,
.
, .
71
,
,
, ,
.
.
,
, ,
5 6 .
, , 5 ,
.
53.1. , ,
.
,
, .
,
; ,
. ,
.
,
.
54.
. ,
goto ,
return.
, ,
. :
f()
{
int _ = ;
if( _ )
{
// ...
_ = ;
, , .
, ,
, . .
72 ++
goto ;
}
else
{
// ...
_ = _;
goto ;
}
:
return _;
}
++,
;
. ,
. ,
x , :
foo()
{
if( _ )
goto ;
_ x; // . (
// goto .)
// ...
:
// x
// x .
}
goto
++.
54.1.
, ,
. ( , "
" ,
). ,
,
return. :
if( a )
{
// ...
return __();
}
else
73
{
// ...
return ;
}
:
if( a )
{
// ...
return __();
}
// ...
return ;
, ,
.
55.
:
if( strcmp(a, b) < 0 )
{
}
else if( strcmp(a, b) > 0 )
{
}
else if( strcmp(a, b) == 0 )
{
}
strcmp() (
),
:
int cmp = strcmp(a, b);
if( cmp < 0 )
{
}
else if( cmp > 0 )
{
}
else // cmp == 0
{
}
74 ++
56.
.
,
. :
, .
,
.
,
.
.c,
.
static
( ). private
. ,
, ,
.
: .
, , , ,
.
56.1.
, ,
,
, . (
,
;
).
,
.
, ,
. ,
, , .
.
,
, , ,
, 10.
75
, , ,
, . , ,
.
, , ,
, . ,
.
56.2.
, :
. (
,
.
,
.
.
,
: A() B(), A(),
B(), ).
,
, ,
? , .
1:
static int glob;
get_glob( x )
{
return glob;
}
set_glob( x )
{
glob = x;
}
2:
76 ++
set_glob( 10 );
recursive_function();
x = get_glob();
;
, .
, :
set_glob(). :
static int glob;
,
- .
do_recursive(), ,
,
.
56.2.1.
,
, ,
, .
(
),
. :
f()
{
static int i;
// ...
f();
:
int f()
{
static int depth = 0;
static int depth_max = 0;
f();
--depth;
return depth_max;
}
depth
,
. depth_max
. depth ,
, .
56.3.
,
++, ,
.
, , ,
(
). .
,
(
). :
static int num_windows = 0; //
//
create_window()
{
78 ++
if( ++num_windows == 1 ) //
initialize_video_system();
// ...
}
destroy_window()
{
// ...
if( --num_windows == 0 ) //
shutdown_video_system(); //
}
++ .
56.4. if
return, else
:
if( )
return xxx;
else
{
__();
}
:
if ( )
return xxx;
__();
, return
, ,
.
. :
f()
{
// ...
if( x )
return 123;
else if ( y )
return 456;
else
return ERROR;
}
f()
{
// ...
79
return x ? 123 :
y ? 456 :
ERROR ;
}
,
.
,
, :
if( A )
return FAIL;
else if( B )
return SUCCESS;
else
{
//
return SUCCESS; //
// .
}
. -,
,
:
if( A )
return FAIL;
else if( B )
;
else
{
//
}
return SUCCESS;
if, :
if( A )
return FAIL;
else if( B )
{
//
}
return SUCCESS;
80 ++
57.
if/else
, if/else (
) ( ),
, , :
if( _() )
error( "!!!!" );
else
{
// 30
}
. ,
:
if( !_() )
{
// 30
}
else
error( "!!!!" );
, if
else , if. ,
, else,
, .
, ,
else. if , ,
.
58.
,
.
. ++
, ,
; ,
. ,
,
(" ")
.
81
,
, ++.
,
;
. ,
, NULL, ,
,
.
,
, .
, ,
. , ,
,
. , :
f()
{
// , i
int i = init_val;
// , i
}
:
f()
{
// , i
g( init_val );
}
g( int init_val )
{
int i = init_val;
// , i
}
- ,
for, .
, ,
. .
59.
, for, .
.
82 ++
. (
++ ).
:
typedef enum shape_type { CIRCLE, LINE, TEXT };
typedef struct
{
shape_type type;
union shape_data
{ // .
} data;
} shape;
print_shape( shape *p )
{
switch( p->type )
{
case CIRCLE: print_circle( p );
case LINE: print_line ( p );
case TEXT: print_text ( p );
}
}
:
typedef struct
{
void (*print)( struct *shape );
union shape_data;
{ // .
}
}
shape;
print_shape( shape *p )
{
( p->type )( p );
}
83
:
shape_type.
print_shape() .
print_shape() ,
.
60. do/while
do/while ,
. ,
. ,
:
if( !_ )
return ERROR;
do
{
();
} while( _ );
:
while( _ )
();
:
if( _() )
do
//
while( _() && _() );
:
while( _() )
{
//
if( !_() )
break;
}
1979
do/while .
84 ++
60.1. do/while
, :
do
{
//
} while( 1 );
, while, ,
while (1) .
61.
,
,
.
, ,
, , .
:
for( i = 0; i < max; ++i )
;
,
,
,
.
62.
( ):
int array[ARRAY_SIZE];
int *p = array;
for( i = 1; i < ARRAY_SIZE ; ++i )
*p++ = 0;
85
, (i
1, array[0]),
.
( ), , ,
,
a[i], :
( a + ( i* sizeof(a[0])))
:
int array[ARRAY_SIZE];
int *current = array;
int *const end = array + (SIZE-1);
while( current <= end )
*current++ = 0;
( ) :
int array[ARRAY_SIZE];
int i;
for( i = 0; i < ARRAY_SIZE ; ++i )
array[i] = 0;
, ,
,
. , i
.
:
for( current = array; current <= end; ++current )
{
// ...
f( current - array ); // f()
//
}
, , ,
:
while( (current - array) < ARRAY_SIZE )
// ...
63. for,
,
while. :
int x = 10;
86 ++
// 200 , x
//
while( x > 0 )
{
// 200
f( x-- );
}
, ,
-- .
x-- for. ++
, ,
x for:
int x = 10;
for ( ; x > 0 ; --x )
{
// 200
f(x);
}
( , ++ for (
int=0;..., ,
x ,
, for.
).
for ,
, :
for( ___ = f();
___ ;
___ = f() )
{
// ...
}
:
int ___ = f();
for(; ___;
___ = f() )
{
// ...
}
, ,
for. .
87
int ____ = f();
for(; ; ____ = f() )
{
if( !____ )
break;
// ...
}
,
. :
int ____ = f();
while( ____ )
{
//
____ = f();
}
64. , ,
for
for ,
,
, , , , ,
for ,
, .
:
int *ptr;
// ...
for( ptr = array, i = array_size; --i >= 0; f(ptr++) )
;
:
int *ptr = array;
for( i = array_size; --i >= 0 ; )
f( ptr++ );
65. ,
"
".
" ".
1. ,
88 ++
char , -10,
11110110 ( 0xf6).
16- int 0xfff6. 8-
char int
, ,
int, char int,
( ) char
. .
: " " (
, ,
); " " . ,
, ,
/++, :
, ,
. ,
, .
.
,
malloc() NULL
. , ,
, ,
,
, .
,
, , ,
.
66.
,
. .
, , :
int x = 32767;
x = (x * 2)/ 2;
( 16- x -1.32767 0x7fff.
2 ,
0xfffe .
, 0xffff -1).
89
.
,
; ,
; .
,
, .
.
, ,
" ". ,
, :
long x;
x &= 0xffff; // , 16-
// 32- long.
:
x &= 0xffffUL;
66.1.
switch
default ,
:
f( int i ) // i 1 2.
{
switch( i )
{
case 1: _(); break;
case 2: __(); break;
default:
fprintf(stderr, " f():
90 ++
i (%d)", i );
exit( -1 );
}
}
if/else, ,
switch.
.
, i 0
:
f( int i ) // i
{
while ( --i >= 0 )
_();
}
while(--i) ,
, i 0.
66.2.
, ISO/ANSI ++ ,
new ,
, ,
,
, new NULL.
,
, fopen(),
FILE.
67.
, ,
. , :
int x = *p++;
f( x );
:
f( *p++ );
, ,
,
, .
, ,
. ,
:
91
:
double lift = Coefficient_of_lift * (0.5 * RHO * square(v));
f( lift );
, ,
, , .
68.
.
,
. ( ,
#define). :
,
.
,
.
.
, (128):
f()
{
char buf[128]
...
fgets( buf, sizeof (buf) / sizeof(*buf), stdin );
}
sizeof() fgets(),
.
.
69.
, , int
32 . , 32-
16- int (
Intel 80x86):
double a[1000], *p = a;
// ...
dist_from_start_of_array_in_bytes = (int)p - (int)a;
( ++):
92 ++
g()
{
doesnt_work( 0 );
}
doesnt_work( char *p )
{
if( !p ) //
// ...
}
,
( ++, ). 0
int, 16- .
32- , 16
16 32-
. , if(!p) ,
16 32 0.
typedef :
typedef int word; // 16
typedef long dword; // 32 .
typedef
, , word -
16 , dword 32 . 32-
:
typedef short word; // 16
typedef int dword; // 32 .
,
ANSI .
ANSI wchar_t
Unicode 16-
. ANSI ,
L. Microsoft
, :
#ifdef _UNICODE
typedef wchar_t _TCHAR
# define _T(x) L##x
#else
typedef char _TCHAR
# define _T(x) x
#endif
_UNICODE , :
93
_TCHAR *p = _T("__");
:
char *p = "__";
_UNICODE ,
:
wchar_t *p = L"__";
.
Unicode,
char _TCHAR
_T(). , , (
_TCHAR char), :
_TCHAR str[4];
// ...
int max_chars = sizeof(str); // , char
// 1
_TCHAR 2
_UNICODE,
, .
:
int max_chars = sizeof(str) / sizeof(*str);
70. (
)
.
"
".
, ,
,
. ++, ,
,
.
,
, ,
.
, ,
int. "malloc()
, int":
int *p = (int *) malloc( sizeof(int) );
94 ++
// ...
unsigned y;
f( y );
f((int)y). , ,
unsigned int ,
int ,
.
, .
, , :
some_object array[ size ];
int my_cmp( some_object *p1, some_object *p2 );
:
some_object array[ size ];
void foo( int x );
, *lp = 0
, x .
, ,
short x,
.
, ,
. ,
p:
p = (char *)(long *);
71.
,
. ,
:
print( const char *str )
{
96 ++
if( !*str ) // ,
return;
while( *str )
putchar( *str++ );
}
if ,
while.
2 3 .
2
.
2. : 1
1 typedef struct node
2 {
3 struct node *next, *prev;
4 //...
5 } node;
6
7 node *head;
8
9 remove( node **headp, node *remove )
10 {
11 // , remove,
12 // , *headp.
13
14 if( *headp == remove ) // .
15 {
16 if( remove->next ) //
17 remove->next->prev = NULL; // ,
18 //
19 //
20 // .
21 *headp = remove->next;
22 }
23 else //
24 {
25 remove->prev->next = remove->next;
26 if( remove->next )
27 remove->next->prev = remove->prev;
28 }
29 }
3 ,
node, next
.
,
, remove .
,
, ,
97
. ,
.
3. : 2
1 typedef struct node
2 {
3 struct node *next, **prev; // <== prev *
4 //
5 } node;
6
7 node *head;
8
9 remove( node **headp, node *remove )
10 {
11 if( *(remove->prev) = remove->next ) //
12 remove->next->prev = remove->prev; // ,
13 //
14 }
72. lint
Lint . (
++ MS-DOS/Windows.
Gimple Software).
,
,
, .
, , :
(void ) printf("...");
:
while( p = f() )
g(p);
= ==. ( ,
.
, ,
,
).
( )
. ,
, = ==:
98 ++
, ,
.
73. ,
,
,
: , ,
. ,
. , ,
malloc() ,
free()
.
,
, ,
. ( __LINE__ _FILE__
malloc()).
. ,
, , ,
.
, .
, free() ,
malloc(). , :
void user( void ) //
{
p = malloc( size );
producer( p ); //
consumer( p ); //
free( p );
}
:
void *producer( )
{
void *p = malloc( size );
// ...
return p;
}
void consumer( void *p )
{
99
// ...
free( p );
}
void user( void )
{
void *p = producer();
consumer( p );
}
, . ,
, malloc(). :
some_object *p = allocate_and_init(); // ,
// .
:
some_object *p = malloc( sizeof(some_object) );
if( !p )
fatal_error(" !")
init();
++ .
74.
malloc()/free()
( new/delete) , ;
.
50% malloc() free()
. ,
,
4 .
4.
1 typedef struct some_class
2 {
3 struct some_class *next;
4 //...
5 }
6 some_class;
7
8 static some_class *free_list = NULL;
9 //--------------------------------------------------------
10 free_object( some_class *object )
11 {
12 // , -
13 // free(),
14 // .
15 object->next = free_list;
100 ++
16 free_list = object;
17 }
18 //--------------------------------------------------------
19 free_all_objects( )
20 {
21 //
22 // .
23 //
24 // , . (
25 //
26 // Quicksort).
27
28 some_object *current;
29 while( free_list )
30 {
31 current = free_list;
32 free_list = current->next;
33 free( current );
34 }
35 }
36 //--------------------------------------------------------
37 some_class *new_object( )
38 {
39 // ,
40 // .
41 // malloc(),
42 // .
43 some_class *object;
44 if( free_list )
45 {
46 object = free_list; //
47 free_list = object->next;
48 }
49 else
50 {
51 // ,
52 // 100, 1
53 // , free_all_objects().
54
55 object = malloc( sizeof(some_class) );
56 }
57
58 if( object )
59 //
60
61 return object;
62 }
75.
,
101
.
,
. ,
, , .
,
, .
, , ;
#ifdef TEST
. ,
,
, .
76.
,
-, CP/M, DDT
. , , :
?
, , ,
, ,
. Windows, ,
,
, , .
,
, ,
, .
,
, :
.
, .
, .
,
, ,
. ( Windows "" -
. , ).
: , Ctrl-C
( ),
, ,
. ,
: ,
102 ++
,
, "-" .
, - .
,
, - :
17 63 .
:
--.
- ( "help/c")
, . ,
( "").
77. ,
(
) .
,
.
- . ,
- ,
,
,
. ,
.
78. -
. (
printf() fprintf(stderr, ...)
). ,
,
, ).
:
#define error printf
#define terminate(x) ExitProcess(x)
:
103
if ( some_error )
{
error(" - ");
terminate( -1 );
}
:
#include <stdio.h>
#include <stdarg.h>
#ifdef WINDOWS
void error( const char* format, ... )
{
char buf[255]; // ,
//
va_list args;
va_start( args, format );
if( vsprintf( buf, format, args ) < sizeof(buf) )
::MessageBox(NULL,buf, "*** ***",
MB_OK | MB_ICONEXCLAMATION );
else
{
::MessageBox(NULL,
" .",
" ",
MB_OK | MB_ICONEXCLAMATION );
ExitProcess( -1 );
}
va_end( args );
}
#elif MOTIF
// ,
// Motif
#else
void error( const char* format, ... )
{
va_list args;
va_start( args, format );
vfprintf(stderr, format, args );
va_end ( args );
}
#endif
++ ,
. ,
++ , ,
.
.
, ,
,
.
,
, ,
( ). ++
,
(inline) ,
.
, .
(
), , ,
. ,
,
. ,
,
.
105
79. .h
, , .c
,
. ,
.
, .
, ++.
,
, .
.cpp
,
.
80. #include
,
,
#include ,
++. , .
- , -
, .
#include
. ,
, ,
. , ,
- .h ,
. :
#ifndef FILENAME_H_
#define FILENAME_H_
, :
#endif // FILENAME_H_
. FILENAME_H_
,
.
106 ++
81.
" (
) ". -, ,
, . (
, .
, ).
.
-, ,
.
:
:
#define end() while(*p) \
++p
.c:
char *f( char *str )
{
char *p = str;
end();
// ...
return p;
}
. -, p ,
, .
, , p .
, , end(),
, p.
. :
#define end(p) while(*p) \
++p
.c:
char *f( char *str )
{
end(str);
// ...
return str;
}
107
str,
. ( ++ ,
. ,
++). str
, .
(- ) , end()
. :
#define end(p) while(*(*p)) \
++(*p)
.c:
char *f( char *str )
{
end(&str);
// ...
return str;
}
end(&str) :
while(*(*&p))
++(*&p)
*&p , p, * &
:
while(*(p))
++(p)
,
, .
:
#define two_things() a();b()
if( x )
two_things();
else
something_else();
( ,
):
if ( x )
a();
b();
else
something_else();
108 ++
" else
if". ,
.
:
#define two_things() { a(); b(); }
:
if( x )
{
a();
b();
}
;
else
something_else();
,
two_things() . ,
. ,
. else
, " else
if".
, , ,
, .
, .
"
" ( ):
#define two_things() ( a(), b() )
,
for. (,
,
).
(
, b()). :
x = ( a(),b() );
:
a();
x = b();
, ,
, . (:
109
a()+b();
, ,
- ). ,
; b()
.
. ,
.
, .
((a())+(b())).
, .
,
, .
,
, , .
, . ( ,
, ,
).
, :
#define two_things() \
do \
{ \
a(); \
b(); \
} while( 0 )
if( x )
two_things();
else
something_else();
:
if( x )
do
{
a();
b();
} while ( 0 ) ; // <==
// while ( 0 )
else
something_else();
:
#define two_things() \
if( 1 ) \
110 ++
{ \
a(); \
b(); \
} else
, do while (0) .
,
,
.
,
:
#define swap_int(x,y) \
do \
{ \
int x##y; \
x##y = x; \
x = y; \
y = x##y \
} \
while (0)
## ANSI .
,
.
:
swap(laurel, hardy);
( x laurel, y hardy),
:
int laurel##hardy;
,
int laurelhardy;
. , -
,
. ++ ,
:
#define _AT_LEFT(this) ((this)->left_child_is_thread ? NULL\
:(this)->left)
#ifdef DEBUG
static tnode *at_left(tnode *this) { return _AT_LEFT(this); }
111
#else
# define at_left(this) _AT_LEFT(this)
#endif
, , ,
,
, . ,
,
.
, ,
1, .
( , ,
- ).
1. ,
: , :
( a && f() ) if( a )
f();
( b || f() ) if( !b )
f();
( z ? f() : g()) if( z )
f();
else
g();
,
&& ||
,
. a &&
f(). a , , f(),
, .
, f(), a ,
f(), a . b,
f() , b, , .
81.1. ?: ,
if/else
1 .
.
.
if/else, ,
&& || if.
112 ++
:
if( z )
i = j;
else
i = k;
:
z && (i = j);
z || (i = k);
,
:
z ? (i = j) : (i = k) ;
.
,
, (.. ),
if/else:
i = z ? j : k ;
81.2.
, , ,
, . :
#define TWO_K 1024 + 1024
:
10 * TWO_K
:
10* 1024 + 1024
:
(10 * 1024) + 1024
:
#define TWO_K (1024 + 1024)
:
#define SQUARE(x) (x * x)
:
SQUARE(y + 1);
113
:
y + 1 * y + 1
:
y + (1 * y) + 1
. :
#define SQUARE(x) ((x) * (x))
:
((y + 1) * (y + 1))
x = TWO_K * 10
11264 (1024+(1024*10))
20480. :
enum { two_k = 1024 + 1024 };
:
const int Two_k = 1024 + 1024;
, .
.
enum : -
, const int
int .
, . ,
;
extern -
. ( ++ ,
,
. ++
).
,
. ,
. ,
114 ++
.
.
. ,
default_i
f():
void f( int i )
{
enum { default_i = 1024 };
if ( !i )
i = default_i ;
}
:
void f( int i )
{
#define DEFAULT_I 1024
if ( !i )
i = DEFAULT_I ;
}
DEFAULT_I ,
. DEFAULT_I
,
.
, .
enum ++,
.
, ++.
,
case .
.
83.
SQUARE()
. :
#define SQUARE(x) ((x)*(x))
,
. .
115
SQUARE(++x) x.
. x 2,
SQUARE(++x) 3 * 4.
,
.
SQUARE(++x) ,
. ++ ,
,
.
, :
double square( double x )
{
return x * x;
}
, , ,
.
83.1.
:
#define SPACE ' '
,
( , ,
).
:
#define SPACE 0x20
(' ')
,
, . ,
ASCII, 0x20, EBDCDIC .
, - .
116 ++
84. ,
, ++,
++. , .
:
#ifdef DEBUG
# define D(x) x
#else
# define D(X) /* */
#endif
D() ,
, . :
f()
{
D( printf(" \n"); )
}
D() printf(),
, .
,
.
( ) ( .c),
.
:
#ifdef ALLOC
# define I(x) x
# define EXPORTED /* */
#else
# define I(x) /* */
# define EXPORTED extern
#endif
(
globals.cpp) :
#define ALLOC
#include "globals.h"
#define ALLOC. globals.cpp,
117
#define ALLOC :
/* */ int glob_x[10] ={1, 2, 3, 4} ;
/* */ some_object glob_y ("", "");
#define ALLOC :
extern int glob_x[10] /* */ ;
extern some_object glob_y /* */ ;
ASSERT(),
, ( #define
DEBUG) ASSERT()
"". , ,
NULL. ASSERT(), :
f( char *p)
{
ASSERT( p, "f() : NULL." );
}
:
#ifdef DEBUG
#define ASSERT(, )
if ( !() ) \
{\
fprintf(stderr, "ASSERT(" # ") "\
"[ " __FILE__ ", %d]:\n\t%s\n",\
__LINE__, () );\
exit( -1 );\
}\
else
#else
# efine ASSERT(c,m) /* */
#endif
ASSERT()
:
ASSERT(p) [ whatever.cpp, 123]:
f() : NULL.
.
,
__FILE__ __LINE__. ,
, ANSI
( #),
. #
"p" ).
118 ++
,
fprintf().
,
,
. ++
, .
, ANSI ,
assert(expr) assert.h,
ANSI .
ANSI assert() ,
NDEBUG ( ).
, .
120 ++
85. ( 2)
. ,
. .
85.1.
, ,
,
2.
2.
type *end = array; type *end =
end += len-1; array+(len-1) .
while (*p++ != '\0') while ( *p++)
while (gets(buf) != while (gets(buf) )
NULL)
if ( p != NULL ) if ( p ) !=0
if ( p == NULL ) if ( !p )
if ( != 0) if ( )
if ( == 0) if ( ! )
if( ) return ; ( return !=
return TRUE; 0).
else ,
return FALSE;
return TRUE.
return ?0:1; return !;
return ?1:0; return !=0;
.
! !=
1
0.
++x; f( x-1 );
f(x); ,
--x;
.
return ++x; return x+1; . .
int x; f(x); x
f( (int)x ); int.
(void) printf(" printf("
"); "); ,
, 121
.
if ( x > y ) if ( x > y )
else if ( x < y ) else if ( x < y )
else if ( x ==y ) else ,
.
*(p+i) p[i]; , ,
.
,
,
.
, ,
, .
, , ,
:
a + b;
, :
a += b;
, , .
85.2. ;
, ,
, ,
. , :
struct fred
{
int status;
// ...
};
/* */. .
122 ++
17 , , ,
- , , .
,
. - :
if ( struct.status &= ~CONDITION_A )
// ...
, . ,
:
struct.status = POSSIBILITY_A;
if ( POSSIBILITIES(struct.status) == POSSIBILITY_A )
// ...
;
. ( ,
, , ,
,
- ,
.
).
enum { possibility_a, possibility_b, possibility_b,
possibility_d };
struct fred
, 123
{
unsigned in_condition_a : 1;
unsigned in_condition_b : 1;
unsigned in_condition_c : 1;
unsigned possibilities : 2;
};
, ,
, :
struct fred flintstone;
flintstone.in_condition_a = 1;
if ( flintstone.in_condition_a )
// ...
flintstone.possibilities = possibility_b;
if ( flintstone.possibilities == possibility_a )
// ...
;
- int,
.
85.3.
"" ++.
.
:
BOOL = FALSE;
while ( ! )
{
if ( _() )
= 1;
}
:
while ( 1 )
{
if ( _() )
break;
}
,
, ,
, .
124 ++
++, goto
.
54.
85.4. ,
- :
#define SHIFT_LEFT(x, bits) ((x) << (bits))
, , ,
,
. ,
, .
85.5. ,
(#define TRUE)
, :
#define TRUE 1
#define FALSE 0
,
f()
"", 1,
:
if( f() == TRUE ) // , f()
// "", 1.
// ...
, . ,
- :
#define FALSE 0
if( f() != FALSE )
// ...
,
. ,
, ,
.
, 125
:
if( _(p) )
, :
if( _(p) != FALSE )
TRUE FALSE ,
-
. , FALSE
-1, TRUE 0. ,
TRUE FALSE,
, ,
TRUE FALSE (
! ).
:
if( _() )
, ,
0.
,
:
#define FALSE 0
#define TRUE !FALSE
!, ,
1, "" ( ), 0, .
:
#define FALSE 0
#define TRUE 1
, :
#define IS_TRUE(x) ((x) == 0)
#define IS_FALSE(x) ((x) != 0)
126 ++
86. 1
unsigned
, ANSI
, , :
struct fred
{
int i : 1;
}
a_fred;
0 -1. :
#define TRUE 1
// ...
if( a_fred.i == TRUE )
// ...
, a_fred.i 0
-1, 1. , if
.
87. , ,
ANSI ,
, , ,
. ANSI ,
, ,
, .
, , ,
80x86.
:
int array[ SIZE ];
int *p = array + SIZE; // ;
// .
while ( --p >= array ) // -
// .
//...
,
,
0x0000. ( 8086
,
). p (0x0000),
, 127
--p 0xfffe (
int 2 ), , p.
, .
:
while ( --p >= array )
{
// ...
if ( p == array )
break;
}
:
int *p = array + (SIZE - 1);
do
{
// ...
, p .
( p+(SIZE-1),
p+SIZE).
88.
,
, . , ,
, :
struct thing
{
int field;
int another_field;
int another_field;
};
array[row][col]
. :
array + (row * size_of_one_row) + (col * size_of_a_thing)
12 , 12 2,
128 ++
.
:
thing *p = (thing *)array;
int n_cells = nrows * ncols;
while ( --n_cells >= 0 )
(p++)->field = 0;
.
p++ 12 p.
, ,
,
. ,
,
.
,
, , :
for ( row = 0; row < nrows ; ++nrows )
for ( col = 0; col < ncols ; ++cols )
f( array[row][col] );
f()
, , ,
, . , f()
++,
,
, ,
.
, ,
, ,
,
. ,
, , .
89. goto,
.
goto ++ ,
54 ,
.
goto , goto
, 129
, .
, , goto
.
goto .
, , ,
, :
while ( 1 )
{
while ( )
{
// ...
while ( _ )
{
1:
// ...
goto 2;
}
// ...
}
if ( _ )
{
// ...
if ( _ )
goto 1;
else
{
2:
// ...
}
}
}
, , ,
, goto.
, goto
. switch, , goto
case.
, :
switch( _ )
{
case A: if ( __ )
// ...
else
{
case b: // ...
}
}
130 ++
goto . :
goto ,
return, , return.
,
.
, ;
.
:
// ...
exit:
return ;
}
goto ,
, , "",
.
while
100 , "" 1000000 ,
int = 0;
int 1, 2, 3;
// ...
while ( ! && 1 )
{
while ( ! && 2 )
{
while ( ! && 3 )
{
if ( _ )
= 1;
}
}
}
goto
:
while ( 1 )
{
while ( 2 )
{
while ( 3 )
{
if ( _ )
goto ;
}
, 131
}
}
:
// ...
,
,
.
.
,
.
++
,
++. "",
++, ,
.
, , ++.
, ++,
"+++".
- . 2-
"Object-Oriented Analysis and Design with Applications"
(Redwood City: Benjamin Cummings, 1994).
, ,
, .
133
8.
90. -
""
90.1. ,
, ,
. -
, - () ,
, ,
"". ,
, .
,
. (
,
).
++ .
, ++, -
, ,
-
- , .
, ++
"" . , ,
.
- ,
. -
.
, , -
, .
" (
), " " -
:
, ".
, ,
( ),
134 ++
: " ++
, ,
,
". ( : "
, ;
, ,
. , .
").
, -
, , ,
,
,
. , ,
, ,
, "
".
++ , .
++ ,
. ,
++, ,
, .
, , ++
.
- ,
,
.
, .
++? ,
++
.
(
)
,
.
.
,
.
,
- ,
135
,
, . , -
,
. (
:
,
).
, ,
.
, , ,
.
- ,
,
, .
, " , ",
. ,
,
, .
" -"
:
.
, .
-,
, ,
-
. ,
.
91.
, , ++,
-
, . , -
,
.
,
. , ,
,
136 ++
. ,
,
. -
.
92. ++
++,
, ,
, , ++.
, , ,
++
, ,
. ,
++, .
++.
, ,
,
, .
, , ,
. , ,
, , ,
,
.
,
,
, .
,
. ++
,
++, , .
++ ,
, , ,
- .
137
93.
, ++ ,
,
, . -,
. ,
-
. (
, , ..).
.
.cpp, , ... 5 6
( , ++)
( -
, ,
).
5. base.tem
1 class base
2 {
3 cls obj;
4 public:
5 virtual
6 ~base ( void );
7 base ( void );
8 base ( const base &r );
9
10 const base &operator=( const base &r );
11 private:
12 };
13 //------------------------------------------------------
14 /* */ base:: ~base( void )
15 {
16 }
17 //------------------------------------------------------
18 inline base::base( void ) : obj( value )
19 {
20 }
21 /-----------------------------------------------------
22 inline base::base( const base &r ) : obj( r.obj )
23 {}
24 //------------------------------------------------------
25 inline const base& base::operator=( const base &r )
26 {
138 ++
6. derived.tem
1 class derived : public base
2 {
3 cls obj;
4 public:
5 virtual
6 ~derived ( void );
7 derived ( void );
8 derived ( const derived& r );
9
10 const derived &operator=( const derived &r );
11
12 private:
13 };
14 //------------------------------------------------------
15 /* */ derived:: ~derived( void )
16 {
17 }
18 //------------------------------------------------------
19 inline derived::derived( void ) : base( value ) ,
20 obj( value )
21 {
22 }
23 //------------------------------------------------------
24 inline derived::derived( const derived &r ) : base ( r ),
25 obj( r.obj )
26 {}
27 //------------------------------------------------------
28 inline const derived& derived::operator=( const derived &r )
29 {
30 if( this != &r )
31 {
32 *((base *)this) = r;
33 obj = r.obj;
34 }
35 return *this;
36 }
139
94. ,
-
.
employee.
struct
. ,
.
- , name char
16- Unicode,
name .
-
name.4
, :
, , name
employee. employee -
, " ", "
" " , ".
,
.
,
,
. ,
- .
name Unicode, .
.
95.
-
,
.
4
, ++,
" -".
" - name",
"", .
140 ++
, ,
" ",
device,
. ,
, . (
: , -
" ". ,
"
", " "
" ").
, ,
. - ,
.
printf(), ,
Windows Motif . -
.
- ,
device, .
, ,
, .
,
.
" " :
. -
" "
. , ,
, .
,
, , .
: ,
, -
.
, " ,
" .
141
,
, " ",
. ,
, .
, , , ,
, , - ,
, . -,
,
.
.
, - .
, .
(,
) , " ",
: " - ,
, ,
, ". , ,
,
, , .
,
,
, ,
, ,
.
96.
97.
-
++
. (
.
).
142 ++
, .
, ,
.
, ++,
, ,
. , ++
. , ,
,
.
Smalltalk,
,
.5 ( Smalltalk,
++). ++ .
,
. , ++ ,
, ; ,
. , ,
.
.
.
() (
).
, ,
,
,
.
5
. Smalltalk ,
, . , Smalltalk
, .
,
. ++,
. ,
,
.
++ ,
. .
143
, ,
.
. :
class string //
{
// ...
public:
const string &operator=( const string &r );
};
class numeric_string // ,
{
string str;
// ...
public:
const string &operator=( const string &r );
}
return *this;
}
, ,
operator=() ,
numeric_string string
.
, +
string ,
+ numeric_string
(.. ,
).
.
, , ,
3, .
, ,
some_cls,
. "
" ,
, . ,
144 ++
, "
", ,
.
"" "
".
3. ,
class container class base : public some_cls
{ {
some_cls contained; // ...
// ...
}; };
98.
,
. ,
, ,
. , ,
, ,
. , .
99.
, ,
.
, ,
. ,
, , ,
, ,
.
. , ,
A, B C, , A, B, D
E, ,
A B,
C,
D E.
,
. .
, , ,
145
. , manager
employee,
(,
). manager
employee, ,
employee.
, . -
.
,
. (public) -
, .
. .
.
99.1.
.
,
,
. ,
.
100. , ,
101. ++ Smalltalk:
object
, , .
++: object, ,
Smalltalk. Smalltalk, , ,
++.
object? ,
? , ,
146 ++
, .
++ new,
.
++, object
Smalltalk. ++
. ,
++ 6 :
, ,
Smalltalk,
++. ,
Smalltalk ++ ,
++. ,
++ ,
++,
. Smalltalk
( ) , ++,
.
, Smalltalk
, , , ,
,
. ++
,
. ,
.
.
, .
,
,
. Smalltalk,
. , (
),
, , ,
.
6
,
BIX 1992 .
"Advanced Win32 Programming"(New York: Wiley,1993), pp.72-78.
147
-.
. employee,
:
class employee
{
// , :
// , ..
};
database list_of_managed_emploees;
}
manager *this_boss;
}
,
employee,
manager.
,
, .
:
class storable; //
, add() database
storable .
storable ( , storable)
database -
, database.
,
, . , ,
100 1.
, . ,
manager ,
148 ++
.
.
class storable;
, ""
.
" ", " ".
. ,
"" , -
,
, , .
,
- (,
, , ..).
.
++
,
:
class parent {}; //
. parent
go_to_sleep(), ,
:
child philip; // -
philip.go_to_sleep(); // , !
, child
parent. , (-
) (-). mother
parent:
149
parent.7 father.
, child mother father,
parent. philip.go_to_sleep() ,
, parent
: , mother, , father.8
,
( ):
class parent { public: go_to_sleep(); };
:
class parent {};
child
parent, mother father.
, . -,
,
7
. mother parent,
mother, ,
" parent".
8
,
, parent child
go_to_sleep(), .
, ,
. ,
, this,
- .
150 ++
. ,
tree_list_node ,
:
class node;
class list_node : public node {};
class tree_node : public node {};
tree_list_node
, , :
class node;
class list_node : virtual public node {};
class tree_node : virtual public node {};
tree_list_node,
.
.
list_node tree_node, ,
node, . node,
? .
node
(tree_list_node).
, - ,
.
,
:
class persistent
{
public:
virtual flush() = 0;
};
};
102.
103.
104.
,
,
(
, ):
, ,
.
,
,
.
,
( ).
,
. ,
.
152 ++
105. ,
106. , ,
107.
,
,
,
( ,
). ,
, ,
,
, . ,
. , .
, ,
.
: , .
,
, ,
, - ,
, ;
. (
, ) ,
( , , ).
,
,
. :
class derived : public base
{
};
:
class derived
{
base base_obj;
153
};
,
.
Windows,
" " "".
(
"" " ") .
, , ,
.
, . ""
, "" ,
.
, ,
. "" Microsoft Windows
.
, , ,
. ,
, ,
, ,
.
108.
. , ,
, , ,
. ,
-
:
,
.
(private) "" ( "") ,
.
154 ++
109.
110.
. .
.
,
. . ,
string, char
. ,
,
Unicode. -
char*, (public),
, char*, .
. :
class string
{
public:
char *buf;
// ...
};
f()
{
string s;
// ...
printf("%s/n", s.buf );
}
buf wchar_t*
Unicode ( ANSI ), ,
buf, .
.
. length,
length,
. ,
, ,
new, buf
delete. ,
- :
155
string s;
char array[128];
s.buf = array;
,
.
private buf
,
. 7 ,
. (, ,
;
, .cpp).
7.
1 class string
2 {
3 char *buf;
4 int length; // ( );
5
6 public:
7 virtual
8 ~string( void );
9 string( const char *input_str = "" );
10 string( const string &r );
11
12 virtual const string &operator=( const string &r );
13
14 virtual int operator< ( const string &r ) const;
15 virtual int operator> ( const string &r ) const;
16 virtual int operator==( const string &r ) const;
17
18 virtual void print( ostream &output ) const;
19 // ...
20 };
21 //-----------------------------------------------------
22 inline string::string( const char *input_str /*= ""*/ )
23 {
24 length = strlen(input_str) + 1;
25 buf = new char[ length ];
26 strcpy( buf, input_str );
27 }
28 //-----------------------------------------------------
29 inline string::string( const string &r )
30 {
31 length = r.length;
32 buf = new char[ length ];
33 strcpy( buf, r.buf );
34 }
35 //-----------------------------------------------------
36 /* */ string:: ~string( void )
156 ++
37 {
38 delete buf;
39 }
40 //-----------------------------------------------------
41 /* */ const string &string::operator=( const string &r)
42 {
43 if( this != &r )
44 {
45 if( length != r.length )
46 {
47 free( buf );
48 length = r.length;
49 buf = new char[ length ];
50 }
51 strcpy( buf, r.buf );
52 }
53 return *this;
54 }
55
56 //-----------------------------------------------------
57 /* */ int string::operator< ( const string &r ) const
58 {
59 return strcmp(buf, r.buf) < 0;
60 }
61 //-----------------------------------------------------
62 /* */ int string::operator> ( const string &r ) const
63 {
64 return strcmp(buf, r.buf) > 0;
65 }
66 //-----------------------------------------------------
67 /* */ int string::operator==( const string &r ) const
68 {
69 return strcmp(buf, r.buf) == 0;
70 }
71 //-----------------------------------------------------
72 /* */ void string::print( ostream &output ) const
73 {
74 cout << buf;
75 }
76 //----------------------------------------------------
77 inline ostream &operator<<( ostream &output, const string &s )
78 {
79 // - string,
80 // ,
81 // .
82
83 s.print(output);
84 return output;
85 }
,
7:
string::operator const char*() { return buf; }
157
, :
void f( void )
{
string s;
// ...
printf("%s\n", (const char*)s );
}
c operator char*(),
Unicode, 16-.
operator wchar_t*(),
f():
printf("%s/n", (const wchar_t*)s );
, ,
- ,
, char*
.
.
buf, operator const char*(),
length,
:
string s;
// ...
char *p = (char *)(const char *)s;
gets( p );
,
:
const char *g( void )
{
string s;
// ...
return (const char *)s;
}
- .
7 ,
char* (print()).
:
string s;
s.print( cout )
:
cout << s;
printf().
.
, , ,
.
,
print(). ,
Unicode char*
.
translate_to_French()
. -
, ,
.
.
110.1. get/set
( )
, "
". ,
++.
:
struct xxx
{
int x;
};
:
class xxx {
private:
int x;
public:
void setx ( int ix ){ x = ix; }
int getx ( void ) { return x; }
159
, .
: .
:
. (public)
.
; ,
. .
, ,
,
, .
get/set. ,
. ,
,
. ,
; ,
.
, .
, , ++
/++,
, .
, ++ ,
.
. -
, ""
, - ,
. ""
,
"", get_date().
, .
, -
.
"". ""
""
"_" ( ),
"" "".
160 ++
""
"". ""
,
"____"
"___", ""
.
""; , "",
"".
"" .
"" , . , ""
, ,
.
111. ,
++
, ,
,
++.
: -
,
.
++ char*.
char*. ,
char* , . .
. , ,
, ,
.
char* -
, string;
string. : -
, .
,
- .
char* string ,
, char*, ,
string, .
161
,
. string
char*,
, ,
char*,
, ++. ,
string, char*.
- Windows
, .
( X-Window, " " Windows
widget). -
, , , ,
, window
string.
112.
,
.
- .
, window
string, .
, string .
, :
string str = "xxx"; // "xxx"
str = ""; // ""
str += ""; // "" .
,
, -,
:
class edit_control : public string
, public window
{/* ... */}
edit_control ,
string, ,
(, ) string,
-.
162 ++
, , operator=()
operator+=(), string , ,
edit_control. , operator=()
string 7 155 ,
:
class edit_control : public string
, public window
{
// ...
virtual string &operator=( const string &r );
}
string,
edit_control; ,
:
f( string *s )
{
// ...
*s = " " ;
}
string .
edit_control ,
.
112.1. -
-
,
, - ,
,
. , ,
.
, - ,
,
( ,
163
).
"" "",
- "" ""
,
.
,
, ,
.
; .
"/"
MacApp MFC. , ""
, "" .
, -
"". ""
"" . ,
"", .
"" ""
" ".9
113.
const .
,
. ++ ,
, ++ .
const , ;
, ,
. :
,
. :
puts( const char *p )
, puts()
, p.
.
9
MFC
"Rewriting the MFC Scribble Program Using an Object-Oriented
Design Approach" "Microsoft Systems Journal" 1995
.
164 ++
, ,
const :
class cls
{
public: int operator==( const cls &p ) const ;
};
( const ,
). const ,
, , . ,
const
this:
const current_class *this;
-
, const,
"
const current_class
current_class".
this,
(
).
.
114. ,
-
" ,
". ,
- .
.
, ,
" ".
, ,
:
typedef struct tagSIZE //
//
{
LONG cx;
LONG cy;
}
SIZE;
165
, ,
cx cy
,
. :
CSize some_size;
some_size.cy; // !
:
some_size.height();
, .
, , ++
. ,
,
,
,
. , .
, , ,
. ,
- , ,
.
115.
.
:
class amanda
{
public:
void peekaboo( void ){ cout << "-\n"; } //
//
//
}
++ . ,
166 ++
, .
, ,
:
class amanda
{
public:
void peekaboo( void );
}
, .
. ,
,
.
, , .
,
, - .
,
-,
.
, .
8 (
). linked_list list_node
.
, .
( , class xxx;
). 8
,
, .
, ,
. 9.
167
8.
1 class list_node;
2
3 class linked_list
4 {
5 int number_of_elements_in_list;
6 list_node *root;
7
8 private: // ,
9 friend class list_node; // list_node
10 void have_removed_an_element(void)
11 {
12 --number_of_elements_in_list;
13 }
14
15 public:
16 void remove_this_node( list_node *p )
17 {
18 // ,
19 // , list_node
20 // remove_yourself_from_me( &root ).
21
22 p->remove_yourself_from_me( &root );
23 }
24
25 // ...
26 };
27
28 class list_node
29 {
30 linked_list *owner;
31 private: //
32 friend class linked_list; // ,
33 // linked_list
34 void remove_yourself_from_me( list_node *root )
35 {
36 // ...
37 owner->have_removed_an_element();
38 }
39 };
9.
1 class list_node;
2
3 class linked_list
4 {
5 int number_of_elements_in_list;
6 list_node *root;
7
8 private:
168 ++
116. ,
.
, ++,
.
. , ,
.
,
. -, :
f( int, long );
f( long, int );
f( 10, 10 ); // : ?
:
f( int );
f( void* );
f( 0 ); // :
++, , 0
, int. :
const void *NULL = 0;
const int ZERO = 0;
f(NULL)
f(ZERO) ,
.
.
,
( ),
. , :
f( int x = 0 );
f() ,
0. , , ,
, .
; ,
. ,
170 ++
, :
class string
{
public:
string( char *s = "" );
string( const string &r );
string( const CString &r ); // MFC.
// ...
};
:
, - .
, print().
, , : -
. :
class string
{
// ...
public:
print( FILE *fp );
print( iostream &ios );
print( window &win );
:
class string
{
// ...
public:
print_file ( FILE *fp );
print_stream ( iostream &ios );
print_window ( window &win );
, device,
: FILE, iostream
window, ,
print(),
device.
, , ,
, , , .
171
8.
.
++
: " ".
++. ,
-
.
++ , .
117.
.
,
friend. , - ,
,
, ,
.
;
, .
-, ,
. , ,
list_node
list, ,
, ,
list_node. list
list_node,
list_node
list. : "-
-
, - ".
, ++
:
.
, , ,
. ,
,
.
172 ++
:
class granting
{
// ...
// , ,
// grantee,
// .
message_sent _from_grantee();
another_message_sent_from_grantee();
private:
// .
// grantee ,
// .
// ...
};
, ;
,
. , - grantee,
,
.
118.
, , ,
. ,
( ,
). ,
. ,
,
,
.
173
119. :
++
,
,
. x.f() y.f(),
x y . , x::f()
y::f(). ::
,
, - ( . ->) .
:
class tree
{
enum { max_nodes = 128 };
public:
enum traversal_mechanism { inorder, preorder, postorder };
// ...
f()
{
tree t;
// ...
t.print( tree::postorder );
}
tree::postorder, print(),
,
tree::. ,
postorder,
other_class::postorder. ,
max_nodes ,
- tree,
.
-
,
. - -
, .
174 ++
case switch;
.
- . -,
, int. -,
.
++:
const int default_size = get_default_size_from_ini_file();
,
.
- ,
,
-. const
, -
:
class fixed_size_window
{
const size height;
const size width;
.
"" ,
. , 10
int_array ,
.
, /++
(a[row][col]). int_array ,
, int_array
.
.
: a[row][col]
(a[row])[col]. a[row] int_array::operator[](),
int_array::row,
. .
175
. [col] int_array::row,
int_array::row::operator[]().
operator[]() . ,
int_array::row ,
,
row.
int_array , int_array .
10.
1 #include <iostream.h>
2
3 class int_array
4 {
5 class row
6 {
7 friend class int_array;
8 int *first_cell_in_row;
9
10 row( int *p ) : first_cell_in_row(p) {}
11 public:
12 int &operator[] ( int index );
13 };
14
15 int nrows;
16 int ncols;
17 int *the_array;
18
19 public:
20 virtual
21 ~int_array( void );
22 int_array( int rows, int cols );
23
24 row operator[] (int index);
25 };
26 //========================================================
27 // - int_array
28 //========================================================
29 int_array::int_array( int rows, int cols )
30 : nrows ( rows )
31 , ncols ( cols )
32 , the_array ( new int[rows * cols])
33 {}
34 //--------------------------------------------------------
35 int_array::~int_array( void )
36 {
37 delete [] the_array;
38 }
39 //--------------------------------------------------------
40 inline int_array::row int_array::operator[]( int index )
41 {
176 ++
8.
120.
121.
,
:
.
.
:
some_class *operator+( some_class *left, some_class *right );
:
some_class x, y;
x = *(&x + &y)
:
x = x + 1;
, .
, double,
double. , double 8-
: ,
.
.
-
,
,
.
, .
.
:
178 ++
, copy_word() .
target .
:
f( const char *p )
{
char *p = new char[1024];
load( p );
char word[64];
copy_word( word, p );
delete( p ); // ! p ,
} // !
, , copy_word(
word, p ), p
. ,
(, , 6-
).
.
- ,
.
copy_word() ,
:
copy_word( word, &p );
& .
,
,
.
,
; ,
.
.
179
, .
:
,
. ,
.
:
f( const some_class &obj );
:
some_class an_object;
f( an_object );
, ,
const
an_object f().
.
: , ,
.
,
, . ,
, ,
:
- ( ,
int);
- .
,
, -
.
, . CDocument
CView.
:
CDocument *doc;
CView *view;
. -, GetNextView()
.
GetCurrentViewAndAdvancePosition(),
180 ++
( )
. :
,
. , .
GetNextView(pos), ,
. ,
, pos
,
.
.
GetNextView() :
POSITION pos = doc->GetFirstViewPosition();
while( p = GetNextView( &pos ) )
p->Invalidate();
&pos , pos ;
? ,
. :
for( CView *p = doc->GetFirstView(); p ; p = p->NextView() )
p->Invalidate();
:
POSITION pos = doc->GetFirstViewPosition();
for( ; pos ; pos = doc->GetNextView(pos) )
(pos->current())->Invalidate();
:
CPosition pos = doc->GetFirstViewPosition();
for( ; pos; pos.Advance() )
( pos->CurrentView() )->Invalidate();
:
ViewListIterator cur_view = doc->View_list(); //
//
//
//
// .
for( ; cur_view ; ++cur_view ) // ++
// .
cur_view->Invalidate(); // -> View*.
, .
181
, " ".
122. ( )
,
. ,
. :
some_class &f()
{
some_class x;
// ...
return x;
}
++.
return
. ,
return x, ,
, .
123. ,
new
new delete
malloc() free(). ,
:
const some_class &some_class::operator+( const some_class &r )
const
{
some_class *p = new some_class;
// ...
return *p;
}
, ,
. :
some_class a, b, c;
c = a + b;
a + b , .
, delete,
:
182 ++
some_class *p;
c = *(p = &(a + b));
. operator+()
. :
const some_class *some_class::operator+( const some_class &r )
const
{
some_class *p = new some_class;
// ...
return p;
}
:
c = *(p = a + b);
, ,
. ,
:
const some_class some_class::operator+( const some_class &r )
const
{
some_class obj;
// ...
return obj;
}
return,
.
, operator=( ) 183
8. ,
operator=( )
, operator=()
, ,
. (
)
(
).
(
) ,
. -, ++,
, .
, , ,
, .
:
some_class x; //
some_class y = x; //
.
:
some_class x;
f( some_class x ) // , .
f( x ); // x
// . .
:
some_class g() // , x - ,
// .
// .
{
some_class x; // return
return x; // x -
} // ( ).
// .
- operator=()
,
.
184 ++
124. operator=( )
125.
operator=( ) :
class class_name
{
const class_name &operator=( const class_name &r );
};
, ,
, ;
,
.
, .
& ,
,
, .
, (*this),
.
operator=(),
, .
; .
, .
, operator=()
, , -
,
.
:
(x =y) = z;
, (x=y)
operator=(), .. .
=z , x=y.
, operator=( ) 185
, operator=()
, const:
//
// operator=().
// |
// V
const class_name &operator=( const class_name &r ) const;
"
",
(x=y)=z.
:
if( this != &r )
operator=(). :
class_name x;
// ...
x = x;
, this
. ,
,
. ,
,
:
class_name array[10];
class_name *p = array;
// ...
*p = array[0];
126. , -,
operator=()
operator=(), .
"" ,
,
this->field = src.field . ,
operator=() .
, . ,
string, char*, , ,
186 ++
. , string
7 155
operator=().
string s1 = "", s2;
// ...
s2 = s1;
s2 s1.
, s1->buf,
, . , s1,
s2 , .
, ,
buf , , , ,
, .
operator=(), 7 155.
,
-.
: " "
"" , ,
memcpy()
, .
operator=()
,
operator=()
.
, ,
++.
127. ,
128.
129. ,
++
, , ,
. , ,
, . , ,
( 7 155):
, operator=( ) 187
class base
{
string s;
public:
base( const char *init_value );
}
//------------------------------
base::base( const char *init_value )
{
s = init_value;
}
: ,
. s base,
++ ,
.
. ,
, , ,
string, ,
. ,
s ,
new \0.
string::operator=().
,
init_value.
.
. :
base( const char *init_value ) : s(init_value)
{}
s ,
operator=() .
,
,
.
,
.
- :
.
.
.
. ,
. ,
188 ++
, . ,
.
, ,
. -
:
class wilma
{
int y;
int x;
public:
wilma( int ix );
};
.c:
wilma::wilma( int ix ) : y(ix * 10), x(y + 1)
{}
, -
, x y.
: x ,
,
y+1, y .
,
:
wilma::wilma( int ix ) : y(ix * 10), x((ix *10) + 1)
{}
130.
.
,
.
,
.
,
base(void),
s string::string(void):
class base
{
public:
base( void ); //
base( const base &r ); //
, operator=( ) 189
class derived
{
string s; //
public:
derived( const derived &r )
};
string
, :
derived::derived( const derived &r ) : base(r), s(r.s) {}
131.
operator=( )
.
10 ++ :
" operator=() ".
, ,
operator=(),
.
operator=(),
, ,
.
,
.
,
operator=()
operator=() (
10
"The Annotated C++ Reference Manual" (Reading:
Addison Wesley, 1990),
ISO/ANSI ++.
. "
++ " (.: , 1992).
..
190 ++
).11 , , ,
. ,
:
class base
{
public:
base( const base &r );
const base &operator=( const base &r );
};
derived x;
derived y = x; //
// .
//
// s.
x = y; // operator=()
// .
// operator=() s.
,
. ,
" " .
.
operator=() ,
( )
. ,
,
memcpy() .
++ ISO/ANSI
, ,
,
. , , ,
operator=(),
11
, operator=(), (
),
.
, operator=( ) 191
.
:
class derived : public base
{
string s;
public:
derived( const derived &r );
const derived &operator=( const derived &r );
};
//-----------------------------------------------------------
derived::derived( const derived &r ) : base(r), s(r.s)
{}
//-----------------------------------------------------------
const derived &derived::operator=( const derived &r )
{
(* (base*)this) = r;
s = r.s;
}
.
operator=()
:
(* (base*)this) = r;
this ;
(base*)this. (* (base*)this)
, (* (base*)this) = r
, operator=()
.
:
base::operator=( r );
, ,
operator=().
, operator=(), . (
,
).
192 ++
132. ,
,
++ . ,
char* 9- 7 155
:
char *pchar = "";
(string) pchar;
, ,
. ,
. ,
char*
string f():
f( const string &s );
// ...
f( "" );
,
.
,
, :
class array
{
// ...
public:
array( int initial_size );
};
, :
f( const array &a );
// ...
f( isupper(*str) );
( f() , *str
, , *str
).
,
:
explicit. .
, operator=( ) 193
class array
{
// ...
public:
enum bogus { set_size_to };
array( bogus, int initial_size );
};
, . ,
bogus ,
.
133.
. ++ ,
:
class window
{
static int num_windows;
public:
window();
~window();
};
int window::num_windows = 0;
window::window()
{
if( ++num_windows == 1 ) //
initialize_video_system();
}
window::~window()
{
if( --num_windows == 0 ) //
shut_down_video_system(); //
}
,
:
194 ++
f()
{
static int have_been_called = 0;
if( !have_been_called )
{
have_been_called = 1;
do_one_time_initializations();
}
}
134.
135. ++
,
.
,
, ,
MFC Microsoft:
f( CWnd *win ) // CWnd -
{
// ""
// ( )
char buf[80]; /* = */
win->GetWindowText(buf, sizeof(buf));
// ...
}
, " "
, , ,
.
,
CWnd ( ).
"" , ,
:
CString caption = win->caption();
:
win->caption() = " ";
.
, MFC -
.. , ,
, operator=( ) 195
.
Microsoft
(API
Windows )
++,
. API
GetWindowText(),
- CWnd.
:
CString str;
win->GetWindowText( str );
: -
, .
, ,
, - .
. ++
-.
, ,
++,
( ).
CWnd 11.
win->text() = "
" (window::caption).
text() ,
.
11 ,
MFC , CWnd, ,
window,
CWnd. ++ ,
.
12 ++ MFC.
window::caption ,
, .
:
f(CWnd *win)
{
caption cap( win )
, caption
, .
,
. , 12
.
11. CWnd:
1 class window : public CWnd
2 {
3 public:
4 class caption
5 {
6 CWnd *target_window;
7
8 private: friend class window;
9 caption( CWnd *p ) : target_window(p) {}
10
11 public:
12 operator CString ( void ) const;
13 const caption &operator=( const CString &s );
14 };
15
16 caption text( void );
17 };
18 //-------------------------------------------------------
19 caption window::text( void )
20 {
21 return caption( this );
22 }
23 //--------------------------------------------------------
24 window::caption::operator CString( void ) const
25 {
26 CString output;
27 target_window->GetWindowText( output );
28 return output; //
29 }
30 //--------------------------------------------------------
31 const caption &window::caption::operator=(const CString &s)
32 {
33 target_window->SetWindowText( s );
34 return *this;
35 }
, operator=( ) 197
12.
1 class caption
2 {
3 CWnd target_window;
4 public:
5 window_text( CWnd *win ) : target_window( win ) {};
6
7 operator const CString( void );
8 const CString &operator=( const CString &r );
9 };
10
11 inline caption::operator CString( void );
12 {
13 CString output;
14 target_window->GetWindowText( output );
15 return output;
16 }
17
18 inline const CString &caption::operator=(const CString &s )
19 {
20 // CString (
21 // "caption"), a = b = ""
22
23 target_window->SetWindowText( s );
24 return s;
26 }
198 ++
8.
, (
- ,
, - ,
, ,
).
- ,
,
,
. , ,
,
. ,
,
data_structure,
tree linked_list (,
data_structure). -
, ++,
, ,
.
136. ,
. -,
,
,
. , , -
shape .
print() ,
(
circle, line, polygon ..).
print()
.
.
storable.
.
199
:
add( storable *insert )
{
storable *object_already_in_database;
// ...
if( object_already_in_database->cmp(insert) < 0 )
//
}
storable cmp(),
, (),
, storable.
storable
. ,
(public).
137. ,
, ,
.
. , -,
;
.
,
.
, ,
.
. ,
:
class storable
{
int stuff;
public:
storable( void );
( )
:
int _storable__print ( storable *this ) { /* ... */ }
int _storable__virtf ( storable *this ) { /* ... */ }
int _storable__nonvirtual ( storable *this ) { /* ... */ }
_vtab _storable__vtab
{
_storable__print,
_storable__virtf,
NULL // -
};
_storable__ctor( void ) //
{
_vtable = _storable__vtable; //
// .
stuff = 0; // .
}
, , :
storable *p;
p->nonvirtual();
:
_storable__nonvirtual( p )
, :
p->print();
:
( p->_vtable[0] )( p );
- ,
.
, ,
201
. ,
:
class employee : public storable
{
int derived_stuff;
// ...
public:
virtual int cmp( const storable &r );
};
:
int _employee__print( employee *this ) { /* ... */ }
int _employee__cmp ( employee *this, const storable *ref_r )
{ /* ... */ }
_vtab _employee_vtable =
{
_employee__print,
_storable_virtf, // ,
//
// .
_employee_cmp
};
,
. (virtf),
,
.
:
202 ++
:
storable *p;
p = (storable *)malloc( sizeof(employee) );
_employee_ctor( p );
_employee_ctor()
_sortable_ctor(),
.
_employee_ctor()
,
.
, , p employee, p->print()
, :
( p->_vtable[0] )( p );
, p ,
print() (
_vtable
). ,
print() ,
.
, ,
, ,
. , -
, . _vtable
,
, . print
:
( this->_vtable[0] )( p );
_vtable _vtable[0]
.
print()
, _vtable
, print().
,
. , , _vtable
.
.
203
(
, ).
138.
.
"" ( =0 )
,
NULL . (
"" ,
).
,
NULL.
UNIX " "
Windows, MS-DOS , ,
0, .
139.
:
class base
{
char *p;
:
base *p = new derived;
// ...
delete p;
, , p
. , p
base. , delete p
:
204 ++
_base__destructor(p);
free(p);
.
, :
virtual ~base() { /* ... */ }
, .
, delete p :
( p->_vtable[DESTRUCTOR_SLOT] ) (p);
p ,
,
.
140. , ,
,
, (public)
.
, ,
-
, .
,
. ,
,
.
,
,
(
).
,
, .
205
141. ,
,
, -
" ". . ,
,
;
.
142.
,
++ -
.
( )
( ),
. (,
).
,
.
. ,
, . , CDialog MFC
Windows ( ).
OnOk(),
, "OK".
CDialog OnOk(),
,
.
:
class mydialog : public CDialog
{
// ...
private:
virtual OnOk( void );
};
206 ++
/* */ mydialog::OnOk( void )
{
if( data_is_valid() )
CDialog::OnOk(); //
else
beep(); //
// Windows
}
OnOk() ,
OnOk() mydialog. OnOk()
,
. ,
CDialog::OnOk() ,
OnOk() CDialog.
.
OnOk(), .
, ,
.
,
.
, .
,
.
143. (
++)
, ++
. ++
.
,
. :
class base
{
public:
virtual int operator==( const base &r ) = 0;
};
class derived
{
207
char *key;
public:
virtual int operator==( const base &r )
{
return strcmp(key, ((const derived &)r).key ) == 0;
}
};
, , r
.
- ,
: base.
, r ,
base, derived.
:
class other_derived : public base
{
int key;
// ...
};
f()
{
derived dobj;
other_derived other;
dynamic_cast<t> 0,
t,
208 ++
.
,
,
object.
object , object
. ,
object ,
.
,
.
,
.
144.
operator=( )
,
.
operator=() :
class some_class
{
public:
virtual
~some_class( void );
some_class( void );
some_class( const some_class &r );
new this
some_class, - r
.12
12
, , , :
209
. -,
. :
class derived : public some_class
{
public:
~derived();
// ,
// operator=() operator=()
// .
}
, ()
, :
this->~some_class()
,
, .
, :
this->some_class::~some_class();
some_class::
. ,
.
.
:
derived d1, d2;
d1 = d2;
operator=() ( ,
)
operator=() ,
new() .
, , ,
. ,
, .
vtable
. vtable
;
new
operator=().
, operator=()
, .
,
, ,
, .
operator=()
:
class some_class
{
void create ( void );
void create ( const some_class &r );
void destroy ( void );
public:
virtual
~some_class( void ) { destroy(); }
some_class( void ) { create(); }
8.
145. ( )
, ,
. . , +
"",
operator+() - . (
a + b b a, ),
.
, ,
, "" ,
+ .
,
string,
<, == .. .
, * -
.
, ,
++ iostream. (<<)
"" .
printf(), shiftf(). , ,
/
UNIX,
. , ++
UNIX,
, Microsoft Windows.
, , , >
, >>
. , , > >> ,
.
, .
cout << x += 1 , ,
<< , +=,
(cout << x) += 1, . ++
, iostream,
"" "",
.
212 ++
" "
.
+ OR, *
AND,
. ,
operator+() OR ++. ,
<< "" ++; "".
"", | &
"" "". ,
,
, ++,
++ ( ).
AND;
. a.Union(b)
a.intersect(b). ( a.union(b)
u, union ).
146.
,
( )
, , ,
.
.
, ,
.
,
, . ,
. operator==() operator!=()
.
( )
"".
, ,
. ,
, , :
string array[ size ];
string *p = array;
++ (keys ,
;
):
tree<string> keys; // ,
//
iterator p = keys;
// ...
for( int i = keys.size(); --i >= 0 ; )
visit( *p++ ); // visit() .
, ,
,
. iterator(p) ,
" " .
147. ,
. ,
, " ",
.
* ++,
, .
4 (t , ti
). *++p *p++
..
tree operator[] () operator*() ,
.
?
4.
ti = t;
--ti;
ti += i; i
ti -= i; i
ti + i;
ti - i; ti
ti[i]; i
ti[-i]; -i
t2 = ti;
214 ++
t2 - ti; ,
ti->msg();
(*ti).msg();
operator==()
operator!=(),
, . ,
== ,
" ",
" "?
< <= ,
. ,
- .
148.
,
lvalue
rvalue. lvalue ++:
. , ,
,
.
,
. "rvalue"
, .
"lvalue" . (, "l" "r" ,
l=r = lvalue.
rvalue).
,
-
, . ,
:
(=, +=, -= ..)
(++, --) lvalue
, . ++ +=1,
, ,
.
- this
lvalue, .
215
(
) .
lvalue,
rvalue.
const . (
,
).
() rvalue
,
. ,
,
- , .
, rvalue,
lvalue.
lvalue.
*, -> [] lvalue,
,
. y , x->y
lvalue, . y
, x->y rvalue,
.
++ * []
. operator-> .
,
. ->
.
-, , .
lvalue rvalue. .().
rvalue.
,
.
149.
(inline)
,
. ,
216 ++
string 7 155:
class string
{
enum special_ { special };
string( special_ ) {}; // .
// ...
public:
const string operator+( const string &r ) const;
// ...
};
//------------------------------------------------------------
const string::operator+( const string &r ) const
{
string tmp( special ); //
217
:
string s1, s2;
s1 + s2;
( ,
buf ):
string(s1.buf, s2.buf)
return .
150.
151. ,
++
. ,
. :
class riches //
{
public:
riches( const rags &r );
};
class rags //
{
public:
operator riches( void );
};
,
rags riches.
" " ( ),
, rags
riches riches,
rags; ,
:
rags horatio_alger; //
. ,
218 ++
:
class some_class
{
public:
operator int (void);
operator const char * (void);
};
, :
some_class x;
cout << x;
. , stream
:
ostream &ostream::operator<<( int x );
ostream &ostream::operator<<( const char *s );
, ,
.
.
, double,
int, long ,
.
219
8.
152. new/delete malloc()/free()
, new() malloc()
.
. ,
free() ,
new ( ).
, ++ new
delete. , ,
strdup() , malloc().
153. , ,
, ,
.
: "
". -
, new, ,
delete. :
.
.cpp, .
, .
220 ++
8.
,
,
, ,
. ,
.
155.
:
#define SQUARE(x) ((x) * (x))
:
SQUARE(++x)
:
((++x)*(++x))
x . ,
++ .
, :
inline int square( int x ){ return x * x; }
. ,
. ,
,
:
template <class type>
inline type square( type x ){ return x * x; }
, .
max(10, 10L),
:
template <class type>
inline type max( type x, type y ){ return (x > y) ? x : y; }
max(10, 10L) ,
max(),
:
221
.
int long,
.
,
, square .
, , ,
.
156.
, 13.
(, array,
int_array).
operator[](). ,
, ,
, .
:
int_array::operator[]() int,
long_array::operator[]() long,
.
,
.
.
13.
1 template <class type, int size >
2 class array
3 {
4 type array[size];
5 public:
6 class out_of_bounds {}; // ,
7 //
8 type &operator[](int index);
9 };
10
11 template <class type, int size >
12 inline type &array<type, size>::operator[](int index)
13 {
14 if( 0 <= index && index < size )
15 return array[ index ]
222 ++
16 throw out_of_bounds;
17 }
, - . ,
.
, ,
-. ,
,
,
operator[](), :
array<int,10> ten_element_array;
array<int,11> eleven_element_array;
array<int,12> twelve_element_array;
array<int,13> thirteen_element_array;
( array<int,10>::operator[](), array<int,11>::operator
[]() ).
, .
, , 14?
:
array<int> ten_element_array (10);
array<int> eleven_element_array (11);
array<int> twelve_element_array (12);
array<int> thirteen_element_array (13);
(
operator[]()) .
14. ( )
1 template <class type>
2 class array
3 {
4 type *array;
5 int size;
6 public:
7 virtual ~array( void );
8 array( int size = 128 );
9
10 class out_of_bounds {};// ,
11 //
12 type &operator[](int index);
13 };
,
( , ). .
223
14
15 template <class type>
16 array<type>::array( int sz /*= 128*/ ): size(sz)
17 , array( new type[ sz ] )
18 {}
19
20 template <class type>
21 array<type>::~array( void )
22 {
23 delete [] array;
24 }
25
26 template <class type>
27 inline type &array<type>::operator[](int index)
28 {
29 if( 0 <= index && index < size )
30 return array[ index ]
31 throw out_of_bounds;
32 }
,
. 13
:
array< array<int, 10>, 20> ar;
(20- 10- ).
14 , ,
, , :
array< array<int> > ar2(20);
array<int>
, 128- ; 20-
128- .
. :
template< class type, int size >
class sized_array : public array<type>
{
public:
sized_array() : array<type>(size) {}
};
, ,
.
, ,
. :
sized_array< sized_array<int,10>, 20> ar3;
224 ++
, 20- 10-
.
157.
158. ;
, , ,
. ,
,
.
-, , .
storable, ,
. collection
:
class collection
{
storable *head;
public:
// ...
storable *find( const storable &a_match_of_this ) const;
};
storable.
,
storable,
collection.
storable,
:
class storable
{
storable *next, *prev;
225
public:
storable *find ( const storable &match_of_this ) const;
storable *successor ( void ) const;
virtual int operator== ( const storable &r ) const;
};
operator==() ,
storable.
13 :
class storable_string : public storable
{
string s;
public:
virtual int operator==( const storable &r ) const;
// ...
};
ISO/ANSI C++
. right
NULL, (r)
storable_string. ,
13
string. .
226 ++
, storable.
. , .
-, , , : ",
,
", , , :
template <class t_key>
class storable
{
storable *next, *prev;
t_key key;
public:
// ...
storable *find ( const storable &match_me ) const;
storable *successor ( void ) const;
int operator==( const storable &r ) const;
};
. -
.
storable, -
. , , storable
.
, , .
. 156. .
227
,
. ,
successor(),
find(), ,
, .
,
. ,
, :
template <class t_key>
class storable_tem : public storable
{
t_key key;
public:
//
virtual int operator==( const storable &r ) const;
// ...
};
, ,
key.
,
, key.
.
.
228 ++
8.
159.
, , :
(, ,
..).
(, ).
,
(, printf).
,
, , ,
.
:
some_obj x;
if( x.is_invalid() )
// .
, , .
. ,
x = a + b;
operator+() ,
, x.
:
if( x == INVALID )
// ...
. .
, .
, exit(),
malloc() . :
if( !(p = malloc(size)) )
fatal_error( E_NO_MEMORY );
, new ,
. new (
229
exit()),
, -
.
. ,
ISO/ANSI ++ , new ,
, , -
, -
,
, malloc()
NULL. , , ,
, new,
.
printf().
, printf() . (
,
0, ). ,
, .
,
, , ,
.
, ?
. . ,
:
some_class obj;
try
{
obj.f();
}
catch( some_class::error &r )
{
//
}
, :
if( obj.f() == ERROR )
//
, try- ,
,
, .
. CFile,
/ ,
230 ++
,
. , write()
.
. ,
:
char data[128];
Cfile f( "some_file", CFile::modeWrite );
try
{
f.Write( data, sizeof(data) );
}
catch( CFileException &r )
{
if( r.m_cause == CfileException::diskFull )
// -
}
. .
:
bytes_written = f.Write( data, sizeof(data));
if( bytes_written != sizeof(data) )
//
.
. -, ,
, . Write()
,
, -
. ,
, , ,
.
Write() ,
. ,
CFile , , :
char data[128];
CFile f( "some_file", CFile::modeWrite );
int bytes_written;
try
{
bytes_written = f.Write( data, sizeof(data) );
}
catch( CFileException &r )
{
231
// bytes_written .
}
- Write()
catch ,
return Write(),
; bytes_written
. , Write()
,
, ,
.
,
- .
.
,
.
Microsoft Windows NT,
.
10-20%
.14
, ,
; ,
, .
160. error
15
.
:
try
{
file f("name", "rw");
buffer b;
b = f.read();
f.write( b );
}
catch( file::open_error &r )
{
14
32- Visual C++ Microsoft;
, .
232 ++
// .
}
catch( file::io_error &r )
{
// - /.
}
, ,
, , :
file f;
try
{
buffer b;
b = f.read()
f.write( b );
}
catch( file::error &r )
{
// ...
}
15.
1 class file
2 {
3 public:
4 class error {};
5 class open_error : public error {};
6 class io_error : public error {};
7
8 // ...
9 }
, file::read_error
file::error ( ).
,
.
,
:
class long_double
{
public:
class error {};
class didvide_by_zero : public error {};
// ...
};
error ,
file::error long_double::error,
233
.
error
. (
,
). , , ,
some_class, :
catch(some_class::error &r)
. ,
error :
class employee
{
public:
class error {};
class database_access_error : public error {};
};
aagh
peon::aagh, peon::error employee::error.
error,
error,
catch().
161.
, ,
ISO/ANSI ++,
. ,
( Microsoft)
.
++.
,
. ""
.
,
. :
class c
234 ++
{
class error {};
int *pi;
public:
c() { throw error(); }
// ...
};
void f( void )
{
try
{
c *cp = new c; // cp ,
//
// ...
delete cp; // .
}
catch( c::error &err )
{
printf (" \n");
delete cp; // : cp
}
}
, , new,
. , ,
, error.
catch-. ,
new cp,
.
, ,
.
ISO/ANSI ++ ,
. ,
.
(
,
):
class
{
int *pi;
public:
c() { /*...*/ throw this; }
};
void f( void )
235
{
try
{
c *cp = NULL;
cp = new c;
c a_c_object();
}
catch( c *points_at_unconstructed_object )
{
if( !cp ) // ,
// new,
delete points_at_unconstructed_object;
}
}
,
new, .
- , , :
void f( void )
{
c *cp = NULL; // cp try-,
// try-
// , cp
// catch-
// try-.
try
{
c a_c_object;
cp = new c;
}
catch( c *points_at_unconstructed_object )
{
if( !cp ) // ,
// new,
delete points_at_unconstructed_object;
}
}
,
,
new, .
,
. (
delete, ,
- ).
, delete
236 ++
. .
, .
c
, new int[128] new
:
c::c( )
{
if( some_error() )
throw error(this); // : pi .
// ...
pi = new int[128]; // : pi ,
// new
// .
// ...
if( some_other_error() )
{
delete [] pi; // .
throw error(this); //
}
}
c::~c( )
{
delete pi;
}
, pi
new. new new
, pi .
(, NULL,
). , delete
. ,
, -
:
c::c( ) : pi(NULL) // ,
// new
{
if( some_error() )
throw error(this); // .
// ...
pi = new int[128]; // new .
// ...
if( some_other_error() )
{
delete [] pi; //
// .
237
throw error(this); // .
}
}
c::~c( )
{
if( pi )
delete pi;
}
, ,
, ,
.
error
.
. 16 ,
try-, catch-. ,
new, ,
delete,
destroy(), catch. ,
has_been_destroyed ,
new, ,
new,
.
, ,
,
. . (
Microsoft Visual C++ 2.2)
catch, , try-
, catch-. , 16
. ,
operator new(),
, .
238 ++
16. except.cpp
1 class
2 {
3 public:
4 class error
5 {
6 c *p; // NULL
7 public:
8 error( c *p_this );
9 void destroy( void );
10 };
11
12 private:
13
14 unsigned has_been_destroyed : 1;
15 int *pi;
16
17 private: friend class error;
18 int been_destroyed( void );
19
20 public:
21 c() ;
22 ~c();
23
24 };
25 //========================================================
26 c::error::error( c *p_this ) : p( p_this ) {}
27 //--------------------------------------------------------
28 void c::error::destroy( void )
29 {
30 if( p && !p->been_destroyed() )
31 delete p;
32 }
33 //========================================================
34 c::c() : has_been_destroyed( 0 )
35 {
36 // ...
37 throw error(this);
38 // ...
39 }
40 //--------------------------------------------------------
41 c::~c()
42 {
43 // ...
44 has_beeb_destroyed = 1;
45 }
46 //--------------------------------------------------------
47 int c::been_destroyed( void )
48 {
49 return has_been_destroyed;
50 }
239
51 //========================================================
52 void main( void )
53 {
54 try
55 {
56 c *cp = new c;
57 c a_c_object;
58
59 delete cp;
60 }
61 catch( c::error &err )
62 {
63 err.destroy(); // ,
64 } // new
65 }
240
-. , ,
, . ,
. , .
, - ++
; ,
,
, .
.
++ , ?
, , , .
_
.
_().
241
, ,
++, -
Microsoft.
,
-.
- , ++
Microsoft Windows, Windows-95, Windows NT
UNIX.
- ,
"Microsoft Systems Journal", "Windows Tech Journal"
"BYTE". " ",
"Dr.Dobb's Journal" 1983 1987 ,
. "Compiler Design in C",
"C+C++" "The C Companion". -
.
allen@holub.com
Software Engineering Consultants, P.O.Box 5679,
Berkeley, CA 94705 ( : (510) 540-7954).