资源描述:
《C静态成员变量和静态成员函数》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C卄静态成员变量和静态成员函数Datamemberscanbedividedintotwotypes:staticvariablesandnonstaticvariablesStaticmemberofastaticclasstojointhestaticmodifier,isastaticmember・Candirectlyusetheclassname+staticmembersaccessthestaticmembers,becausestaticmembersexistinmemory,nonstaticmemberswillneedtoins
2、tantiatetheallocationofmemory,sothestaticmemberscannotaccessnonstaticmembers・Becausestaticmembersexistinmemory,sononstaticmemberscandirectlyaccessthestaticmembersoftheclass.Nonstaticmembers:allwithnoStaticmembersarenonstaticmembers,whentheclassisinstantiated,canbeaccessed.Nonsta
3、ticmembersbyinstantiatingclasssurvivaldependsonthesurvivalandstaticmembersthereisnoconceptofsurvival,becausestaticmembersalwaysresideinthecontent・Aclasscanalsocontainstaticmembersandnonstaticmembers,andstaticconstructorsandnonstaticconstructorsareincludedintheclassTosumupintwoas
4、pects,thefirstaspectismainlyrelativetotheprocessoriented,thatis,inthisrespectdoesnotinvolvetheclass:secondaspects,relativetotheobject—oriented,themaindescriptionoftheroleofstaticintheclass・First,thestatickeywordinprocessorienteddesign1,staticglobalvariablesDefinition:beforethegl
5、obalvariable,plusthekeywordstatic,thevariableisdefinedasastaticglobalvariable・Characteristic:A,whichallocatesmemoryintheglobaldataarea・Bandinitialization:ifnotexplicitlyinitialized,itisimplicitlyinitializedto0(theautomaticvariableisrandomunlessexplicitlyinitialized)・TheCandvisit
6、variablesarevisibleonlyinthesourcefile,andstrictlyspeaking,thedefinitionshouldbeginattheendofthisdocument・Examples(excerptfromtheC++programmingtutorial-moneyeditorP103)://filel.cpp//Example1ttincludeVoid,FN();Staticintn;//definestaticglobalvariablesVoid,main()N二20;Co
7、ut<8、hownbelow:CodeareaGlobaldataareaHeapareaThestac