| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c/c++ >> C++ cheng xu she ji zui jia shi jian

C++ cheng xu she ji zui jia shi jian

2008-06-01 02:05:04 编辑來源:互联网 评论
 
 
 
本文为【C++程序设计最佳实践】的拼音翻译版
  sui zhe ji suan ji yu yan de fa zhan , wo men xian zai bian xie yi ge cheng xu yue lai yue qing yi le 。 li yong yi xie ruan jian kai fa gong ju , wang wang zhi yao tong guo shu biao de tuo tuo dian dian , ji suan ji jiu hui zi dong bang ni sheng cheng xu duo dai ma 。 dan zai hen duo shi hou , ji suan ji de zhe zhong neng li bei lan yong le , wo men wang wang zhi kao lv ba zhe ge cheng xu da qi lai , er bu qu kao lv cheng xu de xing neng ru he , cheng xu shi fou zu gou de jian zhuang 。
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  er ci jie ke de mu de zhu yao shi jie shao yi xie bian ma de jing yan , rang da jia bian xie de cheng xu geng jia jian zhuang he gao xing neng 。1、PRefer const and inline to #define
  zai C++ bian cheng zhong ying gai jin liang shi yong const he inline lai dai ti #define, jin liang zuo dao neng bu yong #define jiu bu yong 。#define chang jian de yong tu you “ ding yi chang liang ” yi ji “ ding yi hong ”, dan qi zhong cun zai zhu duo de bi bing 。
  di yi , cha cuo bu zhi guan , bu li yu tiao shi 。Define de ding yi shi you yu chu li cheng xu chu li de , zuo de shi wan quan de wen ben ti huan , bu zuo ren he de lei xing jian cha 。 zai bian yi qi chu li jie duan ,define ding yi de dong xi yi jing bei wan quan ti huan le , zhe yang zai debug de shi hou jiu kan bu dao ren he de xiang guan xin xi , ji gen zong shi bu neng step into hong 。 li ru , ba aspECT_RATIO yong define ding yi cheng 1.653, bian yi qi jiu kan bu dao ASPECT_RATIO zhe ge ming zi le 。 jia ru bian yi qi bao 1.653 cuo , na me jiu wu cong zhi dao ci 1.653 lai zi yu he chu 。 zai zhen zheng bian ma de shi hou ying gai shi yong ru xia de yu ju lai ding yi :
  static const double ASPECT_RATIO = 1.653;
  di er , mei you ren he lei xing xin xi , bu shi type safe。 yin wei ta shi wen ben ji bie de ti huan , zhe yang bu li yu cheng xu de wei hu 。
  di san ,define de shi yong hen qing yi zao cheng wu ran 。 bi ru , jia ru you liang ge tou wen jian dou ding yi le ASPECT_RATIO, er yi ge CPP wen jian you tong shi bao han le zhe liang ge tou wen jian , na me jiu hui zao cheng chong tu 。 geng nan cha de shi ling wai yi zhong cuo wu , bi ru you ru xia de dai ma :
  // in header file def.h
  #define Apple 1
  #define Orange 2
  #define Pineapple 3
  …
  // in some cpp file that includes the def.h
  enum Colors {White, Black, Purple, Orange};
  zai .h wen jian zhong Orange bei ding yi cheng shui guo de yi zhong , er zai .cpp wen jian zhong Orange you cheng wei le yi zhong yan se , na me bian yi qi jiu hui ba ci chu de Orange ti huan cheng 2, bian yi ke neng reng ran ke yi tong guo , cheng xu ye neng gou yun xing , dan shi zhe jiu cheng le yi ge bug, biao xian chu gu guai de cuo wu , qie hen nan cha cuo 。 zai bi ru ding yi le yi ge qiu a yu b na ge shu da de hong ,#define max(a,b) ((a) > (b) ? (a) : (b))
  int a = 5, b = 0;
  max(++ a, b);
  max(++ a, b + 10);
  zai shang mian de cao zuo zhong ,max(++ a, b); yu ju zhong a bei ++ le liang ci , er max(++ a, b + 10); yu ju zhong a zhi jia le yi ci , zhe yang zai cheng xu chu li zhong jiu hen you ke neng cheng wei yi ge bug, qie ci bug ye fei chang de nan zhao 。 zai shi ji bian ma shi ke yi shi yong ru xia de yu ju lai zuo :
  template<class T>
  inline const T&
  max(const T& a, const T& b) { return a > b ? a : b; }
  2、Prefer C++-style casts
  zai cheng xu zhong jing chang hui xu yao ba yi zhong lei xing zhuan huan cheng ling wai yi zhong lei xing , zai C++ zhong ying gai shi yong static_cast、const_cast、dynamic_cast、reinterpret_cast yao hai zi lai zuo lei xing zhuan huan 。 yin wei zhe you yi xia hao chu , yi shi qi ben shen jiu shi yi zhong zhu shi , zai dai ma zhong kan dao shang mian zhe xie yao hai zi jiu ke ma shang zhi dao ci chu shi jin xing lei xing zhuan huan 。 er shi C yu yan zhong lei xing zhuan huan tong chang shi hen nan jin xing sou suo de , er tong guo yao hai zi cast ze ke yi hen qing yi de zhao dao cheng xu zhong chu xian lei xing zhuan huan de di fang le 。
  3、Distinguish between prefix and postfix forms of increment and decrement Operators
  tong chang dui yu cao zuo xi tong huo bian yi qi zi shen zhi chi de lei xing ,prefix( qian zhui , ru ++i) yu postfix( hou zhui , ru i++) de xiao guo shi yi yang de 。 yin wei xian zai de bian yi qi dou hen cong ming , ta hui zi dong zuo you hua , zhe liang zhe de hui bian dai ma shi yi yang de , xing neng bu hui you cha bie 。 dan you shi hou ye hui you bu tong de , ru yi xie chong zai le cao zuo fu de lei xing 。 xia mian shi mo ni prefix yu postfix de cao zuo guo cheng , ke yi fa xian zai postfix cao zuo zhong hui sheng cheng yi ge lin shi bian liang , er zhe yi lin shi bian liang shi hui zhan yong e wai de shi jian he kai xiao de 。
  
   // prefix form: increment and fetch
  UPInt& UPInt::operator++()
  {
  *this += 1; // increment
  return *this; // fetch
  }
  // postfix form: fetch and increment
  const UPInt UPInt::operator++(int)
  {
  UPInt oldValue = *this; // fetch
  ++(*this); // increment
  return oldValue; // return what was fetched
  }
  yi ban qing kuang xia bu xu yao qu fen shi xian ++, huan shi hou ++, dan shi wo men zai bian xie cheng xu de shi hou zui hao neng xi guan xing de jiang qi xie cheng ++i de xing shi , ru zai shi yong STL zhong de iterator shi ,prefix yu postfix hui you xiang dang da de xing neng cha yi 。 qing bu yao xiao kan zhe xie xi jie , shi ji zai bian xie cheng xu de shi hou , ruo bu zhu chong ju ti xi jie , ni hui fa xian cheng xu de xing neng hui fei chang de di 。 dan yao zhu chong , sui ran zai da duo shu qing kuang xia ke yi yong prefix lai dai ti postfix, dan you yi zhong qing kuang li wai , na jiu shi you [] cao zuo fu shi , bi ru gzArray [++index] shi bu deng yu gzArray[index++] de 。
  
C++程序设计最佳实践
geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo
  4、Minimizing Compile-time Dependencies
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  you xie ren zai bian xie cheng xu shi , wang wang xi huan jiang yi ge .h wen jian bao han dao ling yi ge .h wen jian , er shi jian zheng shi zai zuo da xing ruan jian shi zhe shi yi ge fei chang bu hao de xi guan , yin zhe yang hui zao cheng hen duo yi kao de wen ti , bao han jiao duo de .h wen jian , bie ren you shi yong le zhe ge class, er zai ta de na ge gong cheng zhong ke neng bing bu cun zai zhe xie .h wen jian , zhe yang hen ke neng jiu bian yi bu neng tong guo 。 er qie zhe yang zuo , huan ke neng zao cheng hen nan qu geng xin yi ge mo kuai de qing kuang 。 yin wei yi ge .h wen jian bei hen duo mo kuai bao han de hua , jia ru xiu gai le ci .h wen jian , zai bian yi xi tong de shi hou , bian yi qi hui qu xun zhao na xie mo kuai yi kao yu mou ge bei xiu gai guo de .h wen jian , na me jiu dao zhi le suo you bao han ru ci .h wen jian de mo kuai quan dou yao jin xing chong xin bian yi 。 zai xiang mu bi jiao xiao de shi hou , da jia ke neng huan gan jiao bu dao cha bie , dan shi jia ru shuo shi zai da xing de ruan jian xi tong li , ni ke neng bian yi yi bian yuan ma xu yao qi 、 ba ge xiao shi 。 jia ru ni zhe ge .h wen jian bei hen duo mo kuai bao han de hua , jiu suan zai .h wen jian zhong jia le yi xing zhu shi , zai bian yi shi bian yi qi jian cha na xie wen jian bei gai dong , na me suo you bao han ru ci .h wen jian de mo kuai dou hui bei chong xin bian yi , zao cheng ju da de shi jian he jing li fu dan 。 dui yu ci wen ti , jie jue de fang fa jiu shi rang .h wen jian zi bao han , ye jiu shi shuo rang ta bao han jin liang shao de dong xi 。 suo wei jin liang shao shi zhi ru shan diao ren he yi ge ta bao han jin lai de .h wen jian , dou jiang wu fa zheng chang jin xing gong zuo 。 qi shi zai hen duo qing kuang xia , bing bu xu yao yi ge .h wen jian qu bao han ling yi ge .h wen jian , wan quan ke yi tong guo class sheng ming lai jie jue yi kao guan xi de zhe zhong wen ti 。 zai lai kan xia mian zhe ge li zi :
  #include "a.h" // class A
  #include "b.h" // class B
  #include "c.h" // class C
  #include "d.h" // class D
  #include "e.h" // class E
  class X : public A, private B
  {
  public:
  E SomeFunctionCall(E someParameter);
  private:
  D m_dInstance;
  };
  dang lei X cong lei A he lei B zhong pai sheng shi , xu yao zhi dao X zai nei cun zhong dou you na xie data, tong chang zai nei cun zhong qian mian shi ji lei de data, hou mian jin gen de shi ci pai sheng lei zi shen ding yi de data, yin ci jiu bi xu zhi dao lei A yu lei B de nei bu xi jie , yao bu ran bian yi qi jiu wu fa lai an pai nei cun le 。 dan shi zai chu li shen shu yi ji shen shu fan hui zhi de shi hou , shi ji shang bing bu xu yao zhi dao zhe xie xin xi , zai ci chu ding yi de SomeFunctionCall() zhi xu zhi dao E shi ge class jiu zu gou le , bing bu xu yao zhi dao lei E zhong de data ru chang du deng de ju ti xi jie 。 shang mian de dai ma ying gai gai xie cheng ru xia de xing shi , yi jian shao yi kao guan xi :
  #include "a.h" // class A
  #include "b.h" // class B
  #include "c.h" // class C
  #include "d.h" // class D
  class E;
  class X : public A, private B
  {
  public:
  E SomeFunctionCall(E someParameter);
  private:
  D m_dInstance;
  };
  5、Never treat arrays polymorphically
  bu yao ba shu zu he duo tai yi qi shi yong , qing kan xia mian de li zi 。
  class BST { ... };
  class BalancedBST: public BST { ... };
  void printBSTArray(ostream& s, const BST array[], int numElements)
  {
  for (int i = 0; i < numElements; ++i)
  {
  s << array[i];
  // this assumes an operator<< is defined for BST
  
   }
  }
  BalancedBST bBSTArray[10];
  printBSTArray(cout, bBSTArray, 10);
  shu zu zai nei cun zhong shi yi ge lian xu de nei cun kong jian , er zai shu zu zhong ying gai ru he lai ding wei yi ge yuan su ne ? guo cheng shi zhe yang de , bian yi qi ke yi zhi dao mei ge shu ju lei xing de chang du da xiao , jia ru shu zu de index shi 0, ze hui zi dong qu qu di yi ge yuan su ; jia ru shi zhi ding le mou ge index, bian yi qi ze hui gen ju ci index yu gai shu ju lei xing de chang du zi dong qu suan chu gai yuan su de wei zhi 。
  zai printBSTArray() han shu zhong , jin guan chuan ru de shen shu shi BalancedBST lei xing , dan you yu qi ben lai ding yi de lei xing shi BST, na me ta yi ran hui gen ju BST lai ji suan lei xing de chang du 。 er tong chang pai sheng lei shi li suo zhan de nei cun yao bi ji lei shi li suo zhan de nei cun da yi xie , yin ci gai cheng xu zai bian yi shi hui bao cuo 。 qing ji zhu , yong yuan bu yao ba shu zu he C++ de duo tai xing fang zai yi qi shi yong 。
  6、Prevent exceptions from leaving destrUCtors
  xi gou han shu zhong yi ding bu yao pao chu yi chang 。 tong chang you liang zhong qing kuang hui dao zhi xi gou han shu de tiao yong , yi zhong shi dang gai lei de dui xiang li kai le ta de yu , huo delete biao da shi zhong yi ge gai lei dui xiang de zhi zhen , ling yi zhong shi you yu yi chang er yin qi xi gou han shu de tiao yong 。
  jia ru xi gou han shu bei tiao yong shi you yu exception yin qi , er ci shi zai xi gou han shu zhong you pao chu le yi chang , cheng xu hui li ji bei xi tong zhong zhi , shen zhi dou lai bu ji jin xing nei cun shi fang 。 yin ci jia ru zai xi gou han shu zhong pao chu yi chang de hua , jiu hen qing yi hun xiao yin qi yi chang de yuan yin , qie zhe yang de ruan jian ye hui rang yong hu fei chang nao huo 。 you yu xi gou han shu zhong hen ke neng hui tiao yong qi ta de yi xie han shu , suo yi zai xie xi gou han shu de shi hou yi ding yao zhu chong , dui zhe xie han shu shi fou hui pao chu yi chang yao fei chang qing chu , jia ru hui de hua , jiu yi ding yao xiao xin le 。 bi ru xia mian zhe duan dai ma :
  session::~Session()
  {
  logDestruction(this);
  }
  bi ru logDestruction() han shu ke neng hui pao chu yi chang , na me wo men jiu ying gai cai yong xia mian zhe zhong dai ma de xing shi :
  Session::~Session()
  {
  try
  {
  logDestruction(this);
  }
  catch (...)
  {
  }
  }
  zhe yang cheng xu chu cuo de shi hou bu hui bei li ji guan diao , ke yi gei yong hu yi xie qi ta de xuan ze , zhi shao xian rang ta ba mu qian zai zuo de gong zuo bao cun xia lai 。
  7、Optimization:Remember the 80-20 rule
  zai ruan jian jie you yi ge 20-80 fa ze , qi shi zhe shi yi ge hen you qu de xian xiang , bi ru yi ge cheng xu zhong 20% de dai ma shi yong le gai cheng xu suo zhan zi yuan de 80%; yi ge cheng xu zhong 20% de dai ma zhan yong le zong yun xing shi jian de 80%; yi ge cheng xu zhong 20% de dai ma shi yong le gai cheng xu suo zhan nei cun de 80%; zai 20% de dai ma shang mian xu yao hua fei 80% de wei hu li liang , deng deng 。 zhe ge gui lv huan ke yi bei ji xu tui guang xia qu , bu guo zhe ge gui lv wu fa bei zheng shi , ta shi ren men zai shi jian zhong guan cha de chu de jie guo 。 cong zhe ge gui lv chu fa , wo men zai zuo cheng xu you hua de shi hou , jiu you le zhen dui xing 。 bi ru xiang ti gao dai ma de yun xing su du , gen ju zhe ge gui lv ke yi zhi dao qi zhong 20% de dai ma zhan yong le 80% de yun xing shi jian , yin ci wo men zhi yao zhao dao zhe 20% de dai ma , bing jin xing xiang ying de you hua , na me wo men cheng xu de yun xing su du jiu ke yi you jiao da de ti gao 。 zai ru you yi ge han shu , zhan yong le cheng xu 80% de yun xing shi jian , jia ru ba zhe ge han shu de zhi xing su du ti gao 10 bei , na me dui cheng xu zheng ti xing neng de ti gao , ying xiang shi fei chang ju da de 。 jia ru you yi ge han shu yun xing shi jian zhi zhan zong shi jian de 1%, na jiu suan ba zhe ge han shu de yun xing su du ti gao 1000 bei , dui cheng xu zheng ti xing neng de ti gao ye shi ying xiang bu da de 。 suo yi wo men de ji ben si xiang jiu shi zhao dao zhan yong yun xing shi jian zui da de na ge han shu , ran hou qu you hua ta , na pa zhi shi gai jin le yi dian dian , cheng xu de zheng ti xing neng ye ke yi bei ti gao hen duo 。
  yao xiang zhao chu na 20% de dai ma , wo men de fang fa jiu shi shi yong Profiler, ta shi ji shang shi yi xie gong si suo kai fa de gong ju , ke yi jian cha cheng xu zhong ge ge mo kuai suo fen pei nei cun de shi yong qing kuang , yi ji mei ge han shu suo yun xing de shi jian deng 。 chang jian de Profiler you Intel gong si kai fa de VTune, wei ruan gong si kai fa de Visual Studio profiler,DevPartner from Compuware deng 。
C++程序设计最佳实践
geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo【原文】【汉音对照
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
   sui zhe ji suan ji yu yan de fa zhan , wo men xian zai bian xie yi ge cheng xu yue lai yue qing yi le 。 li yong yi xie ruan jian kai fa gong ju , wang wang zhi yao tong guo shu biao de tuo tuo dian dian , ji suan ji jiu hui zi dong bang ni sheng cheng xu duo dai ma 。 dan zai hen duo shi hou , ji suan ji de zhe zhong neng li bei lan yong le , wo men wang wang zhi kao lv ba zhe ge cheng xu da qi lai , er bu qu kao lv cheng xu de xing neng ru he , cheng xu shi fou zu gou de jian zhuang 。 er ci jie ke de mu de zhu yao shi jie shao yi xie bian ma de jing yan , rang da jia bian xie de cheng xu geng jia jian zhuang he gao xing neng 。  1、PRefer const and inline to #define    zai C++ bian cheng zhong ying gai jin liang shi yong const he inline lai dai ti #define, jin liang zuo dao neng bu yong #define jiu bu yong 。#define chang jian de yong tu you “ ding yi chang liang ” yi ji “ ding yi hong ”, dan qi zhong cun zai zhu duo de bi bing 。    di yi , cha cuo bu zhi guan , bu li yu tiao shi 。Define de ding yi shi you yu chu li cheng xu chu li de , zuo de shi wan quan de wen ben ti huan , bu zuo ren he de lei xing jian cha 。 zai bian yi qi chu li jie duan ,define ding yi de dong xi yi jing bei wan quan ti huan le , zhe yang zai debug de shi hou jiu kan bu dao ren he de xiang guan xin xi , ji gen zong shi bu neng step into hong 。 li ru , ba aspECT_RATIO yong define ding yi cheng 1.653, bian yi qi jiu kan bu dao ASPECT_RATIO zhe ge ming zi le 。 jia ru bian yi qi bao 1.653 cuo , na me jiu wu cong zhi dao ci 1.653 lai zi yu he chu 。 zai zhen zheng bian ma de shi hou ying gai shi yong ru xia de yu ju lai ding yi : static const double ASPECT_RATIO = 1.653;    di er , mei you ren he lei xing xin xi , bu shi type safe。 yin wei ta shi wen ben ji bie de ti huan , zhe yang bu li yu cheng xu de wei hu 。    di san ,define de shi yong hen qing yi zao cheng wu ran 。 bi ru , jia ru you liang ge tou wen jian dou ding yi le ASPECT_RATIO, er yi ge CPP wen jian you tong shi bao han le zhe liang ge tou wen jian , na me jiu hui zao cheng chong tu 。 geng nan cha de shi ling wai yi zhong cuo wu , bi ru you ru xia de dai ma :   // in header file def.h   #define Apple 1   #define Orange 2     #define Pineapple 3    …   // in some cpp file that includes the def.h   enum Colors {White, Black, Purple, Orange};    zai .h wen jian zhong Orange bei ding yi cheng shui guo de yi zhong , er zai .cpp wen jian zhong Orange you cheng wei le yi zhong yan se , na me bian yi qi jiu hui ba ci chu de Orange ti huan cheng 2, bian yi ke neng reng ran ke yi tong guo , cheng xu ye neng gou yun xing , dan shi zhe jiu cheng le yi ge bug, biao xian chu gu guai de cuo wu , qie hen nan cha cuo 。 zai bi ru ding yi le yi ge qiu a yu b na ge shu da de hong ,#define max(a,b) ((a) > (b) ? (a) : (b))   int a = 5, b = 0;   max(++ a, b);   max(++ a, b + 10);    zai shang mian de cao zuo zhong ,max(++ a, b); yu ju zhong a bei ++ le liang ci , er max(++ a, b + 10); yu ju zhong a zhi jia le yi ci , zhe yang zai cheng xu chu li zhong jiu hen you ke neng cheng wei yi ge bug, qie ci bug ye fei chang de nan zhao 。 zai shi ji bian ma shi ke yi shi yong ru xia de yu ju lai zuo :   template<class T>   inline const T&   max(const T& a, const T& b) { return a > b ? a : b; }   2、Prefer C++-style casts    zai cheng xu zhong jing chang hui xu yao ba yi zhong lei xing zhuan huan cheng ling wai yi zhong lei xing , zai C++ zhong ying gai shi yong static_cast、const_cast、dynamic_cast、reinterpret_cast yao hai zi lai zuo lei xing zhuan huan 。 yin wei zhe you yi xia hao chu , yi shi qi ben shen jiu shi yi zhong zhu shi , zai dai ma zhong kan dao shang mian zhe xie yao hai zi jiu ke ma shang zhi dao ci chu shi jin xing lei xing zhuan huan 。 er shi C yu yan zhong lei xing zhuan huan tong chang shi hen nan jin xing sou suo de , er tong guo yao hai zi cast ze ke yi hen qing yi de zhao dao cheng xu zhong chu xian lei xing zhuan huan de di fang le 。   3、Distinguish between prefix and postfix forms of increment and decrement Operators    tong chang dui yu cao zuo xi tong huo bian yi qi zi shen zhi chi de lei xing ,prefix( qian zhui , ru ++i) yu postfix( hou zhui , ru i++) de xiao guo shi yi yang de 。 yin wei xian zai de bian yi qi dou hen cong ming , ta hui zi dong zuo you hua , zhe liang zhe de hui bian dai ma shi yi yang de , xing neng bu hui you cha bie 。 dan you shi hou ye hui you bu tong de , ru yi xie chong zai le cao zuo fu de lei xing 。 xia mian shi mo ni prefix yu postfix de cao zuo guo cheng , ke yi fa xian zai postfix cao zuo zhong hui sheng cheng yi ge lin shi bian liang , er zhe yi lin shi bian liang shi hui zhan yong e wai de shi jian he kai xiao de 。   // prefix form: increment and fetch   UPInt& UPInt::operator++()    {     *this += 1; // increment    return *this; // fetch    }   // postfix form: fetch and increment    const UPInt UPInt::operator++(int)    {     UPInt oldValue = *this; // fetch    ++(*this); // increment     return oldValue; // return what was fetched    }    yi ban qing kuang xia bu xu yao qu fen shi xian ++, huan shi hou ++, dan shi wo men zai bian xie cheng xu de shi hou zui hao neng xi guan xing de jiang qi xie cheng ++i de xing shi , ru zai shi yong STL zhong de iterator shi ,prefix yu postfix hui you xiang dang da de xing neng cha yi 。 qing bu yao xiao kan zhe xie xi jie , shi ji zai bian xie cheng xu de shi hou , ruo bu zhu chong ju ti xi jie , ni hui fa xian cheng xu de xing neng hui fei chang de di 。 dan yao zhu chong , sui ran zai da duo shu qing kuang xia ke yi yong prefix lai dai ti postfix, dan you yi zhong qing kuang li wai , na jiu shi you [] cao zuo fu shi , bi ru gzArray [++index] shi bu deng yu gzArray[index++] de 。 [url=http://www.wangchao.net.cn/bbs/pydetail_1785335.html][img]http://image.wangchao.net.cn/it/1323423846557.gif[/img][/url] geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo   4、Minimizing Compile-time Dependencies    you xie ren zai bian xie cheng xu shi , wang wang xi huan jiang yi ge .h wen jian bao han dao ling yi ge .h wen jian , er shi jian zheng shi zai zuo da xing ruan jian shi zhe shi yi ge fei chang bu hao de xi guan , yin zhe yang hui zao cheng hen duo yi kao de wen ti , bao han jiao duo de .h wen jian , bie ren you shi yong le zhe ge class, er zai ta de na ge gong cheng zhong ke neng bing bu cun zai zhe xie .h wen jian , zhe yang hen ke neng jiu bian yi bu neng tong guo 。 er qie zhe yang zuo , huan ke neng zao cheng hen nan qu geng xin yi ge mo kuai de qing kuang 。 yin wei yi ge .h wen jian bei hen duo mo kuai bao han de hua , jia ru xiu gai le ci .h wen jian , zai bian yi xi tong de shi hou , bian yi qi hui qu xun zhao na xie mo kuai yi kao yu mou ge bei xiu gai guo de .h wen jian , na me jiu dao zhi le suo you bao han ru ci .h wen jian de mo kuai quan dou yao jin xing chong xin bian yi 。 zai xiang mu bi jiao xiao de shi hou , da jia ke neng huan gan jiao bu dao cha bie , dan shi jia ru shuo shi zai da xing de ruan jian xi tong li , ni ke neng bian yi yi bian yuan ma xu yao qi 、 ba ge xiao shi 。 jia ru ni zhe ge .h wen jian bei hen duo mo kuai bao han de hua , jiu suan zai .h wen jian zhong jia le yi xing zhu shi , zai bian yi shi bian yi qi jian cha na xie wen jian bei gai dong , na me suo you bao han ru ci .h wen jian de mo kuai dou hui bei chong xin bian yi , zao cheng ju da de shi jian he jing li fu dan 。 dui yu ci wen ti , jie jue de fang fa jiu shi rang .h wen jian zi bao han , ye jiu shi shuo rang ta bao han jin liang shao de dong xi 。 suo wei jin liang shao shi zhi ru shan diao ren he yi ge ta bao han jin lai de .h wen jian , dou jiang wu fa zheng chang jin xing gong zuo 。 qi shi zai hen duo qing kuang xia , bing bu xu yao yi ge .h wen jian qu bao han ling yi ge .h wen jian , wan quan ke yi tong guo class sheng ming lai jie jue yi kao guan xi de zhe zhong wen ti 。 zai lai kan xia mian zhe ge li zi :   #include "a.h" // class A   #include "b.h" // class B   #include "c.h" // class C   #include "d.h" // class D   #include "e.h" // class E   class X : public A, private B   {    public:   E SomeFunctionCall(E someParameter);    private:    D m_dInstance;   };    dang lei X cong lei A he lei B zhong pai sheng shi , xu yao zhi dao X zai nei cun zhong dou you na xie data, tong chang zai nei cun zhong qian mian shi ji lei de data, hou mian jin gen de shi ci pai sheng lei zi shen ding yi de data, yin ci jiu bi xu zhi dao lei A yu lei B de nei bu xi jie , yao bu ran bian yi qi jiu wu fa lai an pai nei cun le 。 dan shi zai chu li shen shu yi ji shen shu fan hui zhi de shi hou , shi ji shang bing bu xu yao zhi dao zhe xie xin xi , zai ci chu ding yi de SomeFunctionCall() zhi xu zhi dao E shi ge class jiu zu gou le , bing bu xu yao zhi dao lei E zhong de data ru chang du deng de ju ti xi jie 。 shang mian de dai ma ying gai gai xie cheng ru xia de xing shi , yi jian shao yi kao guan xi :   #include "a.h" // class A   #include "b.h" // class B   #include "c.h" // class C   #include "d.h" // class D   class E;   class X : public A, private B   {    public:   E SomeFunctionCall(E someParameter);    private:   D m_dInstance;   };   5、Never treat arrays polymorphically    bu yao ba shu zu he duo tai yi qi shi yong , qing kan xia mian de li zi 。   class BST { ... };   class BalancedBST: public BST { ... };   void printBSTArray(ostream& s, const BST array[], int numElements)   {   for (int i = 0; i < numElements; ++i)   {    s << array[i];   // this assumes an operator<< is defined for BST   }   }   BalancedBST bBSTArray[10];   printBSTArray(cout, bBSTArray, 10);    shu zu zai nei cun zhong shi yi ge lian xu de nei cun kong jian , er zai shu zu zhong ying gai ru he lai ding wei yi ge yuan su ne ? guo cheng shi zhe yang de , bian yi qi ke yi zhi dao mei ge shu ju lei xing de chang du da xiao , jia ru shu zu de index shi 0, ze hui zi dong qu qu di yi ge yuan su ; jia ru shi zhi ding le mou ge index, bian yi qi ze hui gen ju ci index yu gai shu ju lei xing de chang du zi dong qu suan chu gai yuan su de wei zhi 。    zai printBSTArray() han shu zhong , jin guan chuan ru de shen shu shi BalancedBST lei xing , dan you yu qi ben lai ding yi de lei xing shi BST, na me ta yi ran hui gen ju BST lai ji suan lei xing de chang du 。 er tong chang pai sheng lei shi li suo zhan de nei cun yao bi ji lei shi li suo zhan de nei cun da yi xie , yin ci gai cheng xu zai bian yi shi hui bao cuo 。 qing ji zhu , yong yuan bu yao ba shu zu he C++ de duo tai xing fang zai yi qi shi yong 。   6、Prevent exceptions from leaving destrUCtors    xi gou han shu zhong yi ding bu yao pao chu yi chang 。 tong chang you liang zhong qing kuang hui dao zhi xi gou han shu de tiao yong , yi zhong shi dang gai lei de dui xiang li kai le ta de yu , huo delete biao da shi zhong yi ge gai lei dui xiang de zhi zhen , ling yi zhong shi you yu yi chang er yin qi xi gou han shu de tiao yong 。    jia ru xi gou han shu bei tiao yong shi you yu exception yin qi , er ci shi zai xi gou han shu zhong you pao chu le yi chang , cheng xu hui li ji bei xi tong zhong zhi , shen zhi dou lai bu ji jin xing nei cun shi fang 。 yin ci jia ru zai xi gou han shu zhong pao chu yi chang de hua , jiu hen qing yi hun xiao yin qi yi chang de yuan yin , qie zhe yang de ruan jian ye hui rang yong hu fei chang nao huo 。 you yu xi gou han shu zhong hen ke neng hui tiao yong qi ta de yi xie han shu , suo yi zai xie xi gou han shu de shi hou yi ding yao zhu chong , dui zhe xie han shu shi fou hui pao chu yi chang yao fei chang qing chu , jia ru hui de hua , jiu yi ding yao xiao xin le 。 bi ru xia mian zhe duan dai ma :   session::~Session()   {   logDestruction(this);   }    bi ru logDestruction() han shu ke neng hui pao chu yi chang , na me wo men jiu ying gai cai yong xia mian zhe zhong dai ma de xing shi :   Session::~Session()   {    try   {    logDestruction(this);    }    catch (...)   {    }  }    zhe yang cheng xu chu cuo de shi hou bu hui bei li ji guan diao , ke yi gei yong hu yi xie qi ta de xuan ze , zhi shao xian rang ta ba mu qian zai zuo de gong zuo bao cun xia lai 。   7、Optimization:Remember the 80-20 rule    zai ruan jian jie you yi ge 20-80 fa ze , qi shi zhe shi yi ge hen you qu de xian xiang , bi ru yi ge cheng xu zhong 20% de dai ma shi yong le gai cheng xu suo zhan zi yuan de 80%; yi ge cheng xu zhong 20% de dai ma zhan yong le zong yun xing shi jian de 80%; yi ge cheng xu zhong 20% de dai ma shi yong le gai cheng xu suo zhan nei cun de 80%; zai 20% de dai ma shang mian xu yao hua fei 80% de wei hu li liang , deng deng 。 zhe ge gui lv huan ke yi bei ji xu tui guang xia qu , bu guo zhe ge gui lv wu fa bei zheng shi , ta shi ren men zai shi jian zhong guan cha de chu de jie guo 。 cong zhe ge gui lv chu fa , wo men zai zuo cheng xu you hua de shi hou , jiu you le zhen dui xing 。 bi ru xiang ti gao dai ma de yun xing su du , gen ju zhe ge gui lv ke yi zhi dao qi zhong 20% de dai ma zhan yong le 80% de yun xing shi jian , yin ci wo men zhi yao zhao dao zhe 20% de dai ma , bing jin xing xiang ying de you hua , na me wo men cheng xu de yun xing su du jiu ke yi you jiao da de ti gao 。 zai ru you yi ge han shu , zhan yong le cheng xu 80% de yun xing shi jian , jia ru ba zhe ge han shu de zhi xing su du ti gao 10 bei , na me dui cheng xu zheng ti xing neng de ti gao , ying xiang shi fei chang ju da de 。 jia ru you yi ge han shu yun xing shi jian zhi zhan zong shi jian de 1%, na jiu suan ba zhe ge han shu de yun xing su du ti gao 1000 bei , dui cheng xu zheng ti xing neng de ti gao ye shi ying xiang bu da de 。 suo yi wo men de ji ben si xiang jiu shi zhao dao zhan yong yun xing shi jian zui da de na ge han shu , ran hou qu you hua ta , na pa zhi shi gai jin le yi dian dian , cheng xu de zheng ti xing neng ye ke yi bei ti gao hen duo 。    yao xiang zhao chu na 20% de dai ma , wo men de fang fa jiu shi shi yong Profiler, ta shi ji shang shi yi xie gong si suo kai fa de gong ju , ke yi jian cha cheng xu zhong ge ge mo kuai suo fen pei nei cun de shi yong qing kuang , yi ji mei ge han shu suo yun xing de shi jian deng 。 chang jian de Profiler you Intel gong si kai fa de VTune, wei ruan gong si kai fa de Visual Studio profiler,DevPartner from Compuware deng 。 [url=http://www.wangchao.net.cn/bbs/pydetail_1785335.html][img]http://image.wangchao.net.cn/it/1323423846605.gif[/img][/url] geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
玉美人_珠联璧合
清纯美女米卡
清新靓丽的早晨
玲子的自信与性感
痞子的甘南日记
疑是银河落九天
雪域坝上四——纯美色
冬日恋歌——西城杨柳弄轻柔
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
 
©2005- 王朝网络 版权所有