Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
C++-tips-n-traps4

C++-tips-n-traps4

Ratings: (0)|Views: 30|Likes:
Published by toanthang87

More info:

Published by: toanthang87 on Nov 03, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/09/2014

pdf

text

original

 
TheC++ProgramminLanguage C++TipsandTrapOutlin
TipsforCProgrammerC++TrapsandPitfallEciencyandPerformanc
TipsforCProgrammer
 
Us
cons
insteadof 
dene 
todeclarprogramconstants
e.g
 
 
dene 
PI3.14159  
dene 
MAXINT0x7FFFFFF 
dene 
MAXUNSIGNED0xFFFFFFF
 
C+
cons
doubleP
3.14159; 
constint 
MAXINT 
0x7FFFFFFF; 
constunsigned 
MAXUNSIGNED 
0xFFFFFFFF; 
 
Namesdeclaredwith 
dene 
areuntyped andunrestrictedinscop
 
Incontrast,namesdeclaredwit
cons
artypedandfollowC++scoperule
 
e.g
cons
shavestaticlinkag
::
TipsforCProgrammerscont'd 
 
Useinlinefunctionsandparameterizedtypes insteadofpreprocessormacros, 
e.g
 
 
Macros  
dene 
MAXA,BA 
B?A:B 
::
* MAX
+
,
+
;*Trouble!* 
 
Usingatypeasaparameter 
dene 
DECLAREMAXTYPE 
 
TYPEMAXTYPEa,TYPEb 
 
return 
b?a:b
DECLAREMAX 
in
 DECLAREMAX 
doubl
 DECLAREMAX 
char 
 
 
C+
inlinein
MAX 
in
a
in
b 
return 
b?a:b
::
* MAX
+
,
+
;*Noproblem!* 
template 
 
class 
 
inlin
MAXTa,Tb 
retur
b?a:b
TipsforCProgrammerscont'd 
 
Note,therearestillsomeusesforprepro- cessor,however
e.g
 
Wrappingheadersandcommentingoutcode blocks:  
ifndef 
FOOBAR 
dene 
FOOBAR
::
 
endi 
Stringizingandtokenpasting  
dene 
name2A,BA
 
Fileinclusio 
includ
 
iostream.
 
 
TipsforCProgrammerscont'd 
 
Becarefultodistinguishbetween 
in
an
unsigned 
 
UnlikeC,C++distinguishesbetwee
in
an
unsignedint 
,sobecarefulwhenusingoverloadedfunctions
 
includ
 
iostream.
 
inlinevoi
f 
in
 
cout 
 
"fintcalled 
 
n"
inlinevoi
f 
unsigne
 
cout 
 
"funsignedcalled 
 
n"
in
main 
voi
 
f1;callsf 
in
 f1U;callsf 
unsigned 
 
TipsforCProgrammerscont'd 
 
Considerusingreferencesinsteadofpoint- ersasfunctionarguments
e.g
 
voi
screensize 
unsigned 
*height, 
unsigne
*width; 
::
* 
unsigned 
height,width; screensize&height,&width; 
 
C+
voi
screensize 
unsigned 
&height, 
unsigned 
&width 
::
unsigned 
height,width; screensizeheight,width
 
However,itishardertotellifargumentaremodiedwiththisapproach! 
TipsforCProgrammerscont'd 
 
Declarereferenceorpointerargumentsthat arenotmodiedbyafunctiona
cons
e.g
 
struct 
BigStruct 
in
array 
 
100000 
 
in
size
voi
foo 
struct 
BigStruct*bspassedaspointerforecienc
in
strlen 
char 
*str; 
 
C+
voi
foo 
cons
BigStruct&bs
in
strlen 
constchar 
*str; 
 
Thisallowscallerstous
cons
values asargumentsandalsopreventsfunctions fromaccidentallymodifyingtheirargument
TipsforCProgrammerscont'd 
 
Useoverloadedfunctionnamesinsteadof dierentfunctionnamestodistinguishbe- tweenfunctionsthatperformthesamoperationsondierentdatatypes
 
in
abs 
in
x
doubl
fabs 
doubl
x
lon
labs 
lon
x
 
C+
in
abs 
in
x
doubl
abs 
doubl
x
lon
abs 
lon
x
 
DonotforgetthatC++doe
NO
permitoverloadingonthebasisofreturntype! 
 
TipsforCProgrammerscont'd 
 
Us
ne
an
delete 
insteadof 
malloc 
an
free 
e.g
 
in
siz
100; 
in
*ip
mallocsize;*Error!!!* 
::
* freeipa; 
 
C+
constint 
siz
100; 
in
*ip
newint 
 
siz
 
 
::
delete 
ipa; 
 
ne
canbothhelpavoidcommonerrorwith 
malloc 
andalsoensurethatconstruc- torsanddestructorsarecalled 
TipsforCProgrammerscont'd 
 
UseiostreamIOoperator
 
an
 
insteadof 
printf 
an
scanf 
 
oat 
xscanf
 
f",&xprintf"Theansweri
 
 
n",xfprintfstderr,"Invalidcomman
 
n"
 
C+ci
 
xcout 
 
"Theansweris
 
 
 
n"cer
 
"Invalidcommand 
 
n"
 
Th
 
an
 
streamIOoperatorsar1type-safeand2extensibletouserdenedtype
1
TipsforCProgrammerscont'd 
 
Us
stati
objectswithconstructordestructorinsteadofexplicitlycallinginitializationnalizatiofunction
 
struct 
SymbolTabl
::
* 
voi
initsymboltable 
struct 
SymbolTable*
in
lookup 
struct 
SymbolTable*
staticstruct 
SymbolTablesymtab
intmai
 
voi
 
char 
 
10
 
initsymboltable&symtab
::
* 
 
C+
class 
SymbolTable
private 
HashTabl
publi
SymbolTable 
voi
;inittabl
in
lookupString&key
SymbolTable 
voi
stati
SymbolTablesymtab; 
in
main 
voi
 
Strings; 
whil
ci
 
s 
i
symtab.lookups
0 cout 
 
"found
 
 
 
n"
1
TipsforCProgrammerscont'd 
 
Declarevariablesneartheplacewherethey areused,andinitializevariablesintheideclarations, 
e.g
 
voi
dupassign 
char 
**dst
char 
*src 
in
len
in
i
i
src 
=
*dst 
return 
i
*dst
0free*dstle
strlensrc; *dst 
 
char 
*malloclen+1
fo
0;
 
len;
+
*dst 
 
sr
 
 
 
C+
voi
dupassign 
char 
*&dst
constchar 
*src 
i
src 
=
dst 
return 
delete 
dst; 
delete 
checksfords
=
in
le
strlensrc; ds
newcha
 
len+
 
fo
 
in
0;
 
len;
+
ds
 
sr
 
 
1

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->