Anda di halaman 1dari 1

12/3/2016

##macrooperatorCandC++SyntaxReferenceCprogramming.com

##macrooperator
<token>##<token>
The##operatortakestwoseparatetokensandpastesthemtogethertoformasingletoken.Theresultingtokencouldbeavariable
name,classnameoranyotheridentifier.Atrivialexamplewouldbe
#definetypei##nt
typea//sameasintasincei##ntpastestogetherto"int"
Realworldusesofthetokenpastingoperatorofteninvolveclass,variable,orfunctionnames.Forexample,youmightdecidetocreate
amacrothatdeclarestwovariables,onewithanamebasedonparametertothemacroandanotherwithadifferentname,thatstores
theoriginalvalueofthevariable(perhapsfordebugging).
#defineDECLARE_AND_SET(type,varname,value)typevarname=valuetypeorig_##varname=varname
Nowyoucanwritecodelike
DECLARE_AND_SET(int,area,2*6)
andorig_areaalwayshastheoriginalvalueofareanomatterhowthevariableischanged.
Related
#define
Cpreprocessortutorial

http://www.cprogramming.com/reference/preprocessor/tokenpastingoperator.html

1/1