
 
䥪⨢ ᯮ짮 C++. 55  ᯮᮡ      ணࠬ



  쥩 ।樨

䥪⨢ ᯮ짮 C++ 
   <䥪⨢ ᯮ짮 C++>,  । -  業  ணࠬ஢ - ⮣ ,    ⠫   ண 業.   -  筨,   ४ ᥬ,    C++ ᨮ쭮.
 㫨, ME, , ࠧ 業  ᫥ NAVSYSCOM, ࠫ । ⠥ 襩 , 饭 ⮬,  ᫮    C++  ᮧ 䥪⨢  ७ 楫 ணࠬ. ᫨  ⥭  , ⮡  ணࠬ⮬ C++,    .
ਪ , ⠭, ९⥫   < C++>ࢠ । ⮩  뫠   讣 (쬠 讣) ᫠ ,    ⨬ ᨫ ᢮ ஢  ᨮ쭮 ࠧࠡ稪 ணࠬ ᯥ祭.   㣨   ⮣ 鸞,   ࠪ筮    ⥭,   ⮬ ᮤঠ ⢮  ᮢ⮢. <䥪⨢ ᯮ짮 C++>,  ।, த  ࠤ. C++ - 祭   ணࠬ஢. ᫨ C  ॢ,  ன    設 ,  C++ -  楫 ,  ஬ ᠬ ࠧ  ⮢   易  ⮩ ॢ 㧫.  ਠ, ਢ  ⮩ , ।   ᯮᮡ 䥪⨢ ᯮ짮 C++   㬥  ⮬  殮.
 . , ᯮ⥫ ४ Bleading Edge Software Technologies  ࠧࠡ稪,  室   , ࠧ 砥  -   .
 , 騩   ணࠬ ᯥ祭 ⠫  । <䥪⨢ ᯮ짮 C++>  9  ,    ࠧ ⠫       C++.   ,   <䥪⨢ ᯮ짮 C++> ⠥ 易⥫  ⥭  ,   䥪⨢ ணࠬ஢  C++.  㤥   襬 , ᫨ ணࠬ C++    ०, 祬   ப ᨮ쭮 .
 ,   ணࠬ ᯥ祭ࢮ  <䥪⨢ ᯮ짮 C++>    ,    冷 ணࠬ⮬  殮 ࠫ    믮 祭 ࠡ.   뫮 ᯠᥭ!  㦨,  ᮢ  ࠪ᪨   䥪⨢,    100 % ॠ ,  .  ।   ࠪ᪮ ਬ C++  ࠡ  ᮢ६묨 쥧묨 ணࠬ묨 ஥⠬, ।⠢ ଠ  ᠬ  ।⢠   몠.   㤮⢨ 㦨,       ᭮  ᥡ  쥬  , ,   ,  祭 .
 , 孨᪨ ணࠬ  ⭮ 㪮⢮   ,  C++, ।祭  ,   ਬ C++ ᭮  䥪⨢,   室  C++   㣮 ꥪ⭮-ਥ஢ 몠.   ᮤন 業 ଠ,   ᭮, ᦠ⮬, ⥫쭮  ஭⥫쭮 ⨫.
 ࠭ , ࠧࠡ稪 ணࠬ ᯥ祭


୮

 <䥪⨢ ᯮ짮 C++>  㦥 15 ,   C++  砫 ਬ୮  5   ⮣,  ᠫ .  ࠧ, ࠡ  ⨬ ஥⮬   20 .   ६  砫 , 砭, ࠢ,    襫騥   ⥭ (?) .     ࠧ <䥪⨢ ᯮ짮 C++>.  ७  ᥬ.
  㦥 ⪠  ⮪ ,   祬  稫 ᠬ,   筨    㯮, ᪮   ﭭ.  㯯 ⥩ Usenet,  ᮡ comp.lang.c++.moderated  comp.std.c++.  ࠢ, ਢ  ⮩  (, 設⢮),   १ ᫥ 孨᪨ , 㦤   㯯.
 ⡮  ਠ, 襤襣    ,   ⨢  (Steve Dewhurst).  ࠢ 11  ॠ樨  operator= ⥬ ஢   ௭  ⮪ ࡠ  (Herb Sutter),     13   <Exceptional C++> (Addison-Wesley, 2000)[1].   墠   樠樨 (ࠢ 13) ⢮   < ணࠬ஢ C++> (<The C++ Programming Language>, Addison- Wesley, 2002) ୠ 㯠.  ࠢ 17   ࠧ <। ⮤> (<Best practices>)  ᠩ <Boost shared_ptr> (http:// boost.org/libs/ smart_ptr/shared_ptr.htm#BestPractices)  筥  ᭮ ਠ  21   Herb Sutter <More exceptional C++> (Addison-Wesley, 2002).  ࠢ 29  孮 ࠧ⮥ ᫥ ⮩ ⥬, ।ਭ⮥ ࡮ ஬,   8-19   <Exceptional C++>,  ⠪   17-23  <More exceptional C++>   11-13     Exceptional C++ Style> (Addison-Wesley, 2005).  堬 (David Abrahams)      ਭ樯 ࠭஢ ᭮ ᪫祭.  㠫쭮 䥩 (NVI)  ࠢ 35    ࡠ  <㠫쭮> (Virtuality)  ᥭ᪮  2001 . ୠ <C/C++ Users Journal>.   ⮬  ࠢ  ஥஢ < ⮤> (Template Method)  <⥣>    <Design Patterns>[2] (Addison-Wesley, 1995)   (Erich Gamma), ठ  (Richard Helm),  ᮭ (Ralf Johnson)   ᨤ (John Vlissides).  ਬ  NVI  ࠢ 37 ᪠ ਪ  (Hendrik Schober).   ࣠ (David Smallberg) - ॠ ⢠, ᠭ  ࠢ 38.   ࠢ 39   ⮬,  ⨬ ⮣    ਭ樯   ⢥ ᫥, ⢮    थ (David Vandevoorde)   .  (Nickolai M. Josuttis) <Templates C++> (<  몥 C++>) (Addison-Wesley, 2003).   ࠢ 42  ࢮ砫쭮 ।⠢  ⮬,  祣 㦭 祢 ᫮ typename, ᭮  㬥 <    C++  C> (<C++ and C FAQ>) (http://www.comeaucomputing.com/techtalk/#typename),  ন ॣ  (Greg Comeau),    (Leor Zolman)   ᮧ,   ।⠢ 訡筮 ( ,   ॣ).  ࠢ 46      (Dan Saks) <   㧥>. ᪠   ࠢ 52   ⮬,  ᫨      new,      ⠫,    22  <Exceptional C++> ࡠ .    業஢ Boost (㬬஢  ࠢ 55) 뫮 筥  堬ᮬ.
 ᪠ ᠥ ⮣,     祬- 稫  , ᨬ  ⮣,   㡫 ਠ  ᮮ⢥ ⥬.
  ⪠ ⠪ ᪠,   ᯮ짮 ଠ, 祭  ⨢  (Steve Clamage), ⮭ ࠪ (Antoine Trux),   (Timothy Knox)    (Mike Kaelbling), ,  ᮦ,   -   .
୮ ࢮ  ᬠਢ  द (Tom Cargill),  ஫ (Glenn Caroll),   (Tony Davis), ࠩ ୨ (Brian Kernigan),  ଠ (Jak Kirman),   (Doug Lea), ᥩ  (Moises Lejter),   . (Eugene Santos, Jr),   (John Shewchuk),  ᪮ (John Stasko),   (Bjarne Stroustrup), ࡠ  (Barbara Tilly)   . ࡠ (Nancy L. Urbano). ஬ ⮣,  ⭮⥫쭮 襭,  뫨 祭    २, ᪠뢠  . ࡠ,  ३祫 (Chris Treichel),  ࡨ (David Corbin),  ᮭ (Paul Gibson), ⨢ ᪨ (Steve Vinoski),  द (Tom Cargill),   (Neil Rhodes),   (David Bern),   (Russ Williams),  ࠧ (Robert Brazile),  ࣠ (Doug Morgan),  ⥩ (Uwe Steinmuller),   (Mark Somer),   (Doug Moore),  ,   (Seith Meltzer),  ⥩ (Oleg Steinbuk),   (David Papurt),  ᥭ (Tony Hansen),  -᪨ (Peter McCluskey), 䠭 嫨 (Stefan Kuhlins),  㭥 (David Braunegg),  宫 (Paul Chisholm),   (Adam Zell),  ,  , ࠦ  (Natraj Kini),   (Lars Numan), ॣ  (Greg Lutz),  ᮭ,   (John Lakos),   (Roger Scott),  ஬ (Scott Frohman),   (Alan Rooks),   (Robert Poor), ਪ  (Eric Nagler), ⮭ ࠪ,   (Cade Roux), ਪ  (Chandrika Gokul),   (Randy Mangoba)   ⥫졠 (Glenn Teitelbaum).
୮ ண  ஢﫨: ४  (Derek Bosch),  ᮭ (Tim Johnson), ࠩ ୨,   (Junichi Kimura),  ᪨ (Scott Lewandowski),  堥 (Laura Michaels),   (David Smallberg),   (Clovis Tonado),    (Chris Van Wyk)   㤠 (Oleg Zabluda).   ࠦ 먣ࠫ  ਥ  ⥩࣠ (Daniel Steinberg), 㭯ᠤ  (Arunprasad Marathe), 㣠 ⠯ (Doug Stapp),   (Robert Hall), ਫ ᮭ (Cheryl Ferguson),  ⫥ (Gary Bartlett),   (Michael Tamm),   (Kendall Beaman), ਪ ,  ਭ (Max Hailperin),  ⬠ (Joe Gottman), ठ  (Richard Weeks), ⨭ ठ (Valentin Bonnard),   (Jun He),   (Tim King),   (Don Mailer),   (Ted Hill), ઠ ᮭ (Marc Harrison),  㡨⥩ (Michael Rubinstein), ઠ  (Marc Rodgers),   (David Goh), ७⮭ 㯥 (Brenton Cooper),  -ࠬ (Andy Thomas-Cramer), ⮭ ࠪ,   (John Walt), ࠩ ஭ (Brian Sharon),  毠ਪ (Liam Fitzpatric), ୤  (Bernd Mohr),   (Gary Yee),  ' (John O'Hanley), । ᮭ (Brady Paterson),  ᮭ (Christopher Peterson),  㧭猪 (Feliks Kluzniak,   (Isi Dunetz),   (Christopher Creutzi),  㯥 (Ian Cooper), ૠ  (Carl Harris), ઠ ⨪ (Marc Stickel),  㤨 (Clay Budin),  樭㫮 (Panayotis Matsinopulos),  ࣠, ࡠ ,  ᫥楢 (Pajo Misljencevic), 㫨 ⨭ (Giulio Agostini), ।ਪ  (Fredrik Blonqvist),   (Jimmy Snyder), ਠ ᥭ (Byrial Jensen), ⮫줠 쬨᪮ (Witold Kuzminski), 㭮 ﬠ (Kazunobu Kuriyama),  ⥭ᥭ (Michael Christensen), ࣠  㥫 (Jorge Yanez Teruel), ઠ  (Mark Davis),   (Marty Rabinowitz),   (Ares Lagae)  ᠭ .
  ୮ 饣  ᬠਢ: ࠩ ୨,  ,  竨,  . ᥭ,   ,    ਪ . ᬮ  ⥪ ୮ ⢫﫨:  ,  ᠮ, ਪ ,  ⭨,  堬,  㧥,  ஭, ࠩ ୨,  ਬ,  ,  ,  ,  , ਪ ,  ,  , ⨢ ਯ, ⨭ ⥤,  ,  ,  ,  , 㤮 ⮫,  ,  ௥,  , 蠭 ⠫,  ,  , ᥭ , ७ , ॣ ,  堭,  ,  ,  , ᠭ ⨭,  ⨭, ⨢ ,  ଠ,  ,  , 㭠ठ 㫠,  ,  奭, ਪ 㡥,  樠,  ,  . 堬,  ,  ᮭ, ३ ᠭ,  ᮭ,  ,  㫨   . ୮  ⤥ ࠣ䮢, ஬ ⮣, ᬠਢ    ⨫ . .
ᬮ ன (, , ) 㪮 -  㤭 ࠡ,  稥 ⪨ ப ⮫쪮    㤭.  ७ ᥬ,  ࠧ     ⮬.
ᬮ 㪮 ⥬  㤥, ᫨    ।⠢  ਠ,    ய   筮,      ⥪. ࠧ⥫쭮,  室 , ᮣ ।஢ ⥪.   뫠 ।஬ ⮩   㬥   訡,  ய⨫  ⠫.
   室 業஢ 㪮 ஢ਫ  ਬ   ࠧ ,  ⥬ ᤥ   ࠧ, ᫥ ⮣    . ᫨ - 訡 ⠫,    ⢥⢥ ,   .
   ᮡ  ᮭ ᠫ ⪨,   ⥪  .
 , ।    ⮩ , ᬮ⥫쭮 ᮣᨫ ᭮ ࠡ  ⮬ ⢥.  魨,  ᥭ,  ⢥砫 ⭮ 롪     竨 砭 ( ࠩ ,  ⠪ ,  筮      砫). 㫨 娫 <騫  ᮫>,  諮 ⢥  ந⢮ ⮩ .  祭    ᨤ 砬, ⮡ 뤥ঠ 䨪,    ⮬ 嫠஢.  㫫 ( 砫쭨)    ( 砫쭨) ⠪ ਭ ।⢥ ⨥   ⮢ . 樠 易   砫  ஢   ணࠬ FrameMaker  ᮧ ⥪  ଠ PDF,    ᢮ 樠⨢ ᫠   ਫ B  ଠ஢     ७ ஭ . 좥    ⠢ 㪠⥫.  ன    ⢥砫   .   室 ।뢠  直 ࠧ,   ਫ <  ⮣, ⮡   ⮣,   ᪮ 㣮 梥?>.  - ᮢ襭 묮⠫,  થ⨭ .
 祭 ᪮쪨 楢,   ࠡ⠫  㪮, ⥫ ਠ < - 㡨 ஢>       . ॡ  ᨫ, ⮡  ப   ࠭ ⮩ .
  稫  ணࠬ஢  1971 ,   ࠤ,   ⠥ ﬨ  ᥩ .  ७       ࠧࠡ⪨ 祡 ਠ  C++  1989  ( ⠢  ⢨⥫쭮  C++),   1991   ਢ   १樨    Stratus.  㤥    ,  ᫥⢨ ⠫ ࢮ ।樥 ⮩ .  ⠪     ⮬,  ᮣᨫ 㡫 .
 ,  . ࡠ, த   ⥫⢮,  ᫥ ᥬ  , 樨   CD  樨.    ௥.       ᬮ ᤥ ,  ᤥ.
 砫    ᮡ 䮭 뫠  ᪮ 쮭.  ᮦ,  襩  ஥  ⢮, 㦥 室  ॡ쭮 ୥.   祭  墠⠥.



।᫮

 ᠫ  ਠ  <䥪⨢ ᯮ짮 C++>  1991 .   1997 . ⠫ ६  ண ,  ⢥  ਠ, ,    ⥫,    , ࠫ ࠭  : 48  ਣ 50 ࠢ ⠫   묨. ᫨ ࠢ   ,  ஥  뫮 宦  ᬥ᪨ ६ - ४ ,   㣨 梥   ᢥ⥫ ਡ஢.
 쥬   訫  ࠧ 襥. ( ,  ⥫ ந  , 稭  㭤.)  C++  1991   祭 ᨫ쭮,  楫 ⮩  -      ।⠢   ⭮ ᡮ୨ ४権 - 㦥  ⢥砫  ࠢ, 㫨஢ 15  .  1991  뫮 १ ।,    ++ 室 ணࠬ, 騥  ࠡ  .      ࠢ ⭮  ⭥  ,  ࠭ ᠫ  몠 Java  C#.  1991  ᫥  ꥪ⭮-ਥ஢ ணࠬ஢ 뫨 祬-   設⢠ ணࠬ⮢.    -   楯樨,  ﬨ,  ࠧ᭥   㦤  襩 ⥯, ⠫ ᪫祭, 蠡  饭 ணࠬ஢ ⥬.  1991    蠫  ୠ ஥஢.    㯮  㤭 㦤 ணࠬ ⥬.  1991  ࠡ  ଠ ⠭⮬ C++ ⮫쪮 稭, ⥯ ⮬ ⠭ 㦥 8 ,   ࠡ  ᫥饩 ᨥ.
⮡    ,  訫   ⮣   ᨫ ᥡ: < ᮢ ⮨  ࠪ⨪騬 ணࠬ⠬ C++  2005 ?>  १    ࠢ, 祭   .   砥    ணࠬ஢  ਬ 蠡  ࠢ ᠬ. ᪨ 蠡 ᭮  室 १  ⥪, ᪮    ᮢ६ C++ 室  .   祭 ⠪ ਠ  ணࠬ஢  稨 ᪫祭, ୠ ஥஢    ।⢠, ᠭ  㬥 <Technical Report 1> (TR1) ( 㬥 ᬠਢ  ࠢ 54). ਧ ⠪  䠪,  室  ⮤,   ࠡ   ⥬,   ਬ  .   ਠ ⮣  -  ⥬.  ⥫쭠  ᭮饩 ଠ樨  ண  ⠥ 㠫쭮, ⮬  襫 ᯮᮡ  ⮩   ଥ   (ᮮ⢥⢨  ࠢ ண  쥣     ਫ B).
 ࠫ   ᨫ ᤥ   ᨬ쭮 , , 筮,    筮. ᫨  ,  -  ਢ ࠢ   㭨ᠫ쭮 ਬ묨,   訩 ᯮᮡ  㫨஢    㦤  孨᪨ ᮢ 筮 ᭮, ,    㦤, , ᮮ . ᫨  㦨 訡  த - 孨᪨, ࠬ᪨, ⨯᪨,- ,-     ⮬.  ᪥ ᫥饣 ࠦ   㤮⢨ 㯮 ,      - ஡.
ᬮ       ⢮ ࠢ 㢥祭  55, 筮,  ᪠,  ᬮ७   ᪨ .  㫨஢  ⠪ ࠢ,  ᫥ ਤন    ਫ  ᥣ, 㤭, 祬     . ᫨    ।   ⮣,  ⮨   ,   㤮⢨  ᬮ.
稭   室  ᢥ ࢮ  ⮩ ,   祭 ,  ஬ ࠦ ࠢ 訡, 筥  孨᪨ .  㯥  Web-࠭ <Effective C++ Errata>   http://aristeia.com/BookErrata/ec++3e-errata.html. ᫨    㢥   ⮣ , ᮥ   ᯨ 뫪.  ᯮ   ⮣, ⮡  , , ⭮,  , ᫥   ᨮ쭮 ⥫쭮. ஡ .  http://aristeia.com/MailingList.

 㣫 
http://aristeia.com/
, ॣ, ५ 2005






  -  㭤⠫ ᭮ 몠,  ᮢᥬ 㣮 -  ஥஢  ॠ뢠 䥪⨢ ணࠬ.  ᮡ  ᠥ C++, ⭮ 砩 ப ﬨ  ࠧ⥫쭮.   C++  ࠢ쭮  ᯮ짮 ᯮᮡ ⠢ 㤮⢨.  ࠧ ஥   ।⢥ ࠦ  䥪⨢ ॠ. ⥫쭮 ࠭  ࠬ⭮ ॠ  ᮢ, 㭪権  蠡  ᤥ ணࠬ ⮩, ⨢ ⭮, 䥪⨢  ࠪ᪨  ᮤঠ饩 訡.  稨 । 몮 ᠭ 䥪⨢ ணࠬ  C++ - ᮢᥬ  㤭 .   ࠧ㬭 ᯮ짮 C++   , ᫮  ᮯ஢   ࠢ .
 ⮩  -  ,  ਬ C++ 䥪⨢.  宦  ⮣,   㦥   C++  몮 ணࠬ஢,  ⠪    ࠡ  .  । 襬  ४樨  ਬ ⮣ 몠, ᫥   ᤥ  ணࠬ 묨, 묨  ᮯ஢, ७ᨬ묨, 塞묨, 䥪⨢묨  ࠡ騬  ᮮ⢥⢨  ﬨ.
। ᮢ  ࠧ   ⥣ਨ:  ⥣ ஥஢  ࠪ᪮ ᯮ짮 ⤥ 몮 権. 㦤 ᮢ ஥஢ ਧ   ᤥ 롮  ࠧ묨 室  襭 ⮩     C++.  : ᫥  蠡? ⮥  ⮥ ᫥? ⮥ ᫥  ? 㭪樨-童  ᢮ 㭪樨? ।  祭   뫪?  ਭ ࠢ쭮 襭  ᠬ 砫, ᪮ ᫥⢨ 㤠筮 롮    ,   ⠭ ᫨誮 ,  ।뢠 㤥 㤭,   ண.
   筮 ,   ᤥ,   १⮢ 뢠 . 祭  ⨯    ᢠ?     㠫?  ᥡ   new, ᫨    筮 ? ᪫⥫쭮  ࠡ  ⠫, ᪮    猪 ⮫     ᭨  ணࠬ.       権.
筮,   ᫮   㪮⢮  C++. ॥,    55 ᮢ⮢ ( ࠢ),    ணࠬ  ஥.  ࠣ    ᨬ  ⠫,   設⢥  ४ 뫪.  ᥣ   , 砢  ⮣ ࠢ, ஥   ,  ⥬ ᫥  뫪, ⮡ ᬮ, 㤠   ਢ.
  ⠪     C++.   2, ਬ,  ᪠뢠  ࠢ쭮 ॠ樨 ஢, ஢  ஢ ᢠ,   ⮬ ।,   㦥 ,   㭪樨     .   ⥬  ⢮   C++.
 ⮩  - 뤥  ᯥ ணࠬ஢  C++,    㤥  .  㣨  뢠 ࠧ  몠.   ᪠뢠,   ஢  ᮡ  祭 䥪⨢ ணࠬ.  㣨    ⮬,  ⠢ ணࠬ ⪮஢.    -  ⮬,   ஡,     ﭨ 㦨.
   ६   ࠭稢 ⮫쪮 ⠭ C++.  ᯮ   ।⢠ 몠,  ᠭ  樠쭮 ⠭. ७ᨬ - 祢   ⮩ , ⮬ ᫨   ଥ-ᨬ ,   㣨 .
    ⮩   <  C++> - ⢥ ୮   쭮 ணࠬ  C++.  ࠢ -  ४  ⮬   ᯥ:  ᪠  㤠 ,   ⨯ 訡,   ᨬ쭮 䥪⨢,     㭪⮢   㭨ᠫ쭮 ਬ. ஥஢  ࠧࠡ⪠ ணࠬ ᯥ祭 -  ᫮ ,   뢠 ﭨ ࠭祭 ⭮ ᯥ祭, 樮 ⥬  ਫ, ⮬ 襥,    ᤥ,-  ।⠢ ४樨  襭 ⢠ ணࠬ.
᫨  ⥬᪨ 㤥 ᫥ ᥬ ४,  ⭮,  ⮫   묨 誠, ॣ騬   ++,    ࠢ  ᪫祭.  祬   ࠢ ਢ ᭥. -  ⠢ ᠬ   . 쪮 ,    ᭮ ⮣   ࠢ,  ᬮ , ᪮쪮  ᮮ⢥ 襩 ணࠬ  騬 ⮫쪮  ࠭祭ﬨ.
訩 ᯮᮡ ᯮ짮 ⮩  -  ⠩  C++, , 祬   ᥡ  ⠪,   ,  ᯮ짮    ᢮ 楫.  ਬ  ࠪ⨪  ਢ ࠢ ᮢ襭 㬥⭮,     ६  ⮨  ᮡ   稭 㯠 ४ ⨬ ᮢ⠬.



ନ

 让 ᫮ਪ C++,     ணࠬ. 騥 ନ 筮 , ⮬   㡥,     .
 (declaration) ᮮ頥    ⨯ 祣-, ᪠  ⠫.  룫 ⠪:

extern int x; //  ꥪ
std::size_t numDigits(int number); //  㭪樨
class Widget; //  
template<typename T> //  蠡
class GraphNode; // (. ࠢ 42  ⮬,  ⠪ <typename>

,   뢠 楫 ᫮ x <ꥪ⮬>, ᬮ     ६ ஥ ⨯.    <ꥪ⠬>  ⮫쪮 ६ 짮⥫᪨ ⨯,    ਭ   .  ⬥⨬,  㭪 numDigits() 頥 ⨯ std::size_t,   ⨯ size_t  ࠭⢠  std.   ࠭⢮ ,  ஬ 室    ⠭⭮ ⥪ C++. , ᪮ ⠭⭠ ⥪ C (筥 , 89) ⠪   ᯮ짮  ணࠬ  C++, ᨬ, 㭠᫥  C (⠪  size_t),  ⢮  쭮 ⥪,  std,    ,  ᨬ  ⮣,   䠩 뫨 祭 ४⨢ #include.  ⮩   ।,    #include   䠩 C++.  祬  㯮ॡ std::size_t,    size_t.   㯮  ⠭⭮ ⥪  ⥪ ணࠬ,  筮 ᪠ 뫪  std, ,   ,  ⠪ ,  size_t, vector  cout, 室  ࠭⢥  std.  ਬ  ணࠬ  ᥣ  std, ⮬   ⨢ 砥   ᪮.
, size_t -  ᥣ-ᥣ । ४⨢ typedef ᨭ    ⨯,   C++ ᯮ  ࠧ த 稪 (ਬ, ⢠ ᨬ  ப ⨯ char*, ⢠ ⮢  ⥩ STL  . .).  ⠪ ⨯, ਭ 㭪ﬨ operator[]   (vector),  (deque)  ப (string). ⮬ ᮣ襭  㤥 ᫥   ।  ᮡ⢥ 㭪権 operator[]  ࠢ 3.
   㭪樨 㪠뢠  ᨣ,   ⨯ ࠬ஢  頥 祭.  ᪠,  ᨣ 㭪樨 -   ⨯. , ᨣன 㭪樨 numDigits  std::size_t(int), 묨 ᫮,  <㭪, ਭ int   std::size_t>. 樠쭮 । <ᨣ>  C++  砥 ⨯ 頥 㭪樥 祭,   ⮩   㤥 㤮 ,       ᨣ.
। (definition) ᮮ頥  ⠫,  饭  .  ꥪ । -   ,   뤥   .  㭪樨  蠡 㭪樨 । ᮤন ⥫ 㭪樨.  ।   蠡    童:

int x; // । ꥪ
std::size_t numDigits(int number) // । 㭪樨
{ // ( 㭪 頥 ⢮
std::size_t digitsSoFar = 1; //    ᢮ ࠬ)
while((number /= 10) != 0) ++digitsSoFar;
return digitsSoFar;
}
class Widget { // । 
public:
Widget();
~Widget();
...
};
template<typename T> // । 蠡
class GraphNode {
public:
GraphNode();
~GraphNode();
...
};

樠 (initialization) -   ᢠ ꥪ 砫쭮 祭.  ꥪ⮢ 짮⥫᪨ ⨯ 樠 믮 ࠬ.   㬮砭 (default constructor) -  ,    맢  㬥⮢.      ࠬ஢ ,   祭  㬮砭   ࠬ:

class A {
public:
A(); //   㬮砭
};
class B {
public:
explicit B(int x = 0; bool b = true); //   㬮砭,
}; // .  ᭥
// 祢 ᫮ "explicit"
class C {
public:
explicit C(int x); //    
// 㬮砭
};

 ᮢ B  C   祢 ᫮ explicit ().  ।頥  ᯮ짮   ८ࠧ ⨯,   頥 ਬ, ᫨ ८ࠧ 㪠 :

void doSomething(B bObject); // 㭪 ਭ ꥪ ⨯ B
B bObj1; // ꥪ ⨯ B
doSomething(bObj1); // ଠ쭮, B । doSomething
B bObj(28); // ଠ쭮, ᮧ B  楫 28
// (ࠬ bool  㬮砭 true)
doSomething(28); // 訡! doSomething ਭ B,
//   int,    
// ८ࠧ  int  B
doSomething(B(28)); // ଠ쭮, ᯮ 
// B   ८ࠧ (ਢ)
// int  B (. ࠢ 27 ଠ
//  ਢ ⨯)

,   explicit, 筮  ।⥫, ⮬  । 믮 ࠬ  ८ࠧ ⨯ ( ⥫). ᫨  ᭮⥫쭮 稭  ᯮ짮 ஢   ८ࠧ ⨯,  ᥣ   explicit.    ਤন ⮣  ਭ樯.
 ,   ।饬 ਬ ਢ 뤥.     ᯮ짮 ⠪ 뤥, ⮡ ભ   ਠ. (  뤥  ,   ⮫쪮 ⮬,   ,  룫廊 ᨬ筮.)
 ஢ (copy constructor) ᯮ  樠樨 ꥪ 祭 㣮 ꥪ ⮣  ᠬ ⨯,  騩  ᢠ (copy assignment operator) ਬ  ஢ 祭  ꥪ  㣮 - ⮣  ⨯:

class Widget {
public:
Widget(); //   㬮砭
Widget(const Widget& rhs); //  ஢
Widget& operator=(const Widget& rhs); // 騩  ᢠ
...
};
Widget w1; // 맮   㬮砭
Widget w2(w1); // 맮  ஢
w1 = w2; // 맮  ᢠ
// ஢

 ⥫,   , 宦  ᢠ, ⮬  ᨭ⠪ <=> ⠪   ᯮ짮  맮  ஢:

Widget w3 = w2; // 맮  ஢!

 ,  ஢  ⫨  ᢠ. ᫨  ꥪ । ( w3  ᫥ ।),   뢠 ,     ᢠ. ᫨    ꥪ  ᮧ (  <w1=w2>),    ਬ   - ᢠ.
 ஢ - ᮡ  㭪, ⮬   ।,  ꥪ ।  祭. ਬ, ᬮਬ ᫥騩 ࠣ:

bool hasAcceptableQuality(Widget w);
...
Widget aWidget;
if (hasAcceptableQuality(aWidget)) ...

ࠬ w । 㭪樨 hasAcceptableQuality  祭, ⮬  ਢ ਬ 맮 aWidget   w. ஢ ⢫ ஬ ஢   Widget.  ।  祭 砥 맮  ஢. (, ண , । 짮⥫᪨ ⨯  祭 -  . 筮 訩 ਠ - ।  뫪  ⠭, ஡ . ࠢ 20.)
STL - ⠭⭠ ⥪ 蠡 (Standard Template Library) -   ⠭⭮ ⥪,  ⥩஢ (  vector, list, set, map  . .), ஢ (  vector<int>::iterator, set<string>::iterator  . .), ⬮ (  for_each, find, sort  . .)  ᥩ 易  ⨬ 㭪樮쭮.   祭 ப ᯮ ꥪ-㭪樨 (function objects),   ꥪ, 騥 ᥡ  㭪.  ꥪ ।⠢ ᠬ,   ॣ㦥  맮 operator(). ᫨     STL,  ,  饩 , - ⮩ 㪮⢮, 饭 ⮩ ⥬,  ⥪ STL ⮫쪮 㤮,   ᯮ짮  २⢠ 뫮  ⥫쭮. ⮨   ࠡ  ,   ᠬ  .
ணࠬ⠬, 襤訬  C++  몮 த Java  C#,   ࠭ ⨥ । .  ࠧ 稭   権  C++ ⢨⥫쭮  ।:    㢥७ ।᪠,  ந  ६ ᯮ.   ਬ ⠪ த:

int *p = 0; // p - 㫥 㪠⥫
std::cout << *p; // ࠧ묥 㫥 㪠⥫
char name[] = "Daria" // name - ᨢ  6 (  
// 騩 !)
char c = name[10]; // 㪠 ࠢ쭮  ᨢ
// ஦ । 

 ભ,  १ ।   ।᪠      쬠 ,  ணࠬ  C++  ,  ணࠬ  ।    ᮤন ⪮ ᪠.  ࠢ: ⠪ ணࠬ    ⪨ ,   ⮣   ᤥ.  ⭮,   㤥  ᥡ -ࠧ:  ଠ쭮,  ਩ ,   -  뤠 ࠢ १.  ணࠬ  C++ ਤন ࠢ -  । .  ⮩      㪠뢠,   ᤥ.
 ନ,    ணࠬ⮢, 襤  㣨 몮,-  䥩.  Java . NET-ᮢ⨬ 몠 䥩   몠,   C++ 祣  ,   ࠢ 31 ᬠਢ ஥ ਡ.   ᯮ ନ <䥩>,  筮    ᨣ 㭪権, 㯭 童  (< 䥩>, <饭 䥩>, < 䥩>)  ࠦ, ⨬  ⢥ ࠬ஢ ⨯  蠡 (. ࠢ 41).    䥩ᮬ    楯 ஥஢.
⨥  -    , ᯮ騩 ᠭ   (筮 १ 䥩). , ਬ, ⠬ 㭪樨   짮⥫:  ,  뢠 㭪 (   ),  ⠪ ,    ᮯ஢ ⠪ . ⠬   蠡   ணࠬ, ᯮ騥    蠡,  ࠢ ணࠬ,    ᮯ஢  .   室  ,  筮    ணࠬ⮢, ᪮       㦤  ⢮  ࠧࠡ⠭ 䥩ᮬ. ,   , ⠪ 樨 㯭.
,   ਢ몫 㬠  ,    㡥   室   , ᪮쪮  .   殢,  ᠬ -  ணࠬ ᯥ祭, ஥ ࠧࠡ뢠 - 㣮.   ⥫ , ⮡   稫  ࠡ?  ⮣, ࠭      ,  ᠬ ⠭ ⮬ ᮡ⢥  (  㤥 ᯮ짮 , ᠭ ),  ⮣ 業,   ࠧࠡ⪥ 䥩ᮢ 㦭    ⮢.
 ⮩       ࠧ稥  㭪ﬨ  蠡 㭪権,  ⠪  ᠬ  蠡 ᮢ.   砩,  ,  ࠢ  ,  ࠢ   㣮.  ,    ⠪,   ࠧ稥  ᠬ, 㭪ﬨ  蠡,   ஦   㭪樨.



襭  

 ⠫ 롨 ᫥   ꥪ⮢, ᮢ, 㭪権, 蠡  . .,  ᥬ⨪  ਤ㬠       祢. ਬ,   ᯮ  ࠬ஢  lhs  rhs.    ᮮ⢥⢥ < > (left-hand side)  <ࠢ > (right-hand side).   筮 㯮ॡ  㭪, ॠ  ,   operator==  operator*. ਬ, ᫨ a  b - ꥪ, ।⠢騥 樮 ᫠,  ᫨ ꥪ  Rational  ६   㭪樨-童 operator*() ( 砩 ᠭ  ࠢ 24),  ࠦ

a*b

⭮ 맮 㭪樨:

operator*(a, b);

 ࠢ 24   operator* ᫥騬 ࠧ:

const Rational operator*(const Rational& lhs, const Rational& rhs);

 ,  ࠭ - a -  㭪樨 뢠 lhs,  ࠢ - b - rhs.
 㭪権-童 㬥     ।⠢ 㪠⥫ this,  ⢥ ⠢訩 ࠬ   뢠 rhs. ,  ⨫     㭪権-童  Widget  ਬ . <Widget>   祣.   , ஥   ᯮ  ⮣, ⮡ -  ਬ .     ⭮襭  ⠬ ࠢ (⠬), ਬ塞  ᪨ 䥩 (GUI).
   㪠⥫, ᫥ ᮣ襭,  ᮮ⢥⢨   㪠⥫  ꥪ ⨯ T 뢠 pt (<pointer to T>).   ਬ:

Widget *pw; // pw = 㪠⥫  Widget
class Airplane;
Airplane *pa; // pa = 㪠⥫  Airplane
class GameCharacter;
GameCharacter *pgc; // pgc = 㪠⥫  GameCharacter

宦 ᮣ襭 ਬ   뫮: rw   뫪  Widget,  ra - 뫪  Airplane.
   㭪樨-童  ᯮ  mf.



筮

 ᠬ 몥 C++  ।⠢  ⮪ (threads),     - 堭 ࠫ쭮 ᯮ.   ⭮   ⠭⭮ ⥪ C++. 묨 ᫮,  窨 ७ C++  ணࠬ  .
  .   ⮩     २⢥  ⠭⭮, ७ᨬ C++,   ஢  䠪,  ᭮ ⭮⥫쭮 ⮪ - ॡ,   ⠫  ணࠬ. ਧ  䫨  ⠭ C++  ॠ쭮,   ⬥  砨,  ᬠਢ 樨  맢 ஡  ࠡ  筮 ।.   㬠,      筮 ணࠬ஢  C++.  .  ᬠਢ  ࠧ  ਫ,   ஢ ⢮ 筮  ࠫ ⬥  砨,  ணࠬ, 騥  ணࠬ,  ᫥  ᮢ⠬  ஦.
᫨     楯樥 筮    ⮩ ⥬,       ⭮騥   砭.  ⨢ 砥   ,   ਨ -  , 祬 ஬   ,  室 , ᫨  ᮡࠥ ᯮ짮 C++  ᠭ  ணࠬ.



⥪ TR1  Boost

뫪  ⥪ TR1  Boost  㤥   殮 ᥩ ⮩ .    饭 ⤥쭮 ࠢ (54 - TR1  55 - Boost), ,  ᮦ,  室  ᠬ  .      אַ ᥩ,  ᫨  ।⠥    浪,    ,  ᫥騥 砭  ,  祬  :
 TR1 ("Technical Report 1") -  ᯥ䨪  㭪樮쭮,   ⠭ ⥪ C++.  ଫ    蠡 ᮢ  㭪権, ।祭  ॠ樨 -⠡, <⥫㠫> 㪠⥫  ⮬ 뫮, ॣ ࠦ   㣮.   TR1 室  ࠭⢥  tr1, ஥   ࠭⢮  std.
 Boost -  ࣠  Web-ᠩ (http://boost.org),  ஬ । ७ᨬ, ⥫쭮 ஢७ ⥪ C++   室 .   TR1   ࠡ, 믮 Boost,    ,  ⠢騪 ஢   TR1  ਡ⨢ C++, Web-ᠩ Boost 㤥 ⠢  ࠧࠡ稪  筨 ॠ権 TR1. Boost ।⠢ , 祬 祭  TR1,    砥    .




 1
砩  C++


ᨬ   ணࠬ஢,  ⮣ ⮡ ᢮  C++, ॡ ஥ ६.     祭 ப  ⥩,  ⮡ ᯮ짮  䥪⨢, 㦭 ᪮쪮  ᢮ ᯮᮡ 諥.   ࠧ  ਧ    ⮬,  -    묨, - - ,    魠 ᠬ  頬.



ࠢ 1: ⭮  C++    몮

砫 C++   몮 C    ꥪ⭮-ਥ஢ ।.  ࢮ砫쭮  C++ (<C  ᠬ>) ࠦ  .
  ⮣   ⠭   ५,    ࠧ,   砫   ⥣ ணࠬ஢, 室騥  ࠬ C  ᠬ. ᪫祭 ॡ 㣮 室  ஢ 㭪権 (. ࠢ 29).    ।⠢  ஥஢ ணࠬ (. ࠢ 41),  ⥪ STL । 室  塞,   ࠭   ᥡ ।⠢.
 C++ -   ணࠬ஢  ᪮쪨 ࠤ, ন騩 楤୮, ꥪ⭮-ਥ஢, 㭪樮쭮, 饭  ⠯ணࠬ஢.      C++ ࠢ 㬥⮬,   ਢ  ⥫⢮.   ४樨  <ࠢ쭮 ਬ>  ᪫祭.     ⠪ 몥?
 ᥣ ਭ C++    ,    易 몮.  । ⤥쭮 몠 ࠢ 筮 ,    .    室   몠  㣮, ࠢ  . ⮡ 㢨   C++,   ᯮ  ᭮ 모.  ,  ᥣ :
 C.  㡨 ᢮ C++   ᭮  C. , ।, ९, ஥ ⨯ , ᨢ, 㪠⥫  . .-   諮  C.    C++ ।⠢  襭      ࠧ 堭, 祬 C (ਬ . ࠢ 2 - ୠ⨢ ९  13 - ਬ ꥪ⮢  ࠢ ᠬ),    筥 ࠡ  ⮩  C++,     C,  ,  ࠢ 䥪⨢ ணࠬ஢ ࠦ  ࠭祭 ࠪ 몠 C:  蠡,  ᪫祭,  ॣ㧪  . .
 ꥪ⭮-ਥ஢ C++.   C++ ।⠢ , 祬  <C  ᠬ>,    , , ᫥, 䨧, 㠫 㭪樨 (᪮ 뢠)  . .    C++,  ன  襩 ⥯ ਬ ᪨ ࠢ ꥪ⭮-ਥ஢ ஥஢.
 C++  蠡.   C++ 뢠 饭 ணࠬ஢,   設⢮ ணࠬ⮢  .  ⥯ ஭뢠 C++ ᭨ ,  ਧ 襣 ⮭  ணࠬ஢ 㦥 ⠫ 祭 権, ᫨  蠡 (ਬ, . ࠢ 46  ८ࠧ ⨯  맮 蠡 㭪権). ᪨ 蠡,  ᢮ , த ᮢ襭  ࠤ ணࠬ஢: ⠯ணࠬ஢ 蠡 (template metaprogramming - TMP).  ࠢ 48 ।⠢  TMP,  ᫨    㡥 䠭⨪ 蠡,    稭 १୮ 뢠  ⮬. TMP  ⭥  ᠬ ࠭ ਥ ணࠬ஢  C++.
 STL. STL - , 筮, ⥪ 蠡,  祭 ᯥ樠஢. ਭ   ᮣ襭 ⭮⥫쭮 ⥩஢, ஢, ⬮  㭪樮 ꥪ⮢    ᮡ,  蠡  ⥪  ந  -㣮.   ⥪ STL,  易 ᫥  ᮣ襭.
    몠   㤨, ᫨   ,  ᮮࠦ 䥪⨢ ணࠬ஢ ॡ    ⥣  ४祭   몠  㣮. ਬ,  ஥ ⨯ ( ⨫ C) । ࠬ஢  祭  饬 砥  䥪⨢, 祬 ।  뫪,  ᫨  ணࠬ  ꥪ⭮-ਥ஢ ⨫,  -  । 짮⥫ ஢  ஢ ।  뫪  ⠭ 筮 ⠭  䥪⨢.  ᮡ  ⭮   <C++  蠡>, ⮬  ⠬  筮    ࠭ ⨯ ꥪ⮢,  묨  .    諨  ᯮ짮 STL,   ஥ ࠢ C  ।  祭 ⠭ 㠫, ⮬    㭪樮 ꥪ ᬮ஢ १ 㪠⥫ C. (஡  롮 ᯮᮡ । ࠬ஢ . ࠢ 20.)
 ࠧ, C++   த 몮  ⢥ ஬ ࠢ.  -  몮,   ᢮ ᮡ⢥묨 ᮣ襭ﬨ. ᫨  㤥    몠,  㦨,   C++  .

 ᫥ 
 ࠢ 䥪⨢ ணࠬ஢   ᨬ   C++,   ᯮ.



ࠢ 2: ।⠩ const, enum  inline ᯮ짮 #define

 ࠢ  뫮   < ।⥫쭥 ९>, ᪮ #define    ⭮   C++.  ⮬  砥 ஡. ᬮਬ ⮩ ਬ; ஡  - த:

#define ASPECT_RATIO 1.653

᪮  ASPECT_RATIO  ⠪       㤠 ९஬  ⮣,   㯨  ࠡ . ᫨  ந,   ASPECT_RATIO    ⠡ ᨬ. ⮬  室 樨   訡 ( ᮮ饭   㤥 㯮 祭 1.653,   ASPECT_RATIO).  맮 ⠭. ᫨  ASPECT_RATIO 뫮 ।  筮 䠩,  ᠫ  ,     㤥 , 㤠 﫮 祭 1.653,   ᪨ ⢥   ६.   ஡     ⫠, ᪮ ࠭   㤥 ⢮  ⠡ ᨬ.
襭 ⮨    ⠭⮩:

const double AspectRatio = 1.653; // , ᠭ 訬 㪢,
// 筮 ਬ  ᮢ,
// ⮬  訫  

 몮 ⠭⮩, AspectRatio   , ⢥, 頥  ⠡ ᨬ.  ⮬   砥 ᯮ짮 ⠭  饩 窮 (  ⮬ ਬ)    , 祬  ᯮ짮 #define.   ⮬,  ९, ᫥ ⠢   ASPECT_RATIO 稭 1.653, ᮧ ⢮  1.653  ꥪ⭮ ,   ६  ᯮ짮 ⠭   த    ⮣ 祭.
  #define ⠭⠬ 㦭    ᮡ .  ᠥ ⠭ 㪠⥫. ᪮ । ⠭ 筮    䠩 (   砥  ⢮ ࠧ 室 䠩), , ⮡ ᠬ 㪠⥫    祢 ᫮ const,     const ⮣,    㪠뢠. ਬ, ⮡   筮 䠩 ⠭ ப ⨯ char*, ᫮ const 㦭  :

const char * const authorName = "Scott Meyers";

 ஡  魮  ਬ ᫮ const, ᮡ  離  㪠⥫ﬨ, . ࠢ 3.  㦥 ᥩ ⮨ ,  ꥪ ⨯ string 筮 ।⥫쭥 ᢮ த⥫ - ப ⨯ char *, ⮬ authorName  । ⠪:

const std::string authorName("Scott Meyers");

஥ 砭 ᠥ ⠭, 塞  ⠢ . ⮡ ࠭  ⢨ ⠭ ᮬ, 室 ᤥ  童 ,  ⮡ ࠭஢,   ⮫쪮   ⠭, ॡ ᤥ  ᪨ 童:

class GamePlayer {
private:
static const int NumTurns = 5; //  ⠭
int scores[NumTurns]; // ᯮ짮 ⠭
...
};

,    ,-   NumTurns,    ।. 筮 C++ ॡ, ⮡  ।⠢﫨 ।  ᥣ,  ᯮ,     ⠭,   ᪨   ஥ ⨯ (  楫, ᨬ, 㫥᪨) -  ᪫祭  ࠢ.       ⠥   ⠪ ⠭,    ᯮ짮   ।⠢ ।. ᫨   㦭    ᫨   ⠨  稨 ।,    - :

const int GamePlayer::NumTurns; // । NumTurns; . ,
// 祬  㪠뢠 祭

    䠩 ॠ樨,     䠩. ᪮ 砫쭮 祭 ⠭  ।⠢ ⠬,    (  NumTurns 樠஢ 祭 5  ),   窥 ।  砫쭮 祭  ॡ.
⬥⨬, ,       ⠭ ।⢮ #define, ⮬  #define  뢠  ⢨.  ⮫쪮  ।,  ⠥  ᨫ  ᥩ ⠢襩  㥬  (᫨ ⮫쪮 -    #undef).  ,  ४⨢ #define ਬ  ⮫쪮   ⠭  ,      ᯮ짮  ᯥ祭     뫮 樨,   ਤ  ࠦ <private #define> .    ६ ⠭ -童   㫨஢, ਬ஬  㦨 NumTurns.
    ন   ᨭ⠪, ⠪    ࠭  몠 뫮 饭  祭 ᪨ 童   ६ .  ⮣, 樠   ᪠ ⮫쪮  楫 ⨯   ⠭. ᫨ 襯ਢ ᨭ⠪  ࠡ⠥,  砫쭮 祭 ᫥   ।:

class CostEstimate {
private:
static const double FudgeFactor; //  ᪮ ⠭
... //  - 頥  䠩 
};
const double // । ᪮ ⠭
CostEstimate::FudgeFactor = 1.35; //  - 頥  䠩 ॠ樨

筮 祣    ॡ. ⢥ ᪫祭 㦨 ⮣,   樨  室 ⠭. ਬ,   ᨢ GamePlayer::scores  㦭  ࠧ ᨢ. ⮡ ࠡ  ஬, 訡筮 騬 樠஢ ᪨ 楫 ⠭  ,  ᯮ짮 ᯮᮡ,    <  ᫥>.  ᭮  ⮬,  ६ 塞 ⨯  ᯮ짮 ⠬,   祭 ⨯ int, ⮬ GamePlayer  । ⠪:

class GamePlayer {
private:
enum ( NumTurns = 5 }; // "  ᫥" -  
// NumTurns ᨬ  祭 5
int scores[NumTurns]; // ଠ쭮
...
};

 ਥ ⮨   ᪮쪨 稭. -,  <  ᫥>   ⭮襭  宦  #define, 祬  ⠭,     ࠧ ,  㦭. ਬ,    ⠭,     ᫥,      #define. ᫨       뫪  - 楫 ⠭,  ਬ enum - 訩 ᯮᮡ  ⠪ ࠭祭. (஡  প ஥ ࠭祭   ਥ ஢  㧭  ࠢ 18).  ⮬ ,  訥   뤥   ⠭ ꥪ⮢ 楫 ⨯ (᫨ ⮫쪮   ᮧ 㪠⥫  뫪  ꥪ),  ७  ⠪ 㯠,   , ,   祬.   #define, ᫥   ⠭ 稭  ⥫쭮 । .
 稭   <  ᫥>  ࠣ᪠.  ᯮ  祭  ணࠬ, ⮬ 㦭 㬥 ᯮ  ,     ⠫.  ,  ਥ - 㭤⠫쭠 孨, ਬ塞  ⠯ணࠬ஢ 蠡 (. ࠢ 48).
୥  ९. 㣮  砩 ࠢ쭮 ᯮ짮 ४⨢ #define - ᮧ ᮢ,  룫  㭪樨,   ६ 묨 室, 易묨  맮 㭪権.  ।⠢ ,  뢠  㭪 f c 㬥⮬, ࠢ ᨬ쭮   祭:

// 맢 f, ।  ᨬ  a  b
#define CALL_WITH_MAX(a,b) f((a) > (b) ? (a) : (b))

 ⮩ 窥 ᮤন ⠪  ⪮,    ᮢᥬ ⭮,   .
直 ࠧ  ᠭ       ⮬,   㬥 ᫥   ᪮.  ⨢ 砥   ⮫  ஡,  - 맮   ࠦ  ⢥ 㬥.   ᫨  ᤥ  ࠢ쭮, ᬮ,  ࠭   ந:

int a = 5, b = 0;
CALL_WITH_MAX(++a, b); // a 㢥稢 
CALL_WITH_MAX(++a, b+10); // a 㢥稢  ࠧ

ந室饥  max   ⮣,  祬  ࠢ!
 ,   㦤   , ⠪ ᨫ쭮 ⨢騬 ਢ筮 .  ⮤, 騩  ⠪  䥪⨢,   ᯮ짮 ९.   ⮬ ᯥ稢  ।᪠㥬 , ⠪  ஫ ⨯ 㬥⮢ ( ࠪ୮   㭪権).  १ ⨣ ਬ 蠡 ஥ (inline) 㭪樨 (. ࠢ 30):

template <typename T>
inline void callWithMax(const T& a, const T& b) // ᪮   ,
{ //   T,  ।
f(a > b ? a : b); //   뫪  const -
} // . ࠣ 20

 蠡  楫 ᥬ⢮ 㭪権,    ਭ  㬥   ⮣  ⨯  뢠 f  訬  .  室  ࠬ  ᪮  ⥫ 㭪樨,  㦭   ⭮ ᫥ ࠬ஢  . .  ⮣, ᪮ callWithMax -  㭪,   ࠭ ࠢ ⥩ ⢨  ஫ 㯠. ਬ,    ஥ 㭪樨, 饩  童 .       .
稥 const, enum  inline १ ᭨ ॡ  ९ (ᮡ  ⭮  #define),   ࠭  . ४⨢ #include ⠥ ⢥,  #ifdef/#ifndef த   ஫  ࠢ 樥.    ६ ⪠뢠  ९,  । ⮨ 㬠,      쭥襬.

 ᫥ 
   ⠭ ४⨢ #define ᫥ । ⠭ ꥪ  ᫥ (enum).
   㭪樨 ᮢ, । १ #define,  ਬ ஥ 㭪樨.



ࠢ 3: ,  ⮫쪮  ᯮ const


⥫쭮 ᢮⢮ 䨪 const ⮨  ⮬,   뢠 । ᥬ᪮ ࠭祭:  ꥪ   ஢,-   㤥 ஢  ࠭祭  . const  㪠   ணࠬ⠬,  । 稭  ⠢ .          ࠧ, ਧ뢠 ᥡ     ࠭ ⥬ ᠬ,  ࠭祭  㤥 襭.
祢 ᫮ const 㤨⥫쭮 ஭.  ᮢ   ᯮ짮   । ⠭  쭮    ࠭⢥  (. ࠢ 2),  ⠪  ᪨ ꥪ⮢ ( 䠩, 㭪樨  ).  ᮢ ⨬ ਬ    ᪨, ⠪   ᪨ -童.  㪠⥫  ᯥ஢,    ⠭ ᠬ 㪠⥫, ,    㪠뢠,   ,  㣮 (  ,  㣮):

char greeting[] = "Hello";
char *p = greeting; // ⠭ 㪠⥫,
// ⠭ 
const char *p = greeting; // ⠭ 㪠⥫,
// ⠭ 
char * const p = greeting; // ⠭ 㪠⥫,
// ⠭ 
const char * const p = greeting; // ⠭ 㪠⥫,
// ⠭ 

 ᨭ⠪  ⠪ 襭,   . ᫨ ᫮ const  ᫥  窨, ⠭  ,   㪠뢠 㪠⥫; ᫨ ࠢ,  ᠬ 㪠⥫  ⠭. , ᫨  ᫮ const    ஭,  ⠭⭮  ,  㣮.
 ,   㪠뢠,- ⠭,  ணࠬ ⠢ const । 䨪஬ ⨯. 㣨 - ᫥ 䨪 ⨯,  । 窮. ᪮ ࠧ  , ⮬ ᫥騥 㭪樨 ਭ ࠬ   ⮣  ⨯:

void f1(const Widget *pw); // f1 ਭ 㪠⥫ 
// ⠭ ꥪ Widget
void f1(Widget const *pw); //   ᠬ  f2

᪮  ॠ쭮    , ᫥ ਢ몠   ⮩,   㣮.
 STL ᬮ஢  ᭮ 㪠⥫, ⮬ iterator  ᥡ   㪠⥫ T*.  const-   const-㪠⥫ (   T* const):     㪠뢠  - 㣮,  ,    㪠뢠,   ஢. ᫨    ,  㪠뢠   ,  饭 ஢ (  STL- 㪠⥫ const T*),    ⠭ :

std::vector<int> vec;
...
const std::vector<int>::iterator iter = // iter ࠡ⠥  T* const
vec.begin();
*iter = 10; // Ok,  ,  
// 㪠뢠 iter
++iter; // 訡! iter ⠭
std::vector<int>::const_iterator citer = // citer ࠡ⠥  const T*
vec.begin();
*citer = 10; // 訡! *citer ⠭
++citer; // ଠ쭮, citer 

    ਬ const 易  ﬨ 㭪権.  ⮬ 砥 const  ⭮  頥 㭪樥 祭,  ⤥ ࠬࠬ,   㭪権-童 -    㭪樨  楫.
᫨ 㪠   㭪樨,   頥 ⠭⭮ 祭,   㬥 ⢮ 訡  ᪨ ணࠬ,  ᭨ ஢ ᭮  䥪⨢. ਬ, ᬮਬ  㭪樨 operator*  樮 ᥫ,   ࠢ 24:

class Rational {:}
const Rational operator*(const Rational& lhs, const Rational& rhs);

 ணࠬ 㤨,  㢨 ⠪ . 祬 १ 㭪樨 operator*   ⠭ ꥪ⮬? ⮬   ⨢ 砥 짮⥫ 稫    ,    㣠⥫⢮  ࠢ ᫮  :

Rational a, b, c;
:
(a*b)=c; // ᢠ ந a*b!

  ,    ணࠬ 諮    ᢠ 祭 ந  ᥫ,   筮 ᪠,   ⠪    ᬮ. 筮 ⮩ ⪨ ( ᫮,  ⨯   ८ࠧ  bool):

if (a*b = c)... //    ࠢ!

    ᮢ襭 ४, ᫨  a  b  ஥ ⨯.   ਥ ⢠ 짮⥫᪨ ⨯  ᮢ⨬  ஥묨 (. ⠪ ࠢ 18),   ᢠ 祭 १ ந  ꥪ⮢ ।⠢  쬠   ᮢ⨬. ᫨  ,  operator* 頥 ⠭⭮ 祭,  ⠪  ⠭ .  祬   㯠.
 ⭮襭 㬥⮢  䨪஬ const 㤭 ᪠ - ;   ᥡ   ⠭ const-ꥪ. ,  ,   䨪. ᫨ ஢ 㬥   ꥪ  室,    const.  ᥣ- ਤ   ᨬ,   । ᠤ 訡 ⨯ <⥫  ==,  ﭭ ⠫ => ( 祬  ਢ,  ⮫쪮  ).



⠭ 㭪樨-童

祭 䨪 const   㭪権-童 - ।,     뢠  ⠭ ꥪ⮢.  㭪樨-童    稭. -,    䥩 ,   ࠧ ,  㭪樨  ஢ ꥪ,   . -,  ᯥ稢  ࠡ  ⠭묨 ꥪ⠬.  祭   ᠭ 䥪⨢ , ⮬ ,    ࠢ 20,   ᭮ ᯮᮡ  ந⥫쭮 ணࠬ  C++ - । ꥪ  뫪  ⠭.   孨 㤥 ࠡ ⮫쪮  砥,  㭪樨-童  㫨஢ ⠭묨 ꥪ⠬   䨪஬ const.
 ᪠  ,  㭪樨, ⫨騥 ⮫쪮 稥 const  ,   ॣ㦥. , ,  ᢮⢮ C++. ᬮਬ , ।⠢騩  ⥪:

class TextBlock {
public:
...
const char& operator[](std::size_t position) const // operator[] 
{return text[position];} // ⠭ ꥪ⮢
char& operator[](std::size_t position) // operator[] 
{return text[position];} // ⠭ ꥪ⮢
private:
std::string text;
};

㭪 operator[]   TextBlock  ᯮ짮 ᫥騬 ࠧ:

TextBlock tb("Hello");
Std::cout << tb[0]; // 맮 ⠭⭮
//  TextBlock::operator[]
const TextBlock ctb("World");
Std::cout << ctb[0]; // 맮 ⠭⭮
//  TextBlock::operator[]

, ⠭ ꥪ  ᥣ   ॠ ணࠬ  १ ।  㪠⥫  뫪  ⠭. ਢ  ਬ ctb  쭮 ⢥.     ॠ:

void print(const TextBlock& ctb) //  ⮩ 㭪樨 ctb - 뫪
//  ⠭ ꥪ
{
std::cout << ctb[0]; // 맮 const TextBlock::operator[]
...
}

ॣ㦠 operator[]  ᮧ ࠧ ᨨ  ࠧ묨 頥묨 ⨯,   -ࠧ ࠡ뢠 ⠭  ⠭ ꥪ TextBlock:

std::cout << tb[0]; // ଠ쭮 - ⠥
// ⠭ TextBlock
tb[0] = 'x'; // ଠ쭮 - 
// ⠭ TextBlock
std::cout << ctb[0]; // ଠ쭮 - ⠥
// ⠭ TextBlock
ctb[0] = 'x'; // 訡!- 
// ⠭⭮ TextBlock

⬥⨬,  訡  易 ⮫쪮  ⨯ 祭, 頥 operator[]; ᠬ 맮 operator[] 室 ଠ쭮. 稭 訡 -  ⪥ ᢮ 祭 ꥪ ⨯ const char&, ⮬    ⠪ ⨯ 頥 ⠭⭮ ᨥ operator[].
⬥⨬ ⠪,  ⨯, 頥 ⠭⭮ ᨥ operator[],-  뫪  char,   ᠬ char. ᫨  operator[] 頫  char,  ᫥饥 ।  ᪮஢ :

tb[0] = 'x';

  ⥬,  頥 㭪樥 祭 ஥ ⨯ ஢ ४⭮.  ᫨   뫮 ⨬,  䠪,  C++ 頥 ꥪ  祭 (. ࠢ 20), 砫  ᫥饥: ஢  tb.text[0],   ᠬ 祭 tb.text[0].    , 祣  .
  ।孥  䨫㥬.  砥  㭪樨-童  ⠭⭮?   ப ࠭ : ⮢ ⠭⭮ (⠪ ⭠  䨧᪠ ⠭⭮)  ᪠ ⠭⭮.
஭ ⮢ ⠭⭮ ,  㭪-童 ⠭⭠ ⮣  ⮫쪮 ⮣,      -童 ꥪ ( ᪫祭 ᪨),         ꥪ. । ⮢ ⠭⭮  ⥬,   襭  㦨:    ᢠ 童 . ᪨, ⮢ ⠭⭮ -  ⠭⭮, ।  C++: 㭪-童  䨪஬ const   ஢ ᪨ -童 ꥪ,  ண  맢.
 ᮦ,  㭪樨-童,   ᥡ   ⠭⭮, 室 ⮢ .  ⭮, 㭪-童,   ,   㪠뢠 㪠⥫,    ᥡ  ⠭⭠.  ᫨ ꥪ ਭ ⮫쪮 㪠⥫,  㭪 ଠ쭮  ⮢ ⠭⭮,    ⠭ ࠦ.   ਢ   . ਬ, ।,     Text-Block,   ࠭  ப ⨯ char *  string, ᪮  室  ।  㭪樨, ᠭ  몥 C,   ,  ⠪ ꥪ ⨯ string.

class CtextBlock {
public:
...
char& operator[](std::size_t position) const // 㤠筮 ( ⮢
{ return pText[position]} // ⠭⭮)
//  operator[]
private:
char *pText;
};

 ⮬  㭪 operator[] (ࠢ쭮!)   ⠭⭠ 㭪-童,   頥 뫪  ७  ꥪ ( ⥬ 㦤  ࠢ 28). ⠢    ஭  ⬥⨬,  ॠ operator[]    pText.  १  ᯮ ᣥ   㭪樨 operator[].   ⢨⥫쭮  ⮢ ⠭⭮,   ,    ஢.  ᬮ,  ந室:

const CtextBlock cctb("Hello"); //  ⠭⭮ ꥪ
char &pc = &cctb[0]; // 맮 const operator[]  祭
// 㪠⥫   cctb
*pc = 'j'; // cctb ⥯  祭 "Jello"

ᮬ,  - ४⭮  ⮬,   ᮧ ⠭ ꥪ  । 祭, 뢠   ⮫쪮 ⠭ 㭪-童  ⥬     祭!
 ਢ    ᪮ ⠭⭮. ஭ ⮩ 䨫䨨 ⢥ত,  㭪樨-童  const  ஢   맢襣  ꥪ,  ⮫쪮 ⠪, ⮡ 짮⥫   ⮣ 㦨. ਬ,   CTextBlock   ஢  ⥪⮢    :

Class CtextBlock {
public:
...
std::size_t length() const;
private:
char *pText;
std::size_t textLength; // ᫥ ᫥ 祭 
// ⥪⮢ 
bool lengthIsValid; // ४⭠     
};
std::size_t CtextBlock::length() const
{
if(!lengthIsValid) {
textLength = std::strlen(pText); // 訡!  ᢠ
lengthIsValid = true; // 祭 textLength 
} // lengthIsValid  ⠭⭮
// 㭪樨-童
return textLength;
}

 ॠ length(), 筮 ,   ⮢ ⠭⭮, ᪮  ஢ 祭 童 textLength  lengthlsValid.     ६  ஭ ,  ⠭⭮ ꥪ⮢ CTextBlock   ஦.    ᮣᥭ.  ⠨  ⮢ ⠭⭮.  ?
襭 ⮥: ᯮ 䨪 mutable.  ᢮ ᪨ -童  ࠭祭 ⮢ ⠭⭮:

Class CtextBlock {
public:
...
std::size_t length() const;
private:
char *pText;
mutable std::size_t textLength; //  -童 ᥣ  
mutable bool lengthIsValid; // ஢,   ⠭
}; // 㭪-童
std::size_t CtextBlock::length() const
{
if(!lengthIsValid) {
textLength = std::strlen(pText); // ⥯ 冷
lengthIsValid = true; //   
}
return textLength;
}



  㡫஢  ⠭  ⠭ 㭪-童

ᯮ짮 mutable - ⥫쭮 襭 ஡,  ⮢ ⠭⭮    ࠨ,    ࠭  㤭⥩, 易  const. ਬ, ।⠢,  operator[]   TextBlock ( CTextBlock)  ⮫쪮 頥 뫪  ᮮ⢥騩 ᨬ,  ⠪ ஢ 室  । ᨢ, ⮪ ଠ  㯥 , ,  ஢ 楫⭮ . 饭 ᥩ ⮩    ᨨ 㭪樨 operator[] - ⠭  ⠭ ( ᫨ ,  ⥯   筮  ஥ 㭪樨 - . ࠢ 30) - ਢ  ⠪  㪫 :

class TextBlock {
public:
...
const char& operator[](std::size_t position) const
{
... // 믮 ஢ ࠭ ᨢ
... // ⮪஢   
... // ஢ 楫⭮ 
return text[position];
}
char& operator[](std::size_t position) const
{
... // 믮 ஢ ࠭ ᨢ
... // ⮪஢   
... // ஢ 楫⭮ 
return text[position];
}
private:
std:string text;
};

!   ⭮, 易  㡫஢ : 㢥祭 ६ 樨, ࠧ ணࠬ  㤮⢮ ᮯ஢. 筮,  ६    ஢ન 室  ࠭ ᨢ  祣  ⤥ 㭪-童 (⢥, ),   뢠  ᨨ operator[],  饭  ⮩ 㭪樨    㡫஢.
 ⢨⥫쭮 뫮  ⥫쭮 ॠ 㭪樮쭮 operator[]  ࠧ,  ᯮ짮   .     operator[]  뢠 .        뢠 ⠭⭮.
 ᠬ 砫 ⬥⨬, 뢠 ⠭⭮ .  ⨫ 楫 ࠢ 27 ⮬, ⮡ 㡥    ⮣,  㡫஢  - ⮦  .   砥 ⠭⭠  operator[]   筮   ᠬ,  ⠭⭠,  ⫨稥   -   ⢨ 䨪 const.  ⮩ 樨 뢠 const ᭮, ᪮ 짮⥫, 뢠騩 ⠭ operator[], ⠪     ⠭ ꥪ.   ⨢ 砥   ⠫  뢠 ⠭ 㭪. ⮬ ॠ ⠭⭮ operator[] ⥬ 맮 ⠭⭮ ᨨ -   ᯮᮡ  㡫஢ ,     ⮣ ॡ ᯮ짮 ஬ const_cast.  ਢ 騩  १ ,   ⠭ ᭥ ᫥ ⮣,   ⠥ ᫥騥  ᭥:

class TextBlock {
public:
...
const char& operator[](std::size_t position) const //  ,   ࠭
{
...
...
...
return text[position];
}
char& operator[](std::size_t position) const // ⥯ 
// 뢠 const op[]
{
return
const_cast<char&>( //  頥 ⨯
// op[] ᪫ const
static_cast<const TextBlock&>(*this) //  const ⨯
// *this
[position] // 맢 ⠭
); //  op[]
}
...
};

 ,  砥  ਢ,   .  ⨬, ⮡ ⠭ operator[] 뢠 ⠭,  ᫨  ⠭⭮  []  맮 operator[],   ४ᨢ 맮.   ᪮筮 ४ᨨ 㦭 㪠,   ⨬ 맢 const operator[],  אַ ᯮᮡ ᤥ   . ⮬  ਢ *this  ⨯ TextBlock& const TextBlock&. ,  믮塞 ਢ, ⮡  ⠭⭮!  ࠧ,    ਢ:   ⠭⭮ *this (⮡  맢 const operator[]),  ஥ - ᪫砥 const  ⨯ 頥 祭.
ਢ, ஥  const, 믮 ᭮ ८ࠧ ( ⠭⭮ ꥪ  ⠭⭮), ⮬  ᯮ㥬  ⮩ 楫 static_cast. ਢ , ஥ 뢠 const,   믮 ⮫쪮   const_cast, ⮬     롮. (ண , 롮 . ਢ  ⨫ C ⠪ ࠡ⠥, ,     ࠢ 27, ⠪ ਢ ।  ࠢ 饭. ᫨     ࠬ static_cast  const_cast, ⠩    ࠢ 27.)
 ᥣ 祣,  ⮬ ਬ  뢠 , ⮬ ᨭ⠪ 룫廊  ࠭. ,     ਧ   ,    㦭 䥪 -  㡫஢ ।⢮ ॠ樨 ⠭⭮ ᨨ operator[]  ନ ⠭⭮.    ⨦ 楫 諮 ᯮ짮 㪫 ᨭ⠪ᮬ,  ᬮ  ⮫쪮  ᠬ,  孨 ॠ樨 ⠭ 㭪権-童 १ ⠭ । 㦨 ⮣, ⮡  .
  㦭   ,      - ⥬ 맮 ⠭⭮ ᨨ  ⠭⭮ - ࠢ쭮. ,  ⠭⭠ 㭪-童 頥    ᪮ ﭨ ꥪ,  ⠭⭠   ⠪ ࠭⨩. ᫨  맮 ⠭ 㭪  ⠭⭮,    ,  ꥪ,    ஢, 㤥 .  祬 ⮣  ᫥ : ⮡ ꥪ  . ᪨, ⮡  㥬 ,  諮  ᯮ짮 const_cast  뢠 ⠭⭮ *this,    ਧ 㤠筮 襭. ⭠ ᫥⥫쭮 맮 - ⠪,  ᠭ ,- ᭠. ⠭⭠ 㭪-童   ,    ꥪ⮬, ⮬ 맮   ⠭⭮ 㭪樨-童 祬  ஧. ⮬-   ਬ塞  *this  static_cast, 뢠 ⠭⭮  ⮬  ந室.
  㦥 㯮  砫 ⮣ ࠢ, 䨪 const - 㤥᭠ .  㪠⥫  ஢;  ꥪ⮢,   뫠 㪠⥫,   뫪;  ࠬ஢ 㭪権  頥  祭;   ६,  㭪権-童 -  const   . ᯮ ,  ⮫쪮 .  ࠢ!

 ᫥ 
  祣-  䨪஬ const  ࠬ 㦨 訡. const  ᯮ짮  ꥪ⠬    ⢨,  ࠬࠬ 㭪権  頥 祭,  ⠪  㭪ﬨ-童  楫.
  ஢ ⮢ ⠭⭮,    ணࠬ஢, ਬ  ⠭⭮.
  ⠭  ⠭ 㭪樨-童 ,  ,  ॠ,  㡫஢   , ⠢ ⠭  뢠 ⠭.




ࠢ 4: ० 祬 ᯮ짮 ꥪ, 㡥,   樠஢

⭮襭 C++  樠樨 祭 ꥪ⮢   ࠭. ਬ, ᫨  :

int x;

   ⥪ ६ x 㤥 ࠭஢ 樠஢ 㫥,   㣨 - . ᫨  :

class Point {
int x, y;
};
...
Point p;

 童- ꥪ p   樠஢ (ﬨ),   - . ᫨  諨  C++  몠,  樠஢ ꥪ   ⢮,    .
⥭ 樠஢ 祭   稭 । .   ଠ ⠪ ⮥ ⢨,    樨஢ 祭  ⥭,  맢 ਩ ⠭ ணࠬ.     砩  ⮢,  ᯮ ७ ﭨ ꥪ,    뢠,   筮 ⮣  ਢ  ᭨  ணࠬ  ⥫쭮  訡  ⫠稪.
㫨㥬 ࠢ,  뢠,  樠 ꥪ ࠭,   .  ᮦ,  ࠢ 筮 ᫮ -   , ᫨誮 ᫮, ⮡  ⮨ . , ᫨  ࠡ⠥  C- C++ (. ࠢ 1)  樠  ⮨ ।   ६ ᯮ,   ࠭,   ந.  , 祬 ᮤন ᨢ ( C- C++)  易⥫쭮 樠,  ᮤন  ( STL- C++) 樠 ᥣ.
-, 訩 ᯮᮡ   ⠪ । 樨 - ᥣ 樠஢ ꥪ, ० 祬  ᯮ짮.  ꥪ⮢ ஥ ⨯,   童 ᮢ,  㦭  . ਬ:

int x = 0; // 筠 樠 int
const char * text = "ப  ⨫ C"; // 筠 樠 㪠⥫
// (. ⠪ ࠢ 3)
double d; // <樠> ⥭
std::cin >> d; //  室 ⮪

   ⠫  ⢥⢥  樠   . ࠢ ⮥: 㡥,    樠  ꥪ .
⮬ ࠢ  ᫥,     ᢠ  樠樥. ᬮਬ  , ।⠢饣   ᭮ :

class PhoneNumber {:}
class ABEntry { // ABEntry = "Address Book Entry"
public:
ABEntry(const std::string& name, const std::string& address,
const std::list<PhoneNumber>& phones);
private:
std::string theName;
std::string theAddress;
std::list<PhoneNumber> thePhones;
int numTimesConsulted;
};
ABEntry(const std::string& name, const std::string& address,
const std::list<PhoneNumber>& phones)
{
theName = name; //   ᢠ,   樠
theAddress = address;
thePhones = phones;
numTimesConsulted = 0;
}

,  १ ஦ ꥪ ABEntry  祭ﬨ,   ,      訩 室. ࠢ C++ ਢ,  童 ꥪ 樨 । 室  ⥫ .     ABEntry 童 theName, theAddress  thePhones  樠,   ᢠ 祭. 樠 ந室 ࠭:  ⮬᪨ 뢠   । 室  ⥫  ABEntry.   ᠥ numTimesConsulted, ᪮  童 ⭮  ஥ ⨯.     ࠭⨩ ⮣,    㤥 樠஢ । ᢠ.
訩 ᯮᮡ ᠭ  ABEntry - ᯮ짮 ᯨ᮪ 樠樨 童  ᢠ:

ABEntry(const std::string& name, const std::string& address,
const std::list<PhoneNumber>& phones)
:theName(name), // ⥯   - 樠樨
:theAddress(address),
thePhones(phones),
:numTimesConsulted(0)
{} // ⥫  ⥯ 

     ᠬ  १,   ।騩,   뢠  䥪⨢. , ᭮  ᢠ, ᭠砫 뢠   㬮砭  樠樨 theName, theAddress  thePhones,  ⥬ ࠧ ᢠ   祭,  ,  㦥 뫨 ᢮    㬮砭.  ࠧ,  ࠡ ஢  㬮砭  . 室  ᯨ᪮ 樠樨 童   ⮩ ஡, ᪮ 㬥  ᯨ᪥ 樠樨 ᯮ  ⢥ 㬥⮢ ஢  ࠧ 童-.  ⮬ 砥 theName ᮧ ஬ ஢  name, theAddress -  address, thePhones -  phones.  設⢠ ⨯ ⢥ 맮  ஢  䥪⨢ -    䥪⨢, 祬 맮 ஢  㬮砭  ᫥騬 맮 ஢ ᢠ.
 ꥪ⮢ ஥ ⨯ த numTimesConsulted  ࠧ  ⠬  樠樥  ᢠ,   ࠧ   樨஢  ।⢮ ᯨ᪠ 樠樨 童.  ᯨ᪨  ਬ  ⮣,  -童 樠 ࠬ  㬮砭:   ।  㬥⮢ ᮮ⢥饬 . ਬ, ᫨  ABEntry  ,  ਭ騩 ࠬ஢,     ॠ ਬ୮ ⠪:

ABEntry()
:theName(), // 맢   㬮砭  theName
:theAddress(), // ᤥ    theAddress   thePhones;
thePhones(), //   樠஢ 㫥 numTimesConsulted
:numTimesConsulted(0)
{}

᪮  ⮬᪨ 뢠   㬮砭  -童 짮⥫᪨ ⨯,     樠  ᯨ᪥ 樠樨 童,  ணࠬ  ਢ   .  ⭮, , ਤন ⨪ ᥣ   -童  ᯨ᪠ 樠樨,   ᥡ  室 ,  童  樠஢, ᫨  ய,   - . ਬ, ᪮ numTimesConsulted ⭮  ஥ ⨯,  ᪫祭   ᯨ᪠ 樠樨    । .
 ᯨ᮪ 樠樨  室 ᯮ짮,   ஥ ⨯. ਬ, -童,   ⠭⠬  뫪, 易  樠஢, ⠪      祭 ।⢮ ᢠ (. ⠪ ࠢ 5). ⮡  室 ,  -童   樠஢  ᯨ᪥ 樠樨,     易⥫쭮,    ᥣ.   易⥫쭮,   -  䥪⨢, 祬 ᢠ.
    ᪮쪮 ஢,     ᢮ ᮡ⢥ ᯨ᮪ 樠樨. ᫨    -童   ᮢ,  稥 讣 ᫠ ᯨ᪮ 樠樨 ஦ ⥫쭮 㡫஢  ( ᯨ᪠)   ( ணࠬ⮢).  ⠪      ᯨ᪠ 樠樨  -童,   ᢠ ࠡ⠥ ⠪ ,   樠, ६⨢ 樠   (筮 ) 㭪,  뢠  .  室   ᮡ , ᫨ 砫 祭   㦥  䠩   . ,  , 樠 童 ।⢮ ᯨ᪮ 樠樨  ।⥫쭠, 祬 ᥢ樠 ᢠ.
  ᯥ⮢ C++,    ,-  冷,  ஬ 樠  ꥪ⮢.  冷 ᥣ    :   樠 ࠭ ந (. ⠪ ࠢ 12),    童- 樠  ⮬ 浪,  ஬ . ਬ,   ABEntry 童 theName ᥣ 㤥 樠஢ , theAddress - , thePhones - 쨬,  numTimesConsulted - ᫥.  ୮   砥, ᫨  ᯨ᪥ 樠樨 童  ᫥  㣮 浪 (,  ᮦ,  饭). ⮡    㦤 祫, 饣  ணࠬ,    訡 ⭮ ந宦, ᥣ  -童  ᯨ᪥ 樠樨  ⮬ 浪,  ஬    .
⨢   樠樨 ꥪ⮢ ஥ ⨯,    童 ᮢ,  ᯥ稢 ࠢ 樠  ᮢ   -童 ।⢮ ᯨ᪮ 樠樨,   ⠭ ⮫쪮  ,  祬 㦭 㤥 㬠.    浪 樠樨  ᪨ ꥪ⮢,   ࠧ  ࠭樨.
⭥ᥬ  ⮩ ࠧ  ᥬ .
᪨ ꥪ   ,   ᪮஢,    ࠡ ணࠬ. ꥪ, ࠧ饭  ⥪   <>,  ᪨  ⭮. ᪨   ꥪ, ꥪ,    ⢨ ࠭⢠ , ꥪ,   祢 ᫮ static  ᮢ  㭪権,  ⠪   ⢨ ⤥쭮 䠩  室 ⥪⮬. ᪨ ꥪ,   㭪権,    ᪨ ꥪ (᪮    ⭮襭  㭪樨),   稥 뢠 묨 ᪨ ꥪ⠬. ᪨ ꥪ ⮬᪨ 㭨⮦  襭 ணࠬ,    室  㭪樨 main() ⮬᪨ 뢠  .
 ࠭樨 (translation unit) -  室 ,  ஦ ⤥ ꥪ 䠩. 筮   室 䠩   䠩, 祭   ४⨢ #include.
஡ ,  ,  ࠩ ,  ⤥쭮 㥬 室 䠩,    ᮤন,  ࠩ ,   ᪨ ꥪ (   ꥪ     ⢨ ࠭⢠ ,   䠩).    ⮬,  ᫨ 樠 쭮 ᪮ ꥪ ந室    ࠭樨,  ᯮ   㣮,  ⠪ ꥪ   樠஢   ᯮ짮, ᪮ ⭮⥫ 冷 樠樨 ᪨  ꥪ⮢, ।  ࠧ  ࠭樨,  ।.
ᬮਬ ਬ. ।,     FileSystem,   䠩  Internet ⫨稬묨  . ᪮   ।⠢    䠩 ⥬,    ᮧ  쭮  ⢨   ࠭⢥  ᮮ⢥騩  ᯥ樠 ꥪ:

class FileSystem { //  襩 ⥪
public:
...
std::size_t numDisks() const; //    㭪権-童
...
};
extern FileSystem tfs; // ꥪ  ᯮ짮 ⠬
// "tfs" = "the file system"

 FileSystem ।  ਢ, ⮬ ᯮ짮 ꥪ theFileSystem  ⮣,   㤥 ᪮஢, ਢ  ᪨ ᫥⢨.
 ।,   짮⥫ ᮧ , 뢠騩 ⠫ 䠩 ⥬. ⢥,   㤥 ᯮ짮 ꥪ theFileSystem:

class Directory { // ᮧ 짮⥫
public:
Directory( params );
...
};
Directory::Directory( params )
{
...
std::size_t disks = tfs.numDisks(); // ᯮ짮 ꥪ tfs
...
}

 ।,  짮⥫ 蠥 ᮧ ⤥  ꥪ  Directory, ।⠢騩 ⠫  ६ 䠩:

Directory tempDir( params ); // ⠫  ६ 䠩

 ஡ 浪 樠樨 ⠭ 祢: ᫨ ꥪ tfs  樠஢ ࠭, 祬 tempDir,   tempDir ⠥ ᯮ짮 tfs   樠樨.  tfs  tempDir 뫨 ᮧ ࠧ묨 쬨  ࠧ ६  室  ࠧ 室 䠩 -   ᪨ ꥪ, ।  ࠧ  ࠭樨.     㢥७,  tfs 㤥 樠஢ ࠭, 祬 tempDir?
 !  ࠧ : ⭮⥫ 冷 樠樨 ᪨  ꥪ⮢, ।  ࠧ  ࠭樨,  ।.    ᢮ 稭. । <ࠢ> 冷 樠樨  ᪨ ꥪ⮢ 㤭. 祭 㤭. ࠧ訬 㤭.   饬 砥 -  稨   ࠭樨   ᪨ ꥪ⮢, ᣥ஢ ⥬  ⨧樨 蠡 (  ᠬ   १⮬  ⨧樨 㣨 蠡) -  ⮫쪮  । ࠢ 冷 樠樨,  筮   ⮨ ᪠  砨,   冷  ਭ樯 । .
 , 讥   ஥ ணࠬ   ࠭  ஡. 㦭  ६   ᪨ ꥪ  ⤥ 㭪,  ன  㤥  ᪨.  㭪樨  뫪  ꥪ,    ᮤঠ.  ⥬ 뢠 㭪樨  ।⢥ 饭  ꥪ⠬. 㣨 ᫮,  ᪨ ꥪ  묨 ᪨ ꥪ⠬ (  ୠ ஥஢  㧭  ⮬ ᠭ ⨯ ॠ ୠ Singleton).
 室 ᭮  ⮬,  C++ ࠭:  ᪨ ꥪ 樠   ࠧ,  । ꥪ 砥  맮 ⮩ 㭪樨. ⮬ ᫨   אַ    ᪨ ꥪ⠬ 맮 㭪権,  뫪  ᯮ    ᪨ ꥪ,    㢥७,  뫪, 頥  㭪権,  뫠  樠஢ ꥪ. ⥫쭮 २⢮ 砥  ⮬,  ᫨    뢠 㭪, 㫨  ᪨ ꥪ,    ਤ   ᮧ  㭨⮦ ꥪ, 祣  ᪠  ॠ  ᪨ ꥪ.
   ਥ ਬ  ꥪ⠬ tfs  tempDir:

class FileSystem {...}; //  ࠭
FileSystem& tfs() //  㭪  ꥪ tfs,  
{ //  ᪮   FileSystem
static FileSystem fs; // ।  樠 쭮
// ᪮ ꥪ
return fs; //  뫪  
}
class Directory {...}; //  ࠭
Directory::Directory( params ) //  ࠭,   뫪  tfs
{ // 맮 tfs()
...
std::size_t disks = tfs().numDisks();
...
}
Directory& tempDir() //  㭪  ꥪ tempDir,
{ //   ᪮   Directory
static Directory td; // ।/樠 쭮
// ᪮ ꥪ
return td; //  뫪  
}

 ࠡ  ⮩ ஢ ணࠬ ⠪ ,  ࠭,  ᪫祭 ⮣,   tfs  tempDir  ⥯   tfs()  tempDir(). 묨 ᫮, ᯮ 뫪  ꥪ, 饭 㭪ﬨ,  ᯮ짮 ᠬ ꥪ⮢.
㭪樨,   ᮮ⢥⢨   奬  뫪, ᥣ : ।  樠஢  ᪨ ꥪ  ப 1     ப 2.  裡  ⨬     襭   ஥묨, ᮡ, ᫨   뢠 (. ࠢ 30).  㣮 ஭,  䠪,   㭪樨 ᮤঠ  ᥡ ᪨ ꥪ, ᫮  ਬ   ⥬.   㤠  : ⠭ ᪨ ꥪ  த -    - ।⠢ ஡  砥   ணࠬ ᪮쪨 ⮪.   , ਬ, 맢 ᠬ⥫쭮  㭪樨, 騥 뫪,  ⠯ ᪠ ணࠬ,   ࠡ⠥ ⮫쪮  ⮪.  ᪫ ।  室 樠樨.
筮, ਬ  㭪権,  뫪,  ।饭 ஡, 易  浪 樠樨,   ⮣,    ਭ樯 ࠧ㬭 冷 樠樨  ꥪ⮢. ᫨   ,  ஬ ꥪ A   樠஢ ०, 祬 ꥪ B,  ६ ᤥ 樠 A ᨬ  樠樨 B,    ஡ -  ! ᫨, ,  㤥  ⠪ ⮫᪨ 権,  ᠭ 奬 㦨   㦡,  ࠩ ,   ਫ.
 ࠧ, ⮡  ᯮ짮 ꥪ⮢   樠樨,  ᫥ ᤥ  . ࢮ:  樠஢  騥 童 ꥪ ஥ ⨯. ஥: ᯮ짮 ᯨ᪨ 樠樨 童   ⥩ ꥪ.  , :    ࠢ쭮 ஥஢ ஡ ࠭஢ 浪 樠樨  ᪨ ꥪ⮢, ।  ࠧ  ࠭樨.

 ᫥ 
 ᥣ  樠஢ ꥪ ஥ ⨯, ᪮ C++  , ⮫쪮  ᥣ.
   ⤠ ।⥭ ਬ ᯨ᪮ 樠樨 童 ।  ᢠ 祭  ⥫ .  -童  ᯨ᪥ 樠樨  ⮬  浪,      .
  ஡  浪 樠樨  ࠧ  ࠭樨,   ᪨ ꥪ 묨 ᪨ ꥪ⠬.




 2
,    ᢠ


      ।   ᪮쪮 ஢,    ᢠ.  㭪樨,  ⢥  樨 ᮧ  樠樨 ꥪ,  㭨⮦,  ⠪ ᢠ   祭. 訡   㭪 ਢ   騬   ᫥⢨  ࠦ    , ⮬    ᠭ ࠢ쭮.  饩   ࠢ  ணࠬ஢ 㭪権, ⠢ ᭮ ᮢ.



ࠢ 5:  㭪樨 C++ ᮧ  뢠 

 ⮩  ⠥  ?     C++. ᫨     ஢,  ᢠ   ᠬ⥫쭮,   ᤥ   .  ⮣, ᫨      ,   ⮬᪨ ᮧ   㬮砭.   㭪樨  묨  ஥묨 (. ࠢ 30). ਬ, ⠪ :

class Empty {};

 ᫥饬:

class Empty {
public:
Empty() {...} //   㬮砭
Empty(const Empty& rhs) {...} //  ஢
~Empty() {...} //  - . 
//  㠫 
Empty& operator=(const Empty& rhs) {...} //  ᢠ
};

 㭪樨 , ⮫쪮 ᫨  㦭,    砥,     . , ᫥騩  ਢ   ⮬᪮ 樨 ஬:

Empty e1; //   㬮砭;
// 
Empty e2(e1); //  ஢
e2 = e1; //  ᢠ

⠪,    㭪樨  ,    ?   㬮砭   -  ,    頥 㦥 , ਬ 맮 ஢  ஢  ᮢ  ᪨ -童. ⬥⨬,  ᣥ஢    㠫 (. ࠢ 7), ᫨ ⮫쪮     , ᫥饬 ,  ண  㠫  ( ⮬ 砥 㠫쭮 ᫥   ).
 ᠥ  ஢   ᢠ,  ᣥ஢ ஬ ᨨ    ᪨ 童  室 ꥪ  楫. ਬ, ᬮਬ 蠡 NamedObject,   樨஢   ꥪ⠬ ⨯ T:

Template<typename T>
class NamedObject {
public:
NamedObject(const char *name, const T& value);
NamedObject(const std::string& name, const T& value);
...
private:
std:string nameValue;
T objectValue;
};

᪮   NamedObject  ,   ⠭ ஢   㬮砭.  . , ᫨  ஥஢  ⠪,    易⥫쭮   । - 㬥,    㦭 ᯮ,   ந  襭   ᮡ⢥ 樠⨢      㬥⮢.
  NamedObject    ஢,   ᢠ, ⮬  ᣥ  ( 室). ᬮ  ᫥饥 㯮ॡ  ஢:

NamedObject<int>no1("Smallest Prime Number", 2);
NamedObject<int>no2(no1); // 뢠  ஢

 ஢, ᣥ஢ ஬,  樠஢ no2.nameValue  no2.objectValue, ᯮ nol.nameValue  nol.objectValue ᮮ⢥⢥.  nameValue  ⨯ string,   ⠭⭮  string   ஢, ⮬ no2. nameValue 㤥 樠஢ 맮  ஢ string  㬥⮢ nol.nameValue.  㣮 ஭, 童 NameObject<int>::objectValue  ⨯ int (᪮ T  int   ⨧樨 蠡),  int - ஥ ⨯, ⮬ no2.objectValue 㤥 樠஢ ⮢ ஢ nol.objectValue.
஢ ஬  ᢠ   Named-Object<int> 㤥  ᥡ  ࠧ, ,  , ᣥ஢ ஬   ᢠ  ᥡ ⠪,   ᠫ, ⮫쪮  ⮬ 砥,   १ 砥 ४  ᫥ .  ⨢ 砥   ᣥ operator=.
ਬ, ।,   NamedObject ।,   .  ,  nameValue - 뫪  string,  objectValue  ⨯ const T:

template<class T>
class NamedObject {
public:
//     ਭ const name, ᪮ nameValue -
// ⥯ 뫪  ⠭ ப.   㬥⮬ ⨯
// char* ᪫祭, ᪮  㦭 ப,    ᫠
NamedObject(std::string& name, const T& value);
... //   ࠭, ।,
//  operator=  
private:
std::string& nameValue; // ⥯  뫪
const T objectValue; // ⥯ const
};

ᬮਬ,  ந  ਢ  :

std::string newDog("Persephone");
std::string oldDog("Satch");
NamedObject<int> p(newDog, 2); //    ᠫ ,
//  ᮡ 䮭 ᮡࠫ
//  ᢮ ன  ஦
NamedObject<int> s(oldDog, 36); //    ( 
// ⢠) 뫮  ⥯ 36 
p = s; //   ந
//  묨-童 p?

। ᢠ  p.nameValue,  s.nameValue 뫠  ꥪ string,    ࠧ.   ந  童 p.nameValue  १ ᢠ?    뫠    ப,   s.nameValue,     ஢ 뫪? ᫨ ,  뢠 ᭮, ⮬  C++    ꥪ,   㪠뢠 뫪. ,  ,  ஢ ப,   뫠 童 p.nameValue,  ⮣  ஭ 㣨 ꥪ, ᮤঠ騥 㪠⥫  뫪   ப,     ⢮ ।⢥  ᢠ?     ᣥ஢ ஬  ᢠ?
⠫   , C++  ⪠뢠 ஢  . ᫨   ন ᢠ  , 饬  ᥡ 童-뫪,   ।  ᢠ ᠬ⥫쭮.  ࠧ   ᥡ  ᠬ, ᮤঠ騬 ⠭ 童 (⠪  objectValue  ஬ ਠ  NamedObject ). ஢ ⠭ 童 饭, ⮬   ,  㯠   樨  ᢠ. ஬ ⮣,   ⠭  ஢  ᢠ  ந , ᫨       ᢠ.  , ।,  ᣥ஢ ஬  ᢠ  ந ᮢ  ࠡ뢠   ᮢ (. ࠢ 12),   ⮬  筮    뢠 㭪樨-童,      饭.

 ᫥ 
    ஢     㬮砭,  ஢,  ᢠ  .



ࠢ 6:  頩  ஢ 㭪樨,    㦭

  த   ணࠬ ⥬, 㦨騥  ⥫쭮,  㦤  , ।⠢饬 , ⠢  த:

class HomeForSale {...};

   த  ᪠ ,   ꥪ 㭨 -  뢠 ,  筮 .  祬  ᮧ  ꥪ HomeForSale ᫥.   ᪮஢ ,  ।, 㭨쭮? ⮬ ⥫ , ⮡ ⪨ ᪮஢ ꥪ HomeForSale  ஢:

HomeForSale h1;
HomeForSale h2;
HomeForSale h3(h1); // ⪠ ᪮஢ h1 -
//   ஢!
h1 = h2; // ⪠ ᪮஢ h2 -
//   ஢!

, । ⠪   ⠪- . 筮, ᫨   , ⮡  ন । த 㭪樮쭮,     㭪権,   ॠ.   ஬ ஢  ஬ ᢠ  ⥣  ࠡ⠥, ᪮,  ᫥  ࠢ 5, ᫨    ,  -  ணࠬ ந ⪠  맢,   ᣥ  ⮬᪨.
宦  室 . ᫨  ᠬ    ஢   ᢠ,   ᣥ .    㤥 ন ஢.    ᠬ ந, ᫨    㭪樨 ᠬ⥫쭮.   楫 - । ஢!
  襭  ⮬,   ᣥ஢ ஬ 㭪樨  묨. ⮡ । ⮬᪮ ஢,     ᠬ⥫쭮,    ॡ, ⮡  뫨 묨.  ⠪    ஢   ᢠ 묨.   㭪-童,  ।頥 ஢  ஬,  ᤥ  ⮩,   - 뢠 .
奬  쭠, ⮬  㣨 童   㭪樨- -०  뢠  㭪樨. ᫨ ⮫쪮     , ⨢ ।.  ᫨ - 砩 맮 ⠪ 㭪,   ᮮ饭  訡  ⠯ .   -  㭪権-童 묨  ᮧ⥫ ⪠   ॠ樨 -  ࠧ  ᯮ  ।饭 ஢    ⥪ iostreams. ﭨ, ਬ,   ᮢ ios_base, basic_ios  sentry  襩 ॠ樨 ⠭⭮ ⥪.  㦨,    砥   ஢, ⠪   ᢠ  묨    ।.
ਬ  㫮   HomeForSale ᫮:

class HomeForSale {
public:
...
private:
HomeForSale(const HomeForSale&); // ⮫쪮 
HomeForSale& oparetor=( const HomeForSale&);
};

,    㪠  ࠬ஢ 㭪権.  易⥫쭮,  ⠪ 饥 ᮣ襭.  ࠧ  㭪樨    ॠ뢠  ᯮ짮,       ࠬ஢?
 ⠪ ।  㤥 ஢  ⪨ ⮢ ஢ ꥪ HomeForSale,  ᫨  砩 ⠥ ᤥ   㭪樨-童  㭪樨-㣥 ,   訡 ᮮ 騪.
  ६ 訡  ⠯   ⠯ 樨 ( ᥣ  -  㦨 訡   ࠭), ᫨   ஢   ᢠ 묨   ᠬ  HomeForSale,     , ᯥ樠쭮 ᮧ  ।饭 ஢.    祭 :

class Uncopyable {
protected:
Uncopyable() {} // ࠧ ஢
~Uncopyable() {} //  㭨⮦
// ꥪ⮢ ந ᮢ
private:
Uncopyable(const Uncopyable&); //  । ஢
Uncopyable& operator=(const Uncopyable&);
};

⮡ । ஢ ꥪ⮢ HomeForSale, 㦭  㭠᫥   Uncopyable:

class HomeForSale : private Uncopyable { //  ⮬    
... //  ஢, 
} //  ᢠ

 襭 ࠡ⠥, ⮬   ⠥ ஢  ஢   ᢠ, ᫨ - -    㭪樨-童  㦥⢥ 㭪樨 - ந ⪠ ᪮஢ ꥪ HomeForSale.    ࠢ 12, ᣥ஢ ஬ ᨨ  뢠 ᮮ⢥騥 㭪樨   .    , ⠪       묨.
  ᯮ짮  Uncopyable ᮯ殮  묨 ⮭ﬨ. ਬ, ᫥  Uncopyable     (. ࠢ 32  39),   Uncopyable    㠫 (. ࠢ 7). ᪮ Uncopyable   -童,    ਡ  ⨬樨   ᮢ, ᠭ  ࠢ 39,   ᪮   ,    ⢥ ᫥ (. ࠢ 40).  ⢥ ᫥       ஢ ⨬   ᮢ (. ࠢ 39).  ,   ஢  ⮭   ᯮ짮 Uncopyable,   .  ⠪ ᯮ짮 ᨥ  ⥪ Boost (. ࠢ 55).     뢠 noncopyable.  訩 ,    ,    , ᪠ ⠪, ⢥.

 ᫥ 
 ⮡ ⪫ 㭪樮쭮, ⮬᪨ ।⠢塞 ஬,  ᮮ⢥ 㭪-童 ⮩   砩  ॠ. ᫥   ⨯ Uncopyable -   ᯮᮡ ᤥ .



ࠢ 7:   㠫묨  䭮  

  ᯮᮡ ᫥ ६, ⮬   ᮧ   TimeKeeper  ந   ,  ॠ ࠧ 室  ஭ࠦ:

class TimeKeeper {
public:
TimeKeeper();
~TimeKeeper();
...
};
class AtomicClock: public TimeKeeper {:};
class WaterClock: public TimeKeeper {:.};
class WristWatch: public TimeKeeper {:};

        ६,    ⠫ ⮣,   , ⮬   ᯮ짮 䠡筮 㭪樥 (factory function),  頥 㪠⥫    ᮧ  ꥪ ந :

TimeKeeper *getTimeKeeper(); // 頥 㪠⥫  ᪨
// 뤥 ꥪ ,
// ந  TimeKeeper

 ᮮ⢥⢨  ᮣ襭  䠡 㭪 ꥪ, 頥 getTimeKeeper, 뤥  , ⮬  ⮣, ⮡  祪   㣨 ᮢ, , ⮡  祭 ꥪ  ࠭   㭨⮦:

TomeKeeper *ptk = getTimeKeeper(); //  ᪨ 뤥
// ꥪ  娨 TimeKeeper
... // ᯮ짮 
delete ptk; // 㭨⮦, ⮡  窨
// ᮢ

   ࠢ 13,   ,  ꥪ 㭨⮦ , ॢ 訡,   ࠢ 18 ,   ஢ 䠡 㭪  ।饭   訡  ᪮ ணࠬ.    㤨  쥧 ⮪ ਢ  :  ᫨    ࠢ쭮,    㧭,  㤥  ᥡ ணࠬ.
஡  ⮬,  getTimeKeeper 頥 㪠⥫  ꥪ ந  (ਬ, AtomicClock),  㤠  ꥪ 㦭 १ 㪠⥫    (   TimeKeeper),  ⮬    (TimeKeeper)  㠫 .  אַ   ⭮, ⮬   ᯥ䨪樨 C++ 㫨,   ꥪ ந  㭨⮦ १ 㪠⥫     㠫 ஬,  १  ।.  ६ ᯮ  筮 ਢ  ⮬,   ꥪ, ਭ ந ,   㤥 㭨⮦. ᫨ getTimeKeeper() 頥 㪠⥫  ꥪ  AtomicClock,   ꥪ, ਭ AtomicClock (  -童,   ⮬ ), ⭮,   㭨⮦, ⠪   㤥 맢  AtomicClock.   童,  ⭮    (  TimeKeeper),  㭨⮦,  ਢ   ⠪ 뢠 <筮 ࠧ襭> ꥪ⮢.     窥 ᮢ, ०    ஢ 來 ६  ⢥ ⫠稪.
  ஡ : 㦭     㠫 .   㤠 ꥪ⮢ ந ᮢ 㤥 ந室  ,  㦭. ꥪ 㤥 ࠧ襭 楫,    :

class TimeKeeper {
public:
TimeKeeper();
virtual ~TimeKeeper();
...
};
TimeKeeper *ptk = get TimeKeeper();
...
delete ptk; // ⥯ ࠡ⠥ ࠢ쭮

筮   த TimeKeeper ᮤঠ  㣨 㠫 㭪樨, ஬ , ᪮ 祭 㠫 㭪権 - ᯥ  ன ந ᮢ (. ࠢ 34). ਬ,   TimeKeeper   । 㠫쭠 㭪 getCurrentTime, ॠ -ࠧ  ࠧ ந .    㠫묨 㭪ﬨ  猪   㠫 .
᫨     㠫 㭪権,   砥,    ।祭  .   ⠪  । 㠫   ⮨. ᬮਬ , ।⠢騩   ᪮:

class Point { // 窠  ᪮
public:
Point(int xCoord, int yCoord);
~Point();
private:
int x,y;
};

᫨ int  32 ,  ꥪ Point 筮    64-⮢ ॣ.  ⮣, ⠪ ꥪ Point   ।  64-⮢ ᫮ 㭪, ᠭ  㣨 몠 (⠪  C  FORTRAN). ᫨   Point ᤥ 㠫,   .
 ॠ樨 㠫 㭪権 室, ⮡  ꥪ ࠭ ଠ,   ६ ᯮ  ।,  㠫쭠 㭪   맢.  ଠ 筮 ।⠢ 㪠⥫  ⠡ 㠫 㭪権 vptr (virtual table pointer).  ⠡ -  ᨢ 㪠⥫  㭪樨, 뢠 vtbl (virtual table).   ᮬ,  ஬ । 㠫 㭪樨, 樨஢ ⠡ vtbl.   ண ꥪ 뢠 㠫쭠 㭪,    㪠⥫ vptr  ⠡ vtbl   ॠ쭠 㭪,  㦭 맢.
⠫ ॠ樨 㠫 㭪権  .  ,  ᫨  Point ᮤন 㠫 㭪,  ꥪ ⮣ ⨯ 㢥稢  ࠧ.  32-⮢ ⥪  ࠧ ⠥  64  ( 楫 int)  96  ( int  vptr); 64-⮢ ⥪     64  128 , ⮬  㪠⥫  ⮩ ⥪  ࠧ 64 .  ࠧ,  vptr  ꥪ Point 㢥稢  ࠧ  稭  50  100 %! ᫥ ⮣ ꥪ Point 㦥     64- ॣ.  ⮣, ꥪ ⮣ ⨯  C++  룫拉 ⠪,   ,   㣨 몠, ਬ  C, ⮬   㣨 몠   vptr.  १ ⠭  । ꥪ ⨯ Point ᠭ  㣨 몠 ணࠬ, ᫨ ⮫쪮     vptr.   㦥 ⠫ ॠ樨, , ᫥⥫쭮, ⠪   㤥 ७ᨬ.
ࠪ᪨ 뢮  ᥣ ᪠ ⮨  ⮬,  ᭮    㠫묨 ⠪  ୮,     㠫묨 .  ᪠  ᮢ   ⠪ :  ᫥  㠫묨 ⮣,        㠫쭠 㭪.
 㠫    稭 ⭮⥩    ⢨   㠫 㭪権. ਬ,  ⠭⭮  string  㠫 㭪権,  ணࠬ ६   ᯮ    :

class SpecialString: public std::string { // ! std::string ᮤন
... // 㠫 
};

   ⠪    ।,  ᫨ -  ਫ  ८ࠧ 㪠⥫  SpecialString  㪠⥫  string,  ⥬ 믮  ⮣ 㪠⥫ delete,      । :

SpecialString *pss = new SpecialString(" ᭮");
std::string *ps;
...
ps = pss; // SpecialString*=>std::string*
...
delete ps; // ।!  ࠪ⨪ , 뤥
// ꥪ SpecialString,   ᢮, ⮬
//   SpecialString  뢠

  ⭮   ,  ஬  㠫쭮 ,  ⭮  ᥬ ⨯ STL-⥩஢ (ਬ, vector, list, set, tr1::unordered_map [. ࠢ 54]  . .). ᫨   -  ᮡ 㭠᫥ ⠭⭮ ⥩   㣮   㠫 ஬, ন! ( ᮦ,  C++  ।ᬮ७  堭 ।饭 ᫥, , ᪠, final  몥 Java,  sealed  C#).
   㤮     㠫 . ᯮ,   㠫 㭪樨 ஦ ࠪ ,   ,   ᮧ . , ,    ,   ⥫  ᤥ ࠪ,       ⮩ 㠫쭮 㭪樨.  ? ᪮ ࠪ  ।祭  ᯮ짮  ⢥   ᪮     㠫 ,   㠫쭠 㭪 ஦ ࠪ ,  襭 祢:   㠫   ,    ᤥ ࠪ.  ਬ:

class AWOV { // AWOV = "Abstract w/o Virtuals"
public:
virtual ~AWOV() = 0; //   㠫쭮
}; // 

  砥  ᥡ  㠫 㭪, ⮬  ࠪ.  ࠧ    㠫 ,    ᯮ  ⮬,    ᮢ   맢.    ⮭:   ।⠢ ।  㠫쭮 :

AWOV::~AWOV(){}; // ।  㠫쭮 

  ⮬,  ᭠砫 ᥣ 뢠  <ᠬ ந>  (  室饣   㯥 娨 ᫥),  ⥬    .  ᣥ 맮 ~AWOV  ஢ ந   ᮢ,  ,      ॠ樨. ᫨ ⮣  ᤥ, 騪 㤥 .
ࠢ 祭    㠫 ஢ ᠥ ⮫쪮   ᮢ,   ⠪,   㫨஢ ꥪ⠬ ந ᮢ   㪠⥫  . TimeKeeper -   ,  ,   稨 㪠⥫  ꥪ TimeKeeper ᬮ 㫨஢ ꥪ⠬ AtomicClock  WaterClock.
    ࠧࠡ뢠  ⮬ 䨧. ਬ,  ⠭ ⨯ string,  ⨯ STL-⥩஢ ஥஢ ⠪,   ᪠  ᯮ짮  ⢥ , ⠪    묨.   ।祭 㦨  ⢥ ,  䭮 ᯮ짮  ; ਬ஬  㦨  Uncopyable  ࠢ 6   input_iterator_tag  ⠭⭮ ⥪ (. ࠢ 47).  ᠬ  㦭 㠫 .

 ᫥ 
      㠫 . ᫨      㠫 㭪,    㠫 .
  ,  ।祭  ᯮ짮  ⢥    䭮 ਬ,  ᫥  㠫 .



ࠢ 8:   ᪫祭  

C++  頥 ᯮ짮 ᪫祭  ,  , ᫮, 祭 ⥫쭠 ࠪ⨪.    쥧 稭. ᬮਬ ਬ:

class Widget {
public:
...
~Widget() {...} // ।,   ᪫祭
};
void doSomething()
{
std::vector<Widget> v;
...
} //  v ⮬᪨ 㭨⮦

  v 㭨⮦,  ⢥砥  㭨⮦  ꥪ⮢ Widget,    ᮤঠ. ।,  v ᮤন 10 ꥪ⮢ Widget,   ६ 㭨⮦ ࢮ   㦠 ᪫祭. ⠫  ꥪ⮢ Widget ⠪   㭨⮦ ( , 뤥  ,  ), ⮬ 室 맢   .  ।⠢,    ६  ண ꥪ Widget ⠪ 㤨 ᪫祭.   ࠧ  ६ ⨢ ᪫祭,   ᫨誮   C++.  ᨬ   ᫮ ᯮ ணࠬ  㤥 ࢠ,     ।.  ⮬ ਬ  ࠧ   ன 砩.  ⠪ 㤥 ந室  ᯮ짮  筮 ⥩ (ਬ, list, set),  ⥩ TR1 (. ࠢ 54)   ᨢ.  稭 ⮩ ஡   ⥩  ᨢ. ०६ 襭 ணࠬ  ।    १⮬ ⮣,   㦤 ᪫祭. C++   ஢, 㦤 ᪫祭!
 筮  .     , ᫨  襬  室 믮 ,   த ᪫祭? ਬ, ।,      ᮬ, 뢠騬 祭   :

class DBConnection {
public:
...
static DBConnection create(); // 㭪 頥 ꥪ
// DBConnection; ࠬ 
//  饭
void close(); //  ᮥ;  㤠
}; // 㦤 ᪫祭

 ࠭⨨ ⮣,    㤥 맢 close  ꥪ⮢ DBConnection, १ ᮧ   ࠢ ᠬ DBConnection,  뢠 close  ᢮ . , ࠢ騥 ᠬ,  ஡ ᬮਬ   3,   筮 ਪ,   룫拉  ⠪ :

class DBConn { //   ࠢ ꥪ⠬
public: // DBConnection
...
~DBConn() // ᯥ, ⮡ ᮥ  
{ //  ᥣ 뢠
db.close();
}
private:
DBConnecton db;
};

   ᮤঠ ⠪ :

{ //  뢠
DBConn dbc(DBConnection::create()); // ᮧ ꥪ DBConnection
//  ।  ꥪ DBConn
... // ᯮ짮 ꥪ DBConnection
// १ 䥩 DBConn
} //    ꥪ DBConn
// 㭨⮦,  ⮬
// ⮬᪨ 뢠 ⮤ close
// ꥪ DBConnection

  ਥ   ,  ⮤ close 蠥 ᯥ譮,  ᫨  맮 㦤 ᪫祭,    ।  DBConn.  祭 , ⮬  , 㦤騥 ᪫祭,   筨 訡.
  ᭮ ᯮᮡ  ⮩ ஡.  DBConn :
 ࢠ ணࠬ, ᫨ close 㦤 ᪫祭; 筮  ⮣ 뢠 㭪 abort:

DBConn::~DBConn()
{
try {db.close();}
catch(...) {
  ⮪,  맮 close 訫 㤠筮;
std::abort();
}
}

 १ 롮, ᫨ ணࠬ   த ࠡ ᫥ ⮣,    ந諠 訡. २⢮ ⠪ 室 -  ।饭 । . 맮 abort ।  ।.
 墠 ᪫祭, 㦤 맮 close:

DBConn::~DBConn()
{
try {db.close();}
catch(...) {
  ⮪,  맮 close 訫 㤠筮;
}
}

 , ⠪ <ண뢠> ᪫祭 -  , ⮬   塞  ଠ: -  ࠡ⠫ !    㯨 ⠪, ⮡  ०६ ⠭ ணࠬ  । . 롨  室 ᫥   砥,  ணࠬ  ﭨ  த ᯮ,  ᫥ ⮣,  訡 ந諠,  뫠 ந஢.
    襭   . ஡  ⮬,     ணࠬ    ॠ஢  ,  ਢ  㦤 ᪫祭  close.
 ࠧ㬭 ⥣ - ஥஢ 䥩 DBConn ⠪, ⮡   ᠬ   ॠ஢  騥 訡. ਬ,  DBConn  ।⠢ ᮡ⢥ 㭪 close  ⠪ ࠧ  ⠬ 蠭 ࠡ ᪫祭, 襥   樨. ꥪ ⮣    ᫥, 뫮  ᮥ DBConnection 㦥  㭪樥 close, , ᫨   ⠪, 뢠   .  ᠬ ।頥 窠 ᮥ.  ᫨ close -⠪ 㤥 맢    㤨 ᪫祭,    頥  ᠭ  ਠ⠬: ࢠ ணࠬ  <ண> ᪫祭:

class DBConn {
public:
...
void close() //  㭪  ᯮ짮 ⮬
{
db.close()
closed = true;
}
~DBConn()
{
if(!closed)
try {
db.close(); //  ᮥ, ᫨ ⮣  ᤥ
} // 
catch(...) { // ᫨  ᪫祭, ⮪஢
  ⮪, 
//  ࢠ ணࠬ  <ண> 
 맮 close
訫 㤠筮;
}
}
private:
DBConnecton db;
bool closed;
};

६饭 맮 close   DBConn    ( ⠢   DBConn <客筮> 맮)    ਭ樯 ४뢠 ⢥⢥.    ᬮ  ⮬ 襭 ਭ樯, ᠭ  ࠢ 18: 䥩    ᯮ짮 ࠢ쭮.  ᠬ    ⠪. ᫨    㤠筮  㦤 ᪫祭   室 ࠡ  ᪫祭,  ᪫祭  㦤 㭪樥,  饩 ஬. 易   ⥬,  , 㦤騥 ᪫祭,   ᥣ ॢ ०६ 襭 ணࠬ  । .  ⠬,    ᠬ 뢠 㭪 close,   ६塞  譥 ࠡ⮩,    ࠡ 訡,    ⨢ 砥   ᬮ  ॠ஢. ᫨  ,      祬,   ந஢  ,   ,  ᮥ ஥  DBConn. ᫨   ⮬ ந 訡,   close 㤨 ᪫祭,      , ᫨ DBConn ண   ࢥ ணࠬ.  - 殢,     砩 ॠ஢ -㣮,     ᯮ짮.

 ᫥ 
     㦤 ᪫祭. ᫨ 㭪, 뢠  ,   ᤥ,   易 墠뢠  ᪫祭,  ⥬ <ண뢠>   뢠 ணࠬ.
 ᫨   㦤   ॠ஢  ᪫祭  ६ ன 樨,    ।⠢  㭪 (   ),    믮.



ࠢ 9:   뢠 㠫 㭪樨    

  ७:    뢠 㠫 㭪樨  ६ ࠡ ஢  ஢, ⮬   맮    ,   㬠,  १⠬  ࠡ  㤥 . ᫨  - ணࠬ  Java  C#,     ࠢ ᮡ , ⮬    ⮬ ⭮襭 C++  ᥡ .
।,    ᮢ  ஢ থ ࠭権,   祭  㯪,  த  . . , ⮡  ࠭樨 뫮  ஢, ⮬  ࠧ,  ᮧ  ꥪ ࠭樨,  ⮪ 㤨   ᮮ⢥ . 騩 室  襭  ஡ 룫廊 ࠧ㬭:

class Transaction { //    
public: // ࠭権
Transaction();
virtual void logTransaction() const = 0; // 믮   ⨯
//   ⮪
...
};
Transaction::Transaction() // ॠ 
{ //  
...
logTransaction();
}
class BuyTransaction: public Transaction { // ந 
public:
virtual void logTransaction() const = 0; //  ⮪஢
// ࠭樨  ⨯
...
};
class SellTransaction: public Transaction { // ந 
public:
virtual void logTransaction() const = 0; //  ⮪஢
// ࠭樨  ⨯
...
};

ᬮਬ,  ந  ᯮ ᫥饣 :

BuyTransaction b;

᭮,  㤥 맢  BuyTransaction,  ᭠砫   맢  Transaction, ⮬   ꥪ, ਭ騥  ,  ०, 祬 , ਭ騥 ந .  ᫥ ப  Transaction 뢠 㠫쭠 㭪 logTransaction, -  稭 ਧ.  뢠   logTransaction,  ।   Transaction,    BuyTransaction, ᬮ    ⨯ ᮧ ꥪ - BuyTransaction.  ६ ஢    뢠 㠫 㭪樨, ।  ந . ꥪ  ᥡ ⠪,    ਭ  ⨯.  ,  ६ ஢   㠫 㭪権  .
 ᪠ 稭  ⮫,  ,  . ᪮   ᮢ 뢠 ࠭, 祬  ந,  -童 ந    樠஢  ६ ࠡ   .    稭 ।    ⢠  ⫠稪. 饭  ⥬  ꥪ,    뫨 樠஢, ᭮, ⮬ C++   ⠪ .
   㭤⠫ 稭.   ᮧ ꥪ ந  㤨   , ⨯ ꥪ   .  ⮫쪮 㠫 㭪樨   ⠪,    稥 堭 몠, ᯮ騥 ଠ  ⨯  ६ ᯮ (ਬ, ᠭ  ࠢ 27  dynamic_cast   typeid).  襬 ਬ,  ࠡ⠥  Transaction, 樠   ꥪ BuyTransaction,  ꥪ ⭮  ⨯ Transaction.  ⠪  ਭ   C++,   ⮬  :  ꥪ, ⭮騥  BuyTransaction,   樠஢, ⮬ ᭥ ,     . ꥪ   ꥪ⮬ ந    ,   筥 ᯮ  ᫥.
  ⭮   ࠬ.  ⮫쪮 稭 ᯮ  ந , ।,  -童, ਭ騥 ⮬ ,  ।, ⮬ C++ ⠥,     .  室      ꥪ ⠭ ꥪ⮬  ,    C++ - 㠫 㭪樨,  dynamic_cast  . .- ਭ   ⠪.
 ਢ  ਬ   Transaction  頥  㠫쭮 㭪樨,  ।⠢ ᮡ ஢ 襭 ਭ樯, ᠭ   ࠢ.  襭  㦨, ⮬   뤠 ।० ( 㣨 - ;   ।० . ࠢ 53).    ⠪ ।० 訡 猪   ६ ᯮ, ⮬  㭪 logTransaction   Transaction   㠫쭮. ᫨ ⮫쪮   뫠 - । (⭮,   - . ࠢ 34),  ⠪ ணࠬ  ᪮: 騪   室 ॠ Transaction::logTransaction.
 ᥣ ⠪  㦨 맮 㠫쭮 㭪樨  ६ ࠡ   . ᫨ Transaction  ᪮쪮 ஢,    믮     ࠡ,  ᫥ ஥஢ ணࠬ ⠪, ⮡  㡫஢ , ⨢   樠樨,  맮 logTransaction,   㠫 㭪 樠樨, ᪠, init:

class Transaction {
public:
Transaction()
{ init(); } // 맮 㠫쭮 㭪樨
Virtual void logTransaction() const = 0;
...
private:
void init()
{
...
logTransaction(); //   맮 㠫쭮
// 㭪樨!
}
};

楯㠫쭮    ⫨砥  ਢ ,    , ⮬  筮 㤥 ᪮஢  ᪮  ।०.  ⮬ 砥, ᪮ logTransaction -  㠫쭠 㭪  Transaction,    맮 設⢮ ⥬ ६ ᯮ  ணࠬ (筮 뤠 ᮮ⢥饥 ᮮ饭).  ᫨ logTransaction 㤥 <ଠ쭮> 㠫쭮 㭪樥,  ன   Transaction  ॠ,   㭪  㤥 맢,  ணࠬ ࠤ⭮ த ࠡ, ⠢   㬥, 祬  ᮧ ꥪ ந  뫠 맢 ୠ  logTransaction. ⢥ ᯮᮡ  ⮩ ஡ - 㡥,     ஢  ஢  뢠 㠫 㭪権  ᮧ  㭨⮦ ꥪ,    㭪樨,    , ᫥ ⮬  ࠢ.
    㡥  ⮬,  뢠 ࠢ쭠  log-Transaction  ᮧ  ꥪ  娨 Transaction? ⭮,  맮 㠫쭮 㭪樨 ꥪ  ஢  .
 ࠧ ਠ 襭 ⮩ ஡.    - ᤥ 㭪 logTransaction 㠫쭮   Transaction, ⥬ ॡ, ⮡  ந  । 室    ⮪ ଠ  Transaction.  㭪 ⥬   ᭮ 맢 㠫 logTransaction. ਬ୮ ⠪:

class Transaction {
public:
explicit Transaction(const std::string& loginfo);
void logTransaction(const std::string& loginfo) const; // ⥯ -
// 㠫쭠
// 㭪
...
};
Transaction::Transaction(const std::string& loginfo)
{
...
logTransaction(loginfo); // ⥯ -
// 㠫
// 맮
}
class BuyTransaction : public Transaction {
public:
BuyTransaction( parameters )
: Transaction(createLogString( parameters )) // । ଠ
{...} //    ⮪
... //  
// 
private:
static std::string createLogString( parameters );
}

㣨 ᫮, ᫨    뢠 㠫 㭪樨    ,   ஢  ।祩 室 ଠ樨      ந.
 ⮬ ਬ    ਬ ⮩ ᪮ 㭪樨 createLogString  BuyTransaction. ᯮ짮 ᯮ⥫쭮 㭪樨  ᮧ 祭, ।   ,  㤮 (  ⠥), 祬 ᫥  ᯨ᪠ 樠樨 童  ।   ⮣,   㦭.   㭪 ᪮,   ᭮ ﭭ ᫠  樠஢ -童  BuyTransaction.  , ᪮  䠪,   -童   ।,   ᭮ 稭, 祬  뢠 㠫 㭪樨  ஢  ஢.

 ᫥ 
  뢠 㠫 㭪樨  ६ ࠡ ஢  ஢, ⮬  ⠪ 맮     ந ᮢ, ᯮ  娨 ᫥  ⮣,  ᥩ   㭨⮦.



ࠢ 10:  ᢠ   뫪  *this

   ᢮ ᢠ ⮨  ⮬,  ⠪ 樨  믮 ᫥⥫쭮:

int x,y,z;
x = y = z = 15; // 楯窠 ᢠ

 ᭮,   ᢠ ࠢ樠⨢, ⮬ ਢ  ਬ ᢠ  ᫥騬 ࠧ:

x = (y = (z = 15));

 ६ z ᢠ 祭 15, ⥬ १ ᢠ ( 祭 z) ᢠ ६ y, ᫥ 祣 १ ( 祭 y) ᢠ ६ x.
⨣    ⮣,   ᢠ 頥 뫪  ᢮  㬥,  ⮬ ᮣ襭   ᫥  ॠ樨 ஢ ᢠ  ᢮ :

class Widget {
public:
...
Widget& operator=(const Widget& rhs) // 頥 ⨯ - 뫪
{ //  ⥪騩 
...
return *this; //  ꥪ   
} // ࠦ
...
};

 ᮣ襭 ᠥ  ஢ ᢠ,   ⮫쪮 ⠭⭮ ,  . ⥫쭮:

class Widget {
public:
...
Widget& operator+=(const Widget& rhs) // ᮣ襭 ࠭ 
{ // +=, -=, *=,  . .
...
return *this;
}
Widget& operator=(int rhs) //  ⭮ 
{ //  ࠬࠬ ࠧ ⨯
...
return *this;
}
...
};

 ᥣ  ᮣ襭. ᫨ ணࠬ   ਤন,  ⥬   ᪮.   ᫥  ஥ ⨯,    ⨯ (. ࠢ 54) ⠭⭮ ⥪ (  string, vector, complex, tr1::shared_ptr  . .). ᫨    ᪮ 稭  ᮣ襭,   ⮣.

 ᫥ 
   ᢠ ⠪, ⮡  頫 뫪  *this.



ࠢ 11:  operator= ⢫ ஢  ᢠ ᠬ ᥡ

ᢠ ᠬ ᥡ  ਬ୮  ⠪ 樨:

class Widget {...};
Widget w;
...
w = w; // ᢠ ᥡ

 룫廊 筮 ,   ᮢ襭 ४⥭,   ⮬,  ணࠬ  ⠪ ᯮᮡ,    ᮬ  ᥪ㭤.
஬ ⮣, ᢠ ᠬ ᥡ  ᥣ ⠪  㧭. ਬ:

a[i] = a[j]; // ⥭樠쭮 ᢠ ᥡ

 ᢠ ᥡ, ᫨ i  j ࠢ   ⮬  祭, 

*px = *py; // ⥭樠쭮 ᢠ ᥡ

⮦ ⠭ ᢠ ᠬ ᥡ, ᫨ ,  px  py 㪠뢠     .
  祢 砨 ᢠ ᥡ  १⮬ ᮢ饭  (aliasing),   뫪  ꥪ    ᯮᮡ. , ணࠬ,   뫪  㪠⥫ﬨ  ࠧ ꥪ   ⮣  ⨯,    ⥬,   ꥪ  ᮢ. 易⥫쭮 , ⮡  ꥪ   ⨯,  ᫨  ਭ   娨 ᮢ,  뫪  㪠⥫      ⢨⥫쭮 ⭮  ꥪ ந :

class Base {...};
class Derived: public Base {...};
void doSomething(const Base& rb, // rb  *pd     ⥬ 
Derived * pd); // ꥪ⮬

᫨  ᫥ ࠢ 13  14,  ᥣ  ꥪ⠬  ࠢ ᠬ; ᫥  ⥬, ⮡ ࠢ騥 ꥪ ࠢ쭮  ᥡ  ஢.  ⠪ 砥  ᢠ    ⭮⥫쭮 ᢠ ᠬ ᥡ. ᫨  ⠥ ࠢ ᠬ ᠬ⥫쭮 (   , ᫨     ࠢ ᠬ),     , ﭭ ᢮    ᯮ짮. ਬ, ।,   ᮧ ,  ᮤন 㪠⥫  ᪨ । ꥪ  Bitmap:

class Bitmap {...};
class Widget {
...
private:
Bitmap *pb; // 㪠⥫  ꥪ, ࠧ饭  
};

 ਢ ॠ  ᢠ operator=,  룫廊 ᮢ襭 ଠ쭮,  ⠭ ᭮  砥 믮 ᢠ ᠬ ᥡ ( ⠪ ᭠  窨 ७ ᪫祭,  ᥩ   ⮬).

Widget&
Widget::operator=(const Widget& rhs) // ᭠ ॠ operator=
{
delete pb; // ४ ᯮ짮 ⥪饣
// ꥪ Bitmap
pb = new Bitmap(*rhs.pb); //  ᯮ짮  ꥪ
// Bitmap, 㪠  ࠢ 
return *this; // . ࠢ 10
}

஡ ⮨  ⮬,   operator= *this (祬 ᢠ 祭)  rhs ( ᢠ)     ⥬  ꥪ⮬. ᫨  ,  delete 㭨⮦  ⮫쪮 Bitmap, ਭ騩 ⥪饬 ꥪ,   Bitmap, ਭ騩 ꥪ  ࠢ .  襭 ࠡ ⮩ 㭪樨 Widget,         ᢠ ᠬ ᥡ, ᮤন 㪠⥫  㤠 ꥪ!
ࠤ樮 ᯮᮡ ।  訡 ⮨  ⮬,  㦭 믮 ஢ ᮢ  砫 operator=:

Widget&
Widget::operator=(const Widget& rhs) // ᭠ ॠ operator=
{
if(this == &rhs) return *this; // ஢ઠ ᮢ: ᫨
// ᢠ ᠬ ᥡ, 
// 祣  
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}

 蠥 ஡,   㦥 㯮,  ।   ᢠ 뫠  ⮫쪮 ᭠  砥 ᢠ ᥡ,   ᭠  ᫥ ᪫祭,  ᫥ ᭮ ⠥ 㠫쭮  ன ᨨ.  ⭮, ᫨ ࠦ <new Bitmap> 맮 ᪫祭 (  稭 ⪠ ᢮ ,  ᪫祭 㤨  ஢ Bitmap),  Widget ⠪ 㤥 ᮤঠ 㪠⥫  騩 Bitmap.  㪠⥫ - 筨 ⭮⥩.   ᭮ 㤠,    ࠧ묥뢠.     ६  ⫠, , 㤠  﫨,-  .
 ,  ᯮᮡ ६ ᤥ operator=   ᫥ ᪫祭     ᢠ ᠬ ᥡ. ⮬   ணࠬ   ᯥ樠쭮 ᢠ ᠬ ᥡ,  ।稢 ᨫ  ⨦ ᭮  ᫥ ᪫祭.  ࠢ 29  ஡ ᬮ७ ⠫쭮,  ᥩ 筮 㯮,     த㬠 ᫥⥫쭮 ஢ ᢠ  ᯥ ᭮  ᫥ ᪫祭 (  ᭮ ᢠ ᠬ ᥡ) . ਬ,     㤠塞 pb   ,   ᪮㥬 ,    㪠뢠:

Widget& Widget::operator=(const Widget& rhs)
{
Bitmap *pOrig = pb; //  室 pb
pb = new Bitmap(*rhs.pb); // ⠭ 㪠⥫ pb   *pb
delete pOrig; // 㤠 室 pb
return *this;
}

, ᫨ <new Bitmap> 㤨 ᪫祭,  pb ( ꥪ Widget, ஬  ਭ) ⠭ .   ஢ન  ᮢ  ࠡ뢠 ᢠ ᠬ ᥡ, ⮬   ᤥ  室 ꥪ Bitmap, 㤠 ,  ⥬ ࠢ 㪠⥫  ᤥ . ,   ᠬ 䥪⨢ ᯮᮡ ࠡ ᢠ ᠬ ᥡ,   ࠡ⠥.
᫨    䥪⨢,    ஢  ᮢ  砫 㭪樨.  ०  ᥡ,    ந室 ᢠ ᠬ ᥡ, ⮬  믮 ஢ન ⮦  室 ஬.    (室  ꥪ)  ,  ⢫ ᪮쪮 ᭨ ᪮ ᯮ. 䥪⨢ ।⥫쭮 롮ન , ஢  ਧ樨 ⮦  ࠤ.
ୠ⨢ 筮 㯮冷稢 ।  operator=   ᯥ祭  ᭮  ᫥ ᪫祭,  ᭮ ᢠ ᠬ ᥡ   ਬ 孨 <஢  > (<copy and swap>).  ᭮ 易  ᭮  ᫥ ᪫祭, ⮬ ᬠਢ  ࠢ 29.     筮 ࠭ ᯮᮡ ᠭ operator=,    ⮨ :

class Widget {
...
void swap(Widget& rhs); //  묨 *this  rhs
... // . ஡  ࠢ 29
};
Widget& Widget:: operator=(const Widget& rhs)
{
Widget temp(rhs); // ᮧ   rhs
swap(tmp); //   *this  
return *this;
}

  㥬 ⥬, : (1)  ᢠ    ਭ騬 㬥  祭  (2) । ꥪ  祭 砥 ᮧ  ⮣ ꥪ (. ࠢ 20):

Widget& Widget::operator=(Widget rhs) // rhs -  । ꥪ
{ //    । 
// 祭
swap(rhs); //   *this  
return *this;
}

筮  ᯮ,  ⠪ 室 ਭ ᭮   ७, , ६  ஢  ⥫ 㭪樨  ஢ ࠬ,    ᣥ஢  䥪⨢ .

 ᫥ 
 ,  operator= ࠢ쭮  ᥡ,  ꥪ ᢠ ᠬ ᥡ.  ⮣  ࠢ  室  楫 ꥪ⮢, ⭮ 㯮冷 ।  ਬ  ஢ .
 ,   㭪樨, 騥  祬  ꥪ⮬,  ᥡ ४⭮  ᮢ    ꥪ⮢.



ࠢ 12:    ꥪ

  ஥஢ ꥪ⭮-ਥ஢ ⥬,  㫨 ७ ன⢮ ꥪ⮢, ஢  ⮫쪮  㭪樨:  ஢   ᢠ.   㭪ﬨ ஢.  ࠢ 5  ਫ,    騥 㭪樨  室,  ,  ᣥ஢ ஬ ᨨ  筮 ,   :    室 ꥪ.
 ᮡ⢥ 騥 㭪樨,  ᮮ頥 ,  ॠ  㬮砭  祬-  ࠢ.  <>   ਣ ࠧ:   ᮮ頥, ᫨  襩 ॠ樨 - ࠢ쭮.
ᬮਬ , ।⠢騩 稪,  ஬ 騥 㭪樨 ᠭ  ⠪ ࠧ,   맮 ⮪:

void logCall(const std::string& funcName); //    ⮪
class Customer {
public:
...
Customer(const Customer& rhs);
Customer& operator=(const Customer& rhs);
...
private:
std::string name;
};
Customer::Customer(const Customer& rhs)
: name(rhs.name) // ஢  rhs
{
logCall(" ஢ Customer");
}
Customer& Customer::operator=(const Customer& rhs)
{
logCall("騩  ᢮ Customer");
name = rhs.name; // ஢  rhs
return *this; // . ࠢ 10
}

  룫廊 ⫨筮,   ᠬ  ⠪    -   ,    Customer  㤥   童:

class Date {...}; //    ६
class Customer {
public:
... //  ࠭
private:
std::string name;
Date lastTransaction;
};

 ⮣  騥 㭪樨 ஢  ⮫쪮  ꥪ,   name,    lastTransaction.  設⢮ ஢ 祣  ᪠  ⮬   ⠭ ᨬ쭮 ஢ ⨪ (. ⠪ ࠢ 53).   祬 ਢ ᠬ⥫쭮 ᠭ 㭪権 ஢.  ⢥࣠ 㭪樨,   , ⮬   ᮮ頥,     . 襭 祢: ᫨    童  ,     騥 㭪樨 ( ⠪   [. ࠢ 4  45]   ⠭ ਠ operator=   [ਬ  ࠢ 10]; ᫨  㤥,     ).
    砥  ⮩ 樨  ᫥. ᬮਬ ਬ:

class PriorityCustomer: public Customer { // ந 
public:
...
PriorityCustomer(const PriorityCustomer& rhs);
PriorityCustomer& operator=(const PriorityCustomer& rhs);
...
private:
int priority;
};
PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs)
: priority(rhs.priority)
{
logCall(" ஢ PriorityCustomer");
}
PriorityCustomer&
PriorityCustomer::operator=(const PriorityCustomer& rhs)
{
logCall(" ᢠ PriorityCustomer");
priority = rhs. Priority;
return *this;
}

  , 騥 㭪樨   PriorityCustomer    童,  ਣ廊 ⥫쭥. ,   -童,    PriorityCustomer,   ꥪ PriorityCustomer ⠪ ᮤন 童, 㭠᫥  Customer,  -   !  ஢ PriorityCustomer  ᯥ 㬥,    ।     (   㯮 Customer  ᢮ ᯨ᪥ 樠樨 童), ⮬  Customer ꥪ PriorityCustomer 㤥 樠஢ ஬ Customer,  ਭ騬 㬥⮢, ஬  㬮砭 (᫨  ,  ⠪    ᪮).   믮 樠  㬮砭 童 name  lastTransaction.
  ᢠ PriorityCustomer   祬 ⫨砥.   믮  ⮪ ஢ -童  , ⮬   묨.
直 ࠧ,   ᠬ⥫쭮  騥 㭪樨  ந ,   ⮬, ⮡ ᪮஢   . 筮  室  ⮬ ࠧ  (. ࠢ 22), ⮬    אַ 㯠  . ⮬ 騥 㭪樨 ந   뢠 ᮮ⢥騥 㭪樨  :

PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs)
: Customer(rhs), // 맢 騩 
//  
priority(rhs.priority)
{
logCall(" ஢ PriorityCustomer");
}
PriorityCustomer&
PriorityCustomer::operator=(const PriorityCustomer& rhs)
{
logCall(" ᢠ PriorityCustomer");
Customer::operator=(rhs); // ᢮ 祭 -童
//  
priority = rhs. Priority;
return *this;
}

祭 ࠧ <஢  >   ⮣ ࠣ ⥯   ⭮.    騥 㭪樨, 㡥,  (1)    -童  (2) 뢠 ᮮ⢥騥 騥 㭪樨   ᮢ.
 ࠪ⨪   騥 㭪樨   宦 ॠ樨,      ᮡ  㡫஢    맮  㭪樨  㣮.  ६ 墠쭮,  맮  饩 㭪樨  㣮 -  .
 ᫠ 뢠  ஢   ᢠ, ᪮  ⥬ ᠬ ⠥ ᪮஢ ꥪ,  㦥 .  ⮫쪮 ᫥,     ᨭ⠪  ⠪ 樨.  ᨭ⠪,  룫廊 ⠪,    ,   ᠬ   砥 ᮢᥬ .  ⠪ ᨭ⠪,    ᤥ,  ᮢ襭 祢 ᯮᮡ, 祬   ᫮  ꥪ   ०. ⮬     ,  㣮.  ਬ  ,  뢠   ᢠ  ஢  ᫥.
⪠ 믮   -   ஢ 맢  ᢠ - ⠪ ᫥.  樠  ꥪ,   ᢠ ࠡ⠥  㦥 騬  樠஢묨 ꥪ⠬. 믮 ᢠ ꥪ, 室饬   ஢,-      樠஢ ꥪ⮬ - ⠪,    ⮫쪮  樠஢ ꥪ. ᥭ!   ⠩.
 ᫨  㦨,    ஢   ᢠ ᮤঠ 宦 , ஡  㡫஢, ᮧ 㭪-童,   뢠 .  㭪 筮  ⮩   뢠 init.  ⥣ ।⠢ , ⠭ ᯮᮡ  㡫஢    ஢   ᢠ.

 ᫥ 
 騥 㭪樨  ࠭஢ ஢  童- ꥪ  ⥩   ᮢ.
  ⠩ ॠ    㭪権  ନ 㣮.  ⮣   㭪樮쭮   㭪,  맮 .




 3
ࠢ ᠬ


 -   ⠪,  ᫥ ᯮ짮   饭 ⥬. ᫨ ⮣  ᤥ,  ⭮.  ணࠬ  C++   ᯮ㥬 ᮬ  ᪠  (᫨  뤥      ᢮,  砥  ),      ⢠ ᮢ, 묨 㦭 ࠢ.  㣨  ᯮ㥬 ᠬ ⭮ 䠩 ਯ, ⥪,     ᪨ 䥩 짮⥫ (GUI), ᮥ     ⥢ ᮪. ᨬ   ᮢ, , ⮡  砭 ᯮ짮  뫨 ᢮.
⪨ ᯥ  <> ।⠢ ᫮   ᫮,  ᫨ ਭ   ᪫祭, 㭪樨   ﬨ   ਫ, 㥬 ணࠬ⠬  ⫨  ᫥⢨ ᨬ ,  ⠭ ᭮,  ਤ뢠   砥 ᮡ ᯮᮡ ࠢ ᠬ ⥫쭮.
  稭  אַ, 饣  ꥪ 室  ࠢ ᠬ, ஥  饩  C++ প  ஢, ஢  権 ஢.  뢠,   樯஢ 室  ᪫   ஡  ࠢ ᠬ. 騥  ࠢ 饭 ᪫⥫쭮 ࠢ .  ᫥饥 ࠢ  ।騥: ꥪ, ࠢ騥 ,    ࠢ쭮.



ࠢ 13: ᯮ ꥪ  ࠢ ᠬ

।,   ࠡ⠥  ⥪, 饩 樨 (  樨, 樨  . .),  , ।⠢騥 ࠧ  権, ᫥  ୥  Investment:

class Investment {...} // ୥  娨
// ⨯ 権

। ,  ⥪ ।⠢ ꥪ, 뢠騥  樨,   䠡筮 㭪樨 (. ࠢ 7):

Investment *createInvestment(); // 頥 㪠⥫  ᪨
// । ꥪ  娨
// Investment: 맢訩  易
// 㤠  (ࠬ  
// 饭)

 ᫥  , 짮⥫, 맢訩 createlnvestment, ⢥砥  㤠 ꥪ, 饭 ⮩ 㭪樥,  砭  ᯮ짮. ᬮਬ ⥯ 㭪 f,   :

void f()
{
Investment *pInv = createInvestment(); // 맢 䠡 㭪
... // ᯮ짮 pInv
delete pInv; // ᢮ , 
} // ꥪ⮬

룫廊 ,   ᪮쪮 砥,  f  㤠 㤠 ꥪ 権, 祭  createlnvestment. -    㭪樨   । return. ᫨ ⠪  㤥 믮,  ࠢ   ⨣  delete. 宦   , ᫨ 맮 createlnvestment  delete    横,   横 㤥 ࢠ  १ 믮 goto  continue.  ,  ।  , 祭 <:>,  㤨 ᪫祭.   ⮬ 砥 ࠢ     delete. ᨬ  ⮣, 祬 delete 㤥 ய饭,  塞  ⮫쪮 , 뤥  ꥪ Investment,    ,   墠⨫.
筮, ⥫쭮 ணࠬ஢  । 訡  த,  㬠  ⮬,      ६.  ᮯ஢ ணࠬ -   । return  continue,    ᫥⢨ ᢮ ⢨  ⥣ ࠢ ᠬ, ॠ   㭪樨. 㦥 ⮣,  <:> 㭪樨 f  맢 㭪,    㦤 ᪫祭,  筥   ᫥ ண <ᮢ襭⢮>.     ,  f ᥣ   ᢮  delete,  .
⮡ ᯥ ᢮ , 饭 createlnvestment,  㦭 㫨஢   ꥪ, 祩  ⮬᪨ ᢮ ,  ࠢ  㭪 f. ᪨    :    ꥪ,     ⮬᪨ 맮 ஢ C++, ⮡ ࠭஢  ᢮. (   㤨  .)
  ᪨ 뤥  <>, ᯮ     㭪樨    ᢮,  ࠢ     㭪.  ⠪ 権 ।祭  ⠭⭮ ⥪ auto_ptr.  auto_ptr 뢠 ꥪ,  㪠⥫ (⥫㠫 㪠⥫), 祩  ⮬᪨ 뢠 delete  ⮣,    㪠뢠.   ᯮ짮 auto_ptr  ।饭 ⥭樠쭮 ᭮ 窨 ᮢ  襩 㭪樨 f:
void f()
{
std::auto_ptr<Investment> pInv(createInvestment()); // 맮 䠡筮
// 㭪樨
... // ᯮ짮 pInv  ࠭
} // ⮬᪮ 㤠 pInv
// ஬ auto_ptr

 ⮩ ਬ    ⢥ ᯥ ਬ ꥪ⮢  ࠢ ᠬ:
  墠뢠  ࠧ ८ࠧ ꥪ, ࠢ騩 .  ਢ ਬ , 饭 㭪樥 createInvestment, ᯮ  樠樨 auto_ptr,  㤥  ࠢ. ᪨  ᯮ짮 ꥪ⮢  ࠢ ᠬ  뢠 祭   樠 (Resource Acquisition Is Initialization - RAII), ᪮ । 室    樠஢ ꥪ ࠢ ᮬ    ⮬  ।.  祭  ᢠ ࠢ饬 ꥪ  樠樨,    砥   ࠧ ᫥ 祭 ८ࠧ  ࠢ騩  ꥪ.
 ࠢ騥 ᠬ ꥪ ᯮ ᢮   ࠭⨨ ᢮ ᮢ. ᪮  뢠 ⮬᪨  㭨⮦ ꥪ (ਬ,  ꥪ 室   ⢨),  ४⭮ ᢮ ᨬ  ⮣,  ࠢ  .  ᫮,   室 ᢮    ᪫祭,   ⥬ 㦤  ࠢ 8, ⮬ ᥩ      㤥.
   auto_ptr ⮬᪨ 㤠 ,   㪠뢠, , ⮡     ६  ⢮   auto_ptr, 㪠뢠饣      ꥪ. ᫨ ⠪ 砥,  ꥪ 㤥 㤠   ࠧ,  易⥫쭮 ਢ  । . ⮡ । ⠪ ஡, ꥪ auto_ptr   ᢮⢮:  ஢ (।⢮  ஢  ஢ ᢠ) ७ 㪠⥫  ஬ ꥪ ⠭ ࠢ ,   ꥪ 砥   ᢮ 쭮 !

std::auto_ptr<Investment> // pInv1 㪠뢠  ꥪ,
pInv1(createInvestment()); // 饭 createInvestment()
std::auto_ptr<Investment> pInv2(pInv1); // pInv2 ⥯ 㪠뢠  ꥪ,
//  pInv1 ࠢ null
pInv1 = pInv2; // ⥯ pInv1 㪠뢠  ꥪ,
//  pInv2 ࠢ null

 ࠭   ஢  饥   ᭮ ॡ  ⮬,     , ࠢ塞 auto_ptr,   㪠뢠  祬  auto_ptr, 砥,  auto_ptr -  ᥣ  訬 ᯮᮡ ࠢ ᪨ 뤥塞묨 ᠬ. ਬ, STL-⥩ ॡ, ⮡  ᮤন  ஢  ᥡ <ଠ쭮>, ⮬    ꥪ auto_ptr .
ୠ⨢ auto_ptr -  ⥫㠫 㪠⥫  ⮬ 뫮 (reference-counting smart pointer - RCSP). RCSP -  ⥫㠫 㪠⥫,  ᫥, ᪮쪮 ꥪ⮢ 㪠뢠  । ,  ⮬᪨ 㤠 ,      뫠. ⥫쭮, RCSP  ᥡ  ᡮ騪 . ,  ⫨稥  ᡮ騪 , RCSP   ࠧࢠ 横᪨ 뫪 (  ᯮ㥬 ꥪ 㪠뢠   㣠).
 tr1::shared_prt  ⥪ TR1 (. ࠢ 54) -  ⨯ ਬ RCSP, ⮬   :

void f()
{
...
std::tr1::shared_ptr<Investment>
pInv(createStatement()); // 맢 䠡 㭪
... // ᯮ짮 pInv  ࠭
} // ⮬᪨ 㤠 pInv
// ஬ shared_ptr

  룫廊  ⠪ ,   ᯮ騩 auto_ptr,  shared_ptr  ஢  ᥡ ࠧ  ⢥:

void f()
{
...
std::tr1::shared_ptr<Investment> // pInv1 㪠뢠  ꥪ,
pInv1(createStatement()); // 饭 createInvestment
std::tr1::shared_ptr<Investment> // ⥯  ꥪ pInv1  pInv2
pInv2(pInv1); // 㪠뢠  ꥪ
pInv1 = pInv2; // 祣  
...
} // pInv1  pInv2 㭨⮦,  ꥪ,
//    㪠뢠,
// ⮬᪨ 㤠

᪮ ஢ ꥪ⮢ tr1::shared_ptr ࠡ⠥ < >,     ᯮ짮  ⢥ ⮢ STL-⥩஢,  ⠪  㣨 ,  ਢ筮  auto_ptr ⥫쭮.
  㦤.  ࠢ 饭  auto_ptr  tr1::shared_ptr,   㣨 ⨯ ⥫㠫 㪠⥫.   ਬ   ᯮ짮 ꥪ⮢  ࠢ ᠬ. auto_ptr  tr1::shared_ptr - ᥣ  ਬ ꥪ⮢,   . ( ஡  tr1::shared_ptr ⠩  ࠢ 14, 18  54.)
 auto_ptr,  tr1::shared_ptr  ᢮  ᯮ  delete,   delete[]. (   ᠭ  ࠢ 16.)  ,   ਬ auto_ptr  tr1::shared_ptr  ᪨ 뤥 ᨢ, ,    ᪮࡭, ᫥騩  ᪮:

std::auto_ptr<std::string> //  ! 㤥
aps(new std::string[10]); // ᯮ짮   ଠ
//  delete
std::tr1::shared_ptr<int> spi(new int[1024]); //   ஡

  㤨,   ।ᬮ७ 祣  auto_ptr  tr1::shared_ptr  ࠡ  ᪨ 뤥묨 ᨢ -   C++,    TR1.   ⥬,  ⠪ ᨢ  ᥣ   ࠬ  ப (vector  string). ᫨  -⠪ ⠥,  뫮    auto_ptr  tr1::shared_ptr  ᨢ,    ⥪ Boost (. ࠢ 55).     boost::scoped_array  boost::shared_array,  ।⠢ 㦭  .
  ࠢ  ᯮ짮 ꥪ⮢  ࠢ ᠬ ।,  ᫨  ᢮   (ਬ, ਬ delete  ⮣,  ᮤন   ࠢ饣 ᠬ ),  㯠 ࠢ쭮. ⮢   ࠢ ᠬ - த auto_ptr  tr1::shared_ptr -   믮 ᮢ⮢  饣 ࠢ,   室    ᠬ,     ᮢ ⭮.  ⠪   ਤ ࠧࠡ ᮡ⢥  ࠢ ᠬ.   ⠪  㤭 ᤥ,  㦭 ਭ    ᮮࠦ (. ࠢ 14  15).
  ⢥ 饣    ᪠,    㭪樨 createInvestment 筮 㪠⥫ -    窠 ᮢ, ⮬  ᫥ 饭   祭   맢 delete  ⮣ 㪠⥫. ( ᫨ ᯮ auto_ptr  tr1::shared_ptr  믮 delete, 㦭   <> 饭 祭 ⥫㠫 㪠⥫.) ⮡   ஡,  ਤ  䥩 createInvestment,   ⠭ ⥬ ࠢ 18.

 ᫥ 
 ⮡ ।  ᮢ, ᯮ ꥪ RAII,  墠뢠   ᢮   ᢮  .
   ᯮ㥬  RAII -  tr1::shared_ptr  auto_ptr. 筮  ⠭ 롮   tr1::shared_ptr, ⮬     ஢ ᮮ⢥ ⨢ .  ᠥ auto_ptr,  ᫥ ஢  㦥  㪠뢠    ꥪ.



ࠢ 14: ⥫쭮 த뢠   ஢ ᮢ, ࠢ ᠬ

 ࠢ 13   祭   樠 (Resource Acquisition Is Initialization - RAII),   ᭮ ᮧ ࠢ ᠬ ᮢ. 뫮 ⠪ ,    頥   auto_ptr  tr1::shared_ptr  ࠢ ᪨ 뤥塞   .        <祩>,    ⥫㠫 㪠⥫ த auto_ptr  tr1::shared_ptr 筮  室. ६  ६  㤥 ⠫  ﬨ,   ᮧ ᮡ⢥   ࠢ ᠬ.
ਬ, ।,   ᯮ ᠭ  몥 C 䥩  ࠡ  ⥪ᠬ - ꥪ⠬ ⨯ Mutex,  ஬  㭪樨 lock  unlock:

void lock(Mutex *pm); // 墠 ⥪,   㪠뢠 pm
void unlock(Mutex *pm); // ᢮ ᥬ

⮡ ࠭஢,    㤥 ᢮ ࠭ 墠祭 Mutex,  ᮧ ࠢ騩 .   ⠪  த⮢ ਭ樯 RAII, ᮣ᭮ ஬  墠뢠  ६ ஢ ꥪ  ᢮   㭨⮦:

class Lock {
public:
explicit Lock(Mutex *pm)
: mutexPtr(pm)
{lock(mutexPtr);} // 墠 
~Lock() {unlock(mutexPtr);} // ᢮ 
private:
Mutex *mutexPtr;
};

 ᯮ  Lock,  ⮣ ॡ  RAII:

Mutex m; // । ⥪,   㦭 ᯮ짮
...
{ // ᮧ   । ᪮ ᥪ樨
Lock ml(&m); // 墠 ⥪
... // 믮 樨 ᪮ ᥪ樨
} // ⮬᪨ ᢮ ⥪   

 ४᭮,   , ᫨ ᪮஢ ꥪ Lock?

Lock ml1(&m); // 墠 m
Lock ml2(ml1); // ஢ m1  m2 -   ந?

  ਬ 饣 ,   ⠫  ࠧࠡ稪 ᮢ RAII:   ந室  ஢ RAII-ꥪ?  設⢥ 砥 롨ࠥ    ਠ⮢:
  ஢.      ᫠ ࠧ ஢ ꥪ⮢ RAII. ⭮,  ࠢ   த Lock, ⮬  । 㦭   ਬ⨢ ᨭ஭樨 (  ⥪).  ஢ RAII-ꥪ⮢   ᫠,    . ࠢ 6 ,   ᤥ:  騥 樨 묨.   Lock   룫拉 ⠪:

lass Lock: private Uncopyable { //  ஢ -
public: // . ࠢ 6
... //  ࠭
};

  뫮  .  ⥫쭮 㤥ন    ,   㤥 㭨⮦ ᫥ ꥪ,   ᯮ.  ⮬ 砥  ஢ RAII-ꥪ 㦭 㢥稢 稪 ᫠ ꥪ⮢, 뫠  .  ॠ <஢>   tr1::shared_ptr.
 RAII- ॠ ஢  ⮬ 뫮 ⥬ 祭 童 ⨯ tr1::shared_ptr<Mutex>.  ᮦ,   㬮砭 tr1::shared_ptr 砥  ⮬,   㤠 ,   㪠뢠,  祭 稪 뫮 ⨣ ,    ,   㦭.   ࠡ⠥  Mutex,  㦭  ࠧ஢ ,   믮 delete.
 , tr1::shared_ptr   <⨫騪> - 㭪  㭪樮 ꥪ,    맢,  稪 뫮 ⨣  ( 㭪樮쭮  ।ᬮ७  auto_ptr,  ᥣ 㤠 㪠⥫). 㭪-⨫騪 -  易⥫ ன ࠬ  tr1::shared_ptr, ⮬   룫拉 ⠪:

class Lock {
public:
explicit Lock(Mutex *pm) // 樠஢ shared_ptr ꥪ⮬
: mutexPtr(pm, unlock) // Mutex,    㤥
// 㪠뢠, 㭪樥 unlock
{ //  ⢥ ⨫騪
lock(mutexPtr.get());
}
private:
std::tr1::shared_ptr<Mutex> mutexPtr; // ᯮ짮
}; // shared_ptr 
// ⮣ 㪠⥫

⬥⨬,   ⮬ ਬ   Lock   .    ⯠ 室.  ࠢ 5 ,    (ᨬ  ⮣, ᣥ஢  ஬  । 짮⥫) ⮬᪨ 뢠  ᪨ -童 .  襬 ਬ  mutexPtr.   mutexPtr ⮬᪨ 맮 㭪-⨫騪 tr1::shared_ptr (  砥 unlock),  稪 뫮  ⥪ ⨣ . (짮⥫,     室 ⥪⮬ , ⭮,    ਨ, 㪠뢠騥,    뫨  ,       㬮砭 , ᣥ஢ ஬.)
 ஢ ࠢ塞 .  ⨬  ⮫쪮  , ᪮쪮  㦭,  ⢥ 稭 ᯮ짮 , ࠢ饣 ᠬ,- ࠭஢,     㤥 ᢮  砭 ࠡ  .  ⮬ 砥 ஢ ࠢ饣 ᮬ ꥪ 砥 ⠪ ஢ ᠬ ,    <>.   ஢ ࠢ饣 ᮬ ꥪ 믮 <㡮 ஢>.  ॠ樨 ⠭⭮  string  㪠⥫    <>,  ࠭ ᨬ, 室騥  ப. ꥪ ⠪  ᮤন 㪠⥫    <>.  ꥪ string ,    㪠⥫,  ,    㪠뢠.   ᭮ 砥  <㡮 ஢>.
 ।  ࠢ塞 ᮬ.  㦭 ࠭஢,  ⮫쪮  RAII-ꥪ 뫠  ,   ஢ ⠪ ꥪ RAII  ᮬ । ꥪ-.    ࠢ 13,  砥 ஢  ਬ auto_ptr.
騥 㭪樨 ( ஢   ᢠ)   ᣥ஢ ஬,  ᫨ ᣥ஢ ᨨ   ⮣,   㦭 (ࠢ 5    㬮砭), ਤ   ᠬ⥫쭮.    ন 饭 ᨨ  㭪権.  室 ᠭ  ࠢ 45.

 ᫥ 
 ஢ RAII-ꥪ⮢   ᮡ ஢ ᮢ, 묨  ࠢ, ⮬    ஢ ।  RAII-ꥪ.
 筮  ॠ樨 RAII-ᮢ ਬ    奬:  ஢   뫮,    㣨 ਠ.



ࠢ 15: ।⠢   ᠬ ᠬ  ࠢ  ᮢ

ࠢ騥 ᠬ  㦨 ᪨ 墠.  ⨮, 騩  祪 ᮢ,  ⢨ ⠪ 祪 - 㭤⠫쭮 ᢮⢮  ஥஢ ⥬.  쭮          ⢨  ᠬ,  㦤 ᥡ 㯮   .   .  ணࠬ 䥩 ॡ 㯠  ᠬ  ।. ᫨    ⪠  ᯮ짮 ⠪ 䥩ᮢ ( ।    ࠪ⨪),   -  ࠢ騩 ꥪ  ࠡ  ᠬ ᮬ.
ਬ,  ࠢ 13   ਬ ⥫㠫 㪠⥫ த auto_ptr  tr1::shared_ptr  ࠭ १ 맮 䠡筮 㭪樨 createInvestment:

std::tr1::shared_ptr<Investment> pInv(createInvestment()); // 

।,  㭪,    ਬ  ࠡ  ꥪ⠬  Investment:

int daysHeld(const Investment *pi); // 頥 ⢮ 
// ࠭ 権

  뢠  ⠪:

int days = daysHeld(pInv); // 訡!

    ᪮: 㭪 daysHeld   㪠⥫  ꥪ  Investment,   ।  ꥪ ⨯ tr1::shared_ptr <Investment>.
室 - ८ࠧ ꥪ RAII- (  砥 tr1::shared_ptr)  ⨯ ࠢ塞   (  Investment*).   ᭮ ᯮᮡ ᤥ :    ८ࠧ.
 tr1::shared_ptr,  auto_ptr ।⠢ 㭪-童 get  믮  ८ࠧ,    () 㪠⥫  ࠢ塞 ꥪ:

int days = daysHeld(pInv.get()); // ଠ쭮, 㪠⥫, ࠭騩
//  pInv, । daysHeld

    ⥫㠫 㪠⥫, tr1::shared_ptr  auto_ptr ॣ㦠  ࠧ묥 㪠⥫ (operator-> operator*),   ᯥ稢   ८ࠧ  ⨯ ࠢ塞 㪠⥫:

class Investment { // ୥  娨
public: // ⨯ 権
bool isTaxFree() const;
...
};
Investment *createInvestment(); // 䠡筠 㭪
std::tr1::shared_ptr<Investment> //  tr1::shared_ptr
pi1(createInvestment()); //  ࠢ ᮬ
bool taxable1 = !(pi1->isTaxFree()); //   
// १  ->
...
std::auto_ptr<Investment> pi2(createInvestment()); //  auto_ptr 
// ࠢ ᮬ
bool taxable2 = !((*pi2).isTaxFree()); //   
// १  *
...

᪮  室    , ࠢ塞 RAII-ꥪ⮬,   ॠ樨 RAII ।⠢ 㭪樨   ८ࠧ. ਬ, ᬮਬ ᫥騩   ࠡ  ⠬, 㫨騩 <த> 䥩, ᠭ  C:

FontHandle getFont(); //   API - ࠬ ய饭
//  
void releaseFont(FontHandle fh); //  ⮣  API
class Font { //  RAII
public:
explicit Font(FontHandle fh) // 墠 :
:f(fh) // ਬ ।  祭,
{} // ⮬  ⮣ ॡ C API
~Font() {releaseFont(f);} // ᢮ 
private:
FontHandle f; // ࠢ塞  - 
};

।,    ணࠬ 䥩, ᠭ  C, ࠡ騩 ᪫⥫쭮  ନ FontHandle. ⮬  室 ८ࠧ뢠 ꥪ  ⨯ Font  FontHandle.  Font  ।⠢ 㭪  ८ࠧ, ਬ get:

class Font {
public:
...
FontHandle get() const {return f;} // 㭪  ८ࠧ
...
};

 ᮦ, 짮⥫ ਤ 뢠 get 直 ࠧ  ⢨  API:

void changeFontSize(FontHandle f, int newSize); //  C API
Font f(getFont());
int newFontSize;
...
changeFontSize(f.get(), newFontSize); //  ८ࠧ
//  Font  FontHandle

 ணࠬ  ,   ࠧ 믮  ८ࠧ ⮫쪮 ६⥫쭮,   ⪠  ਬ ⮣ .  १  ᭮ 窨 ⮢,    ⮣, ⮡ । ,   ࠧࠡ⠭  Font.
ୠ⨢   ।⠢ ᮬ Font 㭪樨  ८ࠧ  FontHandle:

class Font {
public:
...
operator FontHandle() const // 㭪  ८ࠧ
{return f;}
...
};

 ᤥ 맮 C API 묨  ⢥묨:

Font f(getFont());
int newSize;
...
changeFontSize(f, newFontSize); //  ८ࠧ  Font
//  FontHandle

,  ⮣ 襭   ⭠ ஭: 蠥 ⭮ 訡. ਬ, 짮⥫  ﭭ ᮧ ꥪ FontHandle,    Font:

Font f1(getFont());
...
FontHandle f2 = f1; // 訡! । ᪮஢ ꥪ Font,
//   f1  ८ࠧ  ࠢ塞
//  FontHandle,   ᪮஢  f2

  ணࠬ  FontHandle, ࠢ塞 ꥪ⮬ Font f1,    㯥  ,  f2.   ᥣ . ਬ, ᫨ f1 㤥 㭨⮦,  ᢮,  f2 ⠭ <祩 뫪>.
襭  ⮬,  㦭 ।⠢  ८ࠧ RAII-ꥪ  ࠢ塞   (।⢮ 㭪樨 get),   - ,   ⭮ ,  襭 ன  ஥஢ ,  ᫮  ਬ. 宦,  襥 襭 - ᫥ ᮢ⠬ ࠢ 18,  :  䥩 묨  ࠢ쭮 ਬ  㤭묨 -  ࠢ쭮.   ८ࠧ ⨯ 㭪樨 get -  ।⥫ ਠ, ᪮  蠭  ⥫쭮 ८ࠧ ⨯.   ⢥ ਬ  ८ࠧ  ᤥ   .
 ,  㭪樨, ᯥ稢騥   ࠢ塞 ᠬ, ⨢ ਭ樯 樨. ୮,    砥   .   ⮬,  RAII-    ⮣, ⮡ - 㫨஢.  祭 - ࠭஢,  । ⢨ (  ᢮ ) 易⥫쭮 ந.      ॠ  ᭮ 㭪樮쭮,     室.  ⮣,  RAII-  ⨭  ॠ樨  ⪠    ⭮襭 ࠢ塞 . ਬ, tr1::shared_ptr 㫨  뫮,  ।⠢ ⮩   ࠢ塞  㪠⥫.   設⢮  ஥஢ ᮢ,  뢠 ,    㦭 ,  ᯥ稢   ⮬,   室.

 ᫥ 
 ணࠬ 䥩 (API)  ॡ אַ 饭  ᠬ.  ⮬  RAII-  ।⠢  祭 㯠  ,   ࠢ.
    ᯥ祭 ।⢮    ८ࠧ.  ,  ८ࠧ ᭥,    㤮  짮⥫.



ࠢ 16: ᯮ   new  delete

 ࠢ쭮  ᫥饬 ࠣ?

std::string *stringArray = new std::string[100];
...
delete stringArray;

  ,    浪 - ᯮ짮 new ᮮ⢥ ਬ delete,  -  ᮢ襭 ୮.  ணࠬ ।᪠㥬.  襩 , 99  100 ꥪ⮢ string,   㪠뢠 stringArray, ⭮,   ४⭮ 㭨⮦, ⮬   , ᪮॥ ᥣ, ⠪   맢.
 ᯮ짮 ࠦ new ( ꥪ ᮧ ᪨ ⥬ 맮  new) ந室  ᮡ. -, 뤥  (।⢮ 㭪樨 operator new, . ࠢ 49  51). -,  ⮩  뢠   ᪮쪮 ஢.  맮 delete ⠪ ந室  ᮡ: 뢠   ᪮쪮 ஢,  ⥬  頥 ⥬ (।⢮ 㭪樨 operator delete, . ࠢ 51).  , 騩  裡  ᯮ짮 delete, 砥  ᫥饬: ᪮쪮 ꥪ⮢ ᫥ 㤠  ? ⢥    ।, ᪮쪮 ஢ 㦭 㤥 맢.
 ⢨⥫쭮  ࠧ :   㤠塞 㪠⥫ 㪠⥫   ꥪ   ᨢ ꥪ⮢?   , ᪮ 奬 ।   ⤥ ꥪ⮢ ⢥ ⫨砥  奬 뤥   ᨢ.  ⭮,  뤥   ᨢ 筮   ࠧ, ⮡  delete , ᪮쪮 ஢ 뢠.  , 뤥  ⤥쭮 ꥪ, ⠪ ଠ  ࠭.  奬 ।  ࠦ  㭪  (n - ࠧ ᨢ):
筮,  ⮫쪮 ਬ.    ॡ ॠ뢠 奬  ⠪ ࠧ,   ⠪  .
  ᯮ  delete  㪠⥫,    㧭,  -  ଠ  ࠧ ᨢ? 쪮  . ᫨ ᫥ delete   ᪮,  ।,  㪠⥫ 㪠뢠  ᨢ.  ⨢ 砥  ⠥,   㪠⥫  ⤥ ꥪ:

std::string *stringPtr1 = new std::string;
std::string *stringPtr2 = new std::string[100];
...
delete stringPtr1;
delete[]stringPtr2;

 ந, ᫨ ᯮ짮  <[]>  stringPtr1?   ।,     㤥 .  ।,   ࣠,   ਢ  奬, delete ᭠砫 ⠥ ࠧ ᨢ,  ⥬ 㤥 뢠 ,      䠪,  ,  ன  ࠡ⠥,  ⮫쪮   ᨢ,    ᮤন ꥪ⮢ ⮣ ⨯,     맢 .
 , ᫨   ᯮ  <[]>  stringPtr2? ⭮,   ।,  㤥 맢 ⮫쪮  ,  㦭 뫮 맢 ᪮쪮.  ⮣,   ।   ஥ ⨯,  int, ᬮ       ஢.
ࠢ ⮥: ᫨  ᯮ []  ࠦ new,   ᯮ짮 []   ᮮ⢥饬 ࠦ delete. ᫨   ᯮ []  new,    ᯮ짮   ᮮ⢥饬 ࠦ delete.
 ࠢ ᮡ    ᠭ ᮢ, ᮤঠ 㪠⥫  ᪨ । ,    ᪮쪮 ஢, ᪮  ⮬ 砥   ᯮ짮   new     樠樨 童-㪠⥫. ᫨ ⮣  ᤥ,   㧭,   delete ਬ  ?
 ࠢ  ,   ਡ  ᯮ짮 typedef, ᪮   ᫥,   typedef  㬥஢,   delete ਬ  㤠 ꥪ⮢ ⨯, 뢠 typedef. ᬮਬ ਬ:

typedef std::string AddressLines[5]; //  祫 ⮨  4 ப,
//     ⨯ string

᪮ AddressLines - ᨢ,  ᫥饬 ਬ new

std::string *pal = new AddressLines; // ⬥⨬,  "new AddressLines"
// ୥ string *,  
// ࠦ "new string[4]"

 ᮮ⢥⢮ ଠ delete  ᨢ:

delete pal; //  ।!
delete[] pal; // ࠢ쭮

⮡  ⠭, ࠩ  ਬ typedef  । ⨯ ᨢ.  , ⮬  ⠭⭠ ⥪ C++ (. ࠢ 54) 砥 蠡  string  vector, 騥 ࠪ᪨    ᪨ ᨢ. ,  ਬ  AddressLines  뫮  ।   ப: vector<string>.

 ᫥ 
 ᫨  ᯮ []  ࠦ new,   ਬ []   ᮮ⢥饬 ࠦ delete. ᫨   ᯮ  ᪮ []  ࠦ new,    ᯮ짮    ᮮ⢥饬 ࠦ delete.



ࠢ 17: 饭  <⥫㠫> 㪠⥫ ꥪ, 饫   new,  ᯮ  ⤥쭮 ।

।,   㭪,  ஢ ਮ ࠡ⪨,  㣠 㭪  믮 ன ࠡ⪨ ᪨ 뤥 ꥪ Widget  ᮮ⢥⢨  ⨬ ਮ⮬:

int priority();
void processWidgets(std::tr1::shared_ptr<Widget> pw, int priority);

  ६ ਬ ꥪ⮢, ࠢ ᠬ (. ࠢ 13), processWidgets ᯮ <⥫㠫> 㪠⥫ ( - tr1::shared_ptr)  ࠡ⪨ ᪨ 뤥 ꥪ. ᬮਬ ⥯ ⠪ 맮 processWidgets:

processWidgets(new Widget, priority());

⮯,    ᬠਢ!   ᪮.  tr1::shared_ptr, ਭ騩 㪠⥫,   祢 ᫮ explicit, ⮬  ந室  ८ࠧ  ⨯ 㪠⥫, 饭 ࠦ <new Widget>,  ⨯ tr1::shared_ptr, ண  㭪 process-Widgets.  ᫥騩  :

processWidgets(std::tr1::shared_ptr<Widget>(new Widget), priority());

   ࠭,  ᬮ  ᯮ짮 ࠢ饣 ᠬ ꥪ,   窠 ᮢ. ६, 祬.
० 祬  ᬮ ᣥ஢ 맮 processWidgets,   ᫨ 㬥, ।   ⢥ ࠬ஢. ன 㬥 -  맮 㭪樨 priority,   - (std::tr1::shared_ptr<Widget> (new Widget)) - ⮨   ⥩:
 믮 ࠦ <new Widget>;
 맮  tr1::shared_ptr.
। ⥬  ந 맮 processWidgets,   ᣥ஢   襭 ᫥  :
 맮 priority;
 믮 <new Widget>;
 맮  tr1::shared_ptr.
ࠬ C++ ।⠢ । ᢮  । 浪 믮  権. ( ⨬ C++ ⫨砥  ⠪ 몮,  Java  C#,  ࠬ 㭪権 ᥣ   । 浪.) ࠦ <new Widget>   믮 । 맮  tr1::shared_ptr, ⮬  १ ⮣ ࠦ ।   ⢥ 㬥,  맮 priority   믮 ,   쨬. ᫨   ⠢   ஥  (   ᣥ஢  䥪⨢ ),   稬 ᫥ ᫥⥫쭮 権:
1. 믮 <new Widget>.
2. 맮 priority.
3. 맮  tr1::shared_ptr.
ᬮਬ,  , ᫨ 맮 priority 㤨 ᪫祭.  ⮬ 砥 㪠⥫, 饭 <new Widget>, 㤥 ,    饭  ꥪ tr1::shared_ptr, ,  ,  ।  . 窠  맮 processWidgets ந室 - ⮣,  ᪫祭   ⮬ ᮧ   ⮬ 饭   ࠢ騩 ꥪ.
  ஡ : ᯮ ⤥ ।  ᮧ ꥪ Widget  饭   ⥫㠫 㪠⥫,  ⥬ ।  ⥫㠫 㪠⥫ processWidgets:

std::tr1::shared_ptr<Widget> pw(new Widget); //   ꥪ
//  ⥫㠫 㪠⥫
//  ⤥쭮 ।
processWidget(pw, priority()); //  맮  ਢ
//  窥

 ᯮᮡ ࠡ⠥ ⮬,  ࠬ ।⠢  ᢮  ८। 浪 権  ࠧ ।, 祬  .  ஢  ࠦ <new Widget>  맮  tr1::shared_ptr ⤥  맮 priority, ⮬    ⠢ 맮 priority  .

 ᫥ 
 頩 ꥪ, 뤥 ஬ new,  <⥫㠫> 㪠⥫  ⤥쭮 ।.  ⨢ 砥 ⠪ 맮  ਢ  窠 ᮢ, ᫨  ᪫祭.




 4
஥஢ ணࠬ  


஥஢ ணࠬ ᯥ祭 -  ਥ 祭 ணࠬ,   , 祣    . 筮 ஥ 稭  쭮 饩 ,  ⥬ ⠥ ⠫ﬨ ⮫쪮, ⮡  뫮 㯨  ࠧࠡ⪥  䥩ᮢ. 䥩  ⥬ ॢ    몥 C++.  饩   ᬮਬ ஡ ஥஢    䥩ᮢ  C++. 筥    ᠬ  ࠢ ஥஢ 䥩ᮢ: ᯮ짮  ࠢ쭮   ,  ࠢ쭮 - 㤭. ⠫  ⮩ ᫨,  㫨㥬    ࠢ,  ᠬ ࠧ ⥬,  : ४⭮, 䥪⨢, , 㤮⢮ ᮯ஢, 塞  ᫥ ਭ ᮣ襭.
।⠢  ⮩  ਠ  墠뢠 ᥣ,  㦭   ஥஢  䥩ᮢ.  ⠭       ᮣ襭, 㪠   ⨯ 訡  । 襭 ஡,   । ஥஢騪 ᮢ, 㭪権  蠡.



ࠢ 18: ஥ 䥩 ⠪,    뫮 ᯮ짮 ࠢ쭮  㤭 - ࠢ쭮

C++  䥩ᠬ. 䥩 㭪権. 䥩 ᮢ. 䥩 蠡.  䥩 -  ।⢮, ।⢮ ண 짮⥫   訬 . ।,      ࠧ㬭묨 쬨,  ६  ᤥ ᢮ ࠡ.   ਬ  䥩 ४⭮. ᫨ ,   ਬ -   ࠢ쭮,      殮  .  ,  ⪥ ᯮ짮 䥩 ⠪,  짮⥫    १,    ஢.  ᫨ ,    ,     짮⥫.
 ࠧࠡ⪥ 䥩ᮢ,   ࠢ쭮 ਬ  㤭 -  ࠢ쭮,   ।,  訡   짮⥫. ਬ, ।,   ࠧࠡ뢠  , ।⠢饣 :

class Date {
public:
Date(int month, int day, int year);
...
};

  ,  䥩   ࠧ㬭 ( 类 砥,  ),  ,  ࠩ ,  訡,     짮⥫. -,   । ࠬ  ࠢ쭮 浪:

Date(30, 3, 1995); //   "3, 30",   "30, 3"

-,       㪠 ୮:

Date(2, 20, 1995); //   "3, 30",   "2, 20"

(᫥ ਬ   㬠,  ᯮ,    <2> 室 冷  <3>.  ⪨  ᯫ  冷.)
 訡       ⨯. ⥬ ஫ ⨯ -      ।饭 㥬 ⥫쭮 .   砥     ⨯-⪨, ⮡ ࠧ ,   , ⥬ ᯮ짮    Date:

struct Day { struct Month { struct Year {
explicit Day(int d) explicit Month(int m) explicit Year(int y)
: val(d) {} : val(m) {} : val(y) {}
int val; int val; int val;
}; }; };
class Date {
public:
Date(const Month& m, const Day& d, const Year& y(;
...
};
Date d(30, 3, 1995); // 訡! ࠢ ⨯
Date d(Day(30), Month(3), Year(1995); // 訡! ࠢ ⨯
Date d(Month(3), Day(30), Year(1995)); // 冷, ⨯ ४

  ᤥ Day, Month  Year 業묨 ᠬ, 㫨騬 ᢮  (. ࠢ 22).   ਬ   來 ,  ࠧ㬭 ᯮ짮  ⨯ ᯮᮡ 䥪⨢ । 訡  ᯮ짮 䥩ᮢ.
᫥ ⮣  । ࠢ ⨯,    ࠭ ⢮ ਭ  祭. ਬ,  ⮫쪮 12 ⨬ 祭 楢,    ࠧ ⨯ Month.   ᯮᮡ ᤥ  - ਬ ᫥ (enum)  ।⠢ .  ᫥  ⠪   ⭮襭  ⨯,  ⥫ . ਬ, ᫥   ᯮ짮  祭 ⨯ int (. ࠢ 2).  ᭮ 襭 - ।  ⨬ 楢:

class Month {
public:
static Month Jan() {return Month(1);} // 㭪樨  
static Month Feb() {return Month(2);} // ⨬ 祭 Month.
... // C. , 祬  㭪樨,
static Month Dec() {return Month(12);} //   ꥪ
... // 稥 㭪樨-童
private:
explicit Month(int m); // ।頥 ᮧ 
// 祭 Month
... // ᯥ   
};
Date d(Month::Mar(), Day(30), Year(1995));

 ਬ 㭪権  ꥪ⮢  ।⠢ 楢    筮.  ᯮ   樠樨  ᪨ ꥪ⮢. ࠢ 4  ᢥ  .
㣮 ᯮᮡ ।  訡 ⮢ - ࠭ ⢮ ࠧ襭  ⨯ 権. 騩 ᯮᮡ ⠭ ࠭祭 -  const. ਬ,  ࠢ 3 ,   䨪 const  ⨯ 祭, 頥 㭪樥 operator*,  । ᫥ 訡 :

if(a *b = c)... //    ࠢ

᪨  ਬ 㣮 饣 ࠢ 祭 ࠢ쭮 ᯮ짮 ⨯  ᫮ ࠢ쭮  ᯮ짮:   ⨯   ᮣᮢ   ஥ ⨯ (஬  ᪫⥫ 砥).  㦥 ,   ᥡ  ⨯ த int, ⮬   , ⮡  ⨯    ᥡ 筮. ਬ, ᢠ ࠦ a*b ⨬, ᫨ a  b - 楫, ⮬ ᫨  ᪮ 稭 ⪫  ⮣ ,    ⨬    ⨯.  ᮬ,  ⠪,   ᥡ int.
 ࠢ 宦   ஥ ⨯ 室  ⮣, ⮡ ᯥ ᮣᮢ 䥩ᮢ.   ࠪ⨪   ਬ 䥩ᮢ ᮣᮢ - ୮, ᠬ .  , ᮣᮢ - ﬠ ண  襭 ⢠ 䥩. 䥩 STL-⥩஢  設⢥ 砥 ᮣᮢ (   쭮),    ᯮᮡ   ᯮ짮. ਬ,  STL-⥩  㭪-童 size,  ᮮ頥, ᪮쪮 ꥪ⮢ ᮤন  ⥩. ⨢,  몥 Java  ᨢ ᯮ ᢮⢮ length,   String - ⮤ length,    List - ⮤ size.   . NET:  Array  ᢮⢮ Length,   ArrayList - ᢮⢮ Count.  ࠧࠡ稪 ,  ⥣஢ । ࠧࠡ⪨ (IDE)   ᮮ⢥⢨ ⢥묨,   訡. ᮮ⢥⢨  ணࠬ த⨢ ࠡ,    IDE   .
 䥩,  ॡ, ⮡ 짮⥫ - ,   ᯮ짮 ࠢ쭮,  짮⥫  ᯮᮡ ,    ॡ. ਬ,  ࠢ 13 ।⠢ 䠡筠 㭪,  頥 㪠⥫  ᪨ । ꥪ  娨 Investment:

Investment *createInvestment(); //  ࠢ 13: ࠬ
//   饭

⮡  窨 ᮢ, 㪠⥫, 饭 createInvestment, 易⥫쭮   㤠. ⥫쭮, 짮⥫  ᮢ,  ࠩ ,  訡:  㤠 㪠⥫  㤠    ࠧ.
ࠢ 13 뢠,     祭, 饭 createInvestment,  <⥫㠫> 㪠⥫  auto_ptr  tr1::shared_ptr,  ⥬ ᠬ   ⢥⢥  맮 delete.  , ᫨  㤥 ਬ <⥫㠫> 㪠⥫?     ।饭 ⮩ ஡  뫮   䠡 㭪,  ᠬ 頥 <⥫㠫> 㪠⥫:

std::tr1::shared_ptr<Investment> createInvestment();

 짮⥫ 㤥 㦤 ࠭ 頥 祭  ꥪ ⨯ tr1::shared_ptr,    ਤ   ⮬,  ꥪ Investment  襭 ࠡ   室 㤠.
᪨  祭 ⨯ tr1::shared_ptr  ஥஢騪 䥩 ।   㣨 訡, 易  ᢮ , ⮬ ,  ᭥  ࠢ 14, tr1::shared_ptr ᪠ ਢ離 㭪樨-⨫騪  ⥫㠫쭮 㪠⥫   ᮧ (auto_ptr   ⠪ ).
।,   짮⥫,  稫 㪠⥫ Investment*  createInvestment, ,    ࠡ  ।  㭪樨 getRidOfInvestment,  ⮣ ⮡ ਬ   delete.  䥩 - ﬠ ண  㣮 訡, 饩  ᯮ짮  ⮣ 堭 㤠 ᮢ (짮⥫  -⠪ 맢 delete  getRidOfInvestment).  createInvestment    ஡   ⮣,  ୥ tr1::shared_ptr  ਢ易    ⢥ ⨫騪 㭪樥 getRidOfInvestment.
 tr1::shared_ptr ਭ  㬥: 㪠⥫,  㦭 ࠢ,  㭪-⨫騪,    맢,  稪 뫮 ⨣ .      ᫥騬 ࠧ ᮧ 㫥 㪠⥫ tr1::shared_ptr  getRidOfInvestment  ⢥ ⨫騪:

std::tr1_shared_ptr<Investment> // ⪠ ᮧ 㫥 shared_ptr
pInv(0, getRidOfInvestment); //  ⨫騪
//   ᪮

 ᮦ, C++   ਥ.  tr1::shared_ptr ॡ, ⮡   ࠬ  㪠⥫,  0 -   㪠⥫,  楫. ,  ८ࠧ  㪠⥫,     ⮣ 筮: tr1::shared_ptr ⠨  饬 㪠⥫. ਢ ⨯ 蠥  ஡:

std::tr1_shared_ptr<Investment> // ᮧ null shared_ptr
pInv(static_cast<Investment*>(0), //  getRidOfInvestment  ⢥
getRidOfInvestment); // ⨫騪. static_cast .
//  ࠢ 27

 ,  , ॠ騩 createInvestment,    tr1::shared_ptr  getRidOfInvestment  ⢠ ⨫騪, 㤥 룫拉 ਬ୮ ⠪:

std::tr1::shared_ptr<Investment> createInvestment()
{
std::tr1::shared_ptr<Investment> retVal(static_cast<Investment*>(0),
getRidOfInvestment);
retVal = ...; // retVal  㪠뢠
//  ४ ꥪ
return retVal;
}

筮, ᫨ 㪠⥫,   ࠢ pInv,  뫮  ।  ᮧ pInv,   뫮  ।   pInv  樠樨 pInv 㫥  ᫥騬 ᢠ 祭 (. ࠢ 26).
ᮡ ᨬ筮 ᢮⢮ tr1::shared_ptr 砥  ⮬,   ⮬᪨ ᯮ । 짮⥫ ⨫騪, ⮡  㣮 ⥭樠쭮 訡 짮⥫ - <஡ ᪮쪨 DLL>.  , ᫨ ꥪ ᮧ ஬ new   ᪨ ᪮ ⥪ (DLL),  㤠 ஬ delete  㣮.   ଠ  ⠪ 樨  訡  ६ ᯮ. tr1::shared_ptr 蠥  ஡, ᪮  ⨫騪  㬮砭 ᯮ delete  ⮩  ᠬ DLL,   ᮧ tr1::shared_ptr.  , ਬ,  ᫨  Stock  ந  Investment  㭪 createInvestment ॠ ᫥騬 ࠧ:

std::tr1::shared_ptr<Investment> createInvestment()
{
return std::tr1::shared_ptr<Investment>(new Stock);
}

 饭  ꥪ tr1::shared_ptr  ।  ࠧ묨 DLL  ᪠ ⮫  ᠭ  ஡. ꥪ tr1::shared_ptr, 㪠뢠騩  Stock, <>,   DLL   맢 delete,  稪 뫮  Stock ⨣ .
祬,  ࠢ   tr1::shared_ptr,   ⮬,   䥩   ࠢ쭮 ᯮ짮  㤭묨 -  ࠢ쭮.   tr1::shared_ptr  ⮫쪮 ⮩ ᯮᮡ   ᪨ 訡,    ⮨ ⠭.  ࠭ ॠ tr1::shared_ptr 室  ⥪ Boost (. ࠢ 55).  ꥪ shared_ptr  Boost   ࠧ 筮 㪠⥫,   ᪨ 뤥   㦥 楫  , ⭮  ⨫騪, ᯮ 맮 㠫쭮 㭪樨  饭  ⨫騪, ந ᨭ஭ ⮪   祭 稪 뫮  筮 ।. (  ⪫ প 筮, । ᨬ ९.)  ,  ⥫㠫 㪠⥫  ࠧ  筮, ࠡ⠥   ᯮ ⥫ ᪨ 뤥塞 .    ਫ  ⥫  ६ ᯮ  ,  㬥襭 ᫠ 訡 짮⥫  .

 ᫥ 
 訥 䥩  ᯮ짮 ࠢ쭮  㤭 ᯮ짮 ࠢ쭮.   ६ ᯥ  ࠪ⨪   䥩.
  ᯥ祭 ४⭮ ᯮ짮 䥩   ᮣᮢ  ᮢ⨬  ஥묨 ⨯.
  ।饭 訡 ਬ ᫥騥 ᯮᮡ: ᮧ  ⨯, ࠭祭 ⨬ 権  ⨬ ⨯, ࠭祭 ⨬ 祭,  ⠪ ᢮ 짮⥫  易⥩  ࠢ ᠬ.
  tr1::shared_ptr ন 짮⥫᪨ 㭪樨-⨫騪.  ᭨ <஡ ᪮쪨 DLL>   ,  ⭮, ᯮ짮  ⮬᪮ ᢮ ⥪ (. ࠢ 14).



ࠢ 19: ᬠਢ ஥஢   ஥஢ ⨯

 C++,    㣨 ꥪ⭮-ਥ஢ 몠 ணࠬ஢,  ।   ।  ⨯. ⮬   ६   ࠧࠡ稪 C++ 㤥   ᮢ襭⢮ 襩 ⥬ ⨯.  ,   -   ࠧࠡ稪 ᮢ,    ࠧࠡ稪 ⨯. ॣ㦥 㭪樨  , ࠢ ।  ᢮ , । 樠樨  浪 㭨⮦ ꥪ⮢ -   室   㪠. ⮬   室  ஥஢ ᮢ ⠪,  ࠧࠡ稪 몠 室  ஥஢ ஥ ⨯.
஥஢  ᮢ - ⢥⢥ ࠡ,  ⨬  ᪠. 訥 ⨯  ⢥ ᨭ⠪, ⨢ ਭ ᥬ⨪     䥪⨢ ॠ権.  C++  ᯫ஢ ।   ᤥ  ⨦    楫.  ࠪ⨪ ந⥫쭮 㭪権-童     ⮣,   .
⠪,   ஥஢ 䥪⨢ ? ० ᥣ   ,  祬  . ஥஢    ⠢ । ࠧࠡ稪 , ⢥    ࠭稢 ᯥ  襭:
   ᮧ  㭨⮦ ꥪ  ⨯?  ⢥      ஢  ஢,  ࠢ 㭪権 ।  ᢮  ( new,  new[],  delete   delete[] - .  8), ᫨  ᮡࠥ  ८।.
   ⫨ 樠 ꥪ  ᢠ 祭? ⢥    । ࠧ    ࠬ  ࠬ ᢠ.    樠  ᢠ, ⮬   ᮮ⢥ ࠧ 맮 㭪権 (. ࠢ 4).
  砥  ꥪ⮢  ⨯  ।묨  祭? ,   ஢ । ॠ ।  祭   ⨯.
  ࠭祭  ⨬ 祭 襣  ⨯? 筮 ⮫쪮  樨 祭 -童   ࠢ묨.  樨 । ਠ,   ন .  ਠ 㦥 ,  ᫥ ஫஢ 訡  㭪-童,  ᮡ  ,  ᢠ  㭪 ⠭ 祭 (<setter> functions).   ⠪ ஭ ᪫祭,  㦤  㭪樨,  ᯥ䨪樨  ᪫祭.
 뢠    ⨯   ᫥? ᫥ ᢮   㣨,   ᫥ ࠭祭,  묨 ᠬ.  ⭮, 㦭 뢠,     㭪樨-童: 㠫묨   (. ࠢ 34  36). ᫨  , ⮡ 襬   ᫥ 㣨,  㦭 ⥫쭮 த㬠,  㭪樨  㠫묨; ᮡ  ⭮   (. ࠢ 7).
  ਠ ८ࠧ ⨯ ⨬  襣  ⨯?  ⨯    㣨 ⨯, ⮬    ।ᬮ७ ਠ ८ࠧ  訬 ⨯  㣨? ᫨   ࠧ  ८ࠧ ꥪ ⨯ T1  ꥪ ⨯ T2, ਤ   㭪 ८ࠧ   T1 (  operator T2),      T2,    맢  ⢥ 㬥⮬. ᫨    ࠧ ⮫쪮  ८ࠧ,  㦭 㤥  ᯥ樠 㭪樨,     砥    ࠬ ८ࠧ  -explicit ࠬ   㬥⮬. (ਬ    㭪権 ८ࠧ ਢ  ࠢ 15.)
    㭪樨     ⨯? ⢥    ।  㭪権,     襬 .     㭪ﬨ-童, 㣨 -  (. ࠢ 23, 24  46).
  ⠭ 㭪樨   㯭묨?   㤥  묨 (. ࠢ 6).
      童 襣  ⨯? ⢥     ।,  童   묨 (public),  - 饭묨 (protected)   - 묨 (private).   ।⮨ ,   / 㭪樨   ﬨ ,  ⠪        㣮.
  ⠪ < 䥩> 襣  ⨯?  த ࠭⨨   ।⠢ ⭮⥫쭮 ந⥫쭮, ᭮ ⭮⥫쭮 ᪫祭 (. ࠢ 29)  ᯮ짮 ᮢ (ਬ, ஢  ᪮ )?  த ࠭⨨ । ࠭祭  ॠ 襣 .
 ᪮쪮 騩   ⨯? ,  ⢨⥫쭮   ।  ⨯. ,  । 楫 ᥬ⢮ ⨯. ᫨ ⠪,   㦭 ।   ,   蠡 .
 ⢨⥫쭮   ⨯ ।⠢ ᮡ ,   㦭? ᫨  ।  ந  ⮫쪮  ⮣, ⮡  㭪樮쭮 饣 , , , ⮩ 楫    ।    㭪権-童  蠡.
    ⢥, ⮬ । 䥪⨢ ᮢ -  .     믮 । 짮⥫ﬨ  C++  ⨯,  祬  㯠 ஥  㦥 ࠢ뢠   ᨫ.

 ᫥ 
 ஥஢  -  ஥஢ ⨯. ० 祬 ।  ⨯, 㡥,  ᬮ७  ,  㦤  饬 ࠢ.



ࠢ 20: ।⠩ ।  뫪  const ।  祭

 㬮砭  C++ ꥪ ।  㭪樨   㭪ﬨ  祭 (᢮⢮, 㭠᫥  C). ᫨  㪠 ⨢, ࠬ 㭪樨 樠 ﬨ ॠ 㬥⮢,  ᫥ 맮 㭪樨 ணࠬ 砥  頥 㭪樥 稭.  ࠡ뢠 ࠬ ஢. ⮬ ।  祭    樥. ਬ, ᬮਬ ᫥  ᮢ:

class Person {
public:
Person(); // ࠬ 饭  
virtual ~Person(); // . ࠢ 7 - 祬 㠫
...
private:
std::string name;
std::string address;
};
class Student: public Person {
public:
Student(); //   ࠬ 饭
~ Student();
...
private:
std::string schoolName;
std::string schoolAddress;
};

 ﭨ  ᫥騩 ,  뢠 㭪 validateStudent,  ਭ 㬥 Student ( 祭)  頥 ਧ  ४⭮:

bool validateStudent(Student s); // 㭪 ਭ ࠬ
// Student  祭
Student plato; // ⮭ 稫  
bool platoIsOk = validateStudent(plato); // 맮 㭪樨

 ந室  맮 ⮩ 㭪樨?
᭮,  뢠  ஢ Student  樠樨 ࠬ plato.  ᭮,  s 㭨⮦    validate-Student. ⮬ । ࠬ  祭 ⮩ 㭪樨 室   맮  ஢ Student   맮  Student.
    . ꥪ Student ᮤন  ᥡ  ꥪ string, ⮬  ࠧ,    ꥪ Student,   ⠪ ஢    ꥪ.  Student ᫥  Person, ⮬  ࠧ,  ꥪ Student,   ᪮஢  ꥪ Person.  ꥪ Person ᮤন   ꥪ string, ⮬  ஢ Person   ᮡ  맮  string. ⠪, । ꥪ Student  祭 ਢ   맮  ஢ Student,  맮  ஢ Person  ६ 맮 ஢ ஢ string.   ꥪ Student ࠧ蠥,  맮  ᮮ⢥ 맮 , ⮬  ⮨ । Student  祭 ⠢  ஢   ஢!
 ,  ४⭮  ⥫쭮 .   殢,  , ⮡   ꥪ 뫨  樠஢  㭨⮦.    뫮    ᯮᮡ ய   맮 ஢  ஢. ᮡ !  - ।  뫪  ⠭:

bool validateStudent(const Student& s);

 ᯮᮡ ࠧ 䥪⨢:  뢠    , ᪮  ᮧ   ꥪ. 䨪 const    ࠬ . 室  validateStudent ਭ ࠬ Student  祭, 맢訩    ⮬,   饭   ,  㭪    ।  ꥪ; validateStudent ᬮ ஢ ⮫쪮  .  ,  Student ।  뫪, 室   const, ᪮  ⨢ 砥 뢠 ணࠬ  ᯮ  ⮬, ⮡ validateStudent  ᨫ   ।  ꥪ.
। ࠬ஢  뫪 ⠪   ஡ < १ > (slicing).  ꥪ ந  । ( 祭)  ꥪ  , 뢠  ஢  ,   ,  ਭ ந, <१>.   ⠥ ⮫쪮 ⮩ ꥪ   -   ⢥, ⠪   ᮧ   .   ᥣ  ,   㦭. ਬ, ।,   ࠡ⠥  ஬ ᮢ  ॠ樨 ᪮  ⥬:

class Window {
public
...
std::string name() const; // 頥  
virtual void display() const; //     ᮤন
};
class WindwoWithScrollBars: public Window {
public:
...
virtual void display() const;
};

 ꥪ  Window  , ஥    ।⢮ 㭪樨 name,      ⮡ࠦ,   㪠뢠 稥 㭪樨 display.  䠪,  display - 㭪 㠫쭠,   ⮬,  ᯮᮡ ⮡ࠦ  ꥪ⮢   Window  ⫨  ᯮᮡ ⮡ࠦ ꥪ⮢ WindowWithScrollBar (. ࠢ 34  36).
 ।,     㭪,  㤥     ⥬ ⮡ࠦ .   ᯮᮡ ᠭ ⠪ 㭪樨:

void printNameAndDisplay(Window w) // ࠢ쭮! ࠬ
{ //   <१>
std::cout << w.name();
w.display();
}

ᬮਬ,  , ᫨ 맢  㭪, ।  ꥪ WindowWithScrollBar:

WindowWithScrollBar wwsb;
PrintNameAndDisplay(wwsb);

ࠬ w 㤥 ᪮஢ -  ।  祭, ?-  ꥪ Window,   ⥫쭠 ଠ,    ꥪ⮬ WindowWithScrollBar, 㤥 १.  printNameAndDisplay w ᥣ 㤥  ᥡ  ꥪ  Window (⮬     ꥪ  Window), ᨬ  ⨯ ꥪ,  ⢨⥫쭮 । 㭪樨.  ⭮, 맮 㭪樨 display  printNameAndDisplay ᥣ 맮 Window::display   - WindowWithScrollBar::display.
ᮡ 襭 ஡ <१> - । w  뫪  ⠭:

void printNameAndDisplay(const Window& w) // ࠢ쭮, ࠬ
{ //    <१>
std::cout << w.name();
w.display();
}

 w  ᥡ ࠢ쭮,      ।⠢  ⢨⥫쭮.
᫨  ﭥ < > C++,  㢨,  뫪 筮 ॠ  㪠⥫, ⮬ । 祣-  뫪 筮 砥 । 㪠⥫.  १ ꥪ ஥ ⨯ (ਬ, int) ᥣ  䥪⨢ ।  祭, 祬  뫪. ⮬  ஥ ⨯, ᫨    롮 - ।  祭   뫪  ⠭,    ।  祭.   ᮢ ᠥ ஢  㭪樮 ꥪ⮢ STL, ⮬   ᯥ樠쭮 ஥஢  ।  祭. ணࠬ, ॠ騥   㭪樮 ꥪ, ⢥  , ⮡ ᯥ 䥪⨢ ।   祭  ᪫ <१>.  ਬ ⮣,   ࠢ  ᨬ  ᯮ㥬   C++ (. ࠢ 1).
஥ ⨯  訬 ꥪ⠬, ⮬   뢮,   ஥ ⨯ - 訥   ।  祭,  ᫨  । 짮⥫. ⥫쭮. ,  ꥪ 让,   ,  맮   ஢  襢.  ꥪ - ।  設⢮ ⥩஢ STL - ᮤঠ  ᥡ   筮 㪠⥫,  ஢ ⠪ ꥪ⮢   ᮡ ஢ ᥣ,    㪠뢠.    祭 ண.
  쪨 ꥪ    ஢,  ࠢ   뢠 ﭨ  ந⥫쭮.   ᬠਢ ஥  짮⥫᪨ ⨯ -ࠧ,  ᫨    ७ ।⠢. ਬ,    ࠧ ꥪ, 騥    double  ॣ,  ᫨ ⮢ ࠧ ⠬ 祭 ஥ ⨯ double.  ⠪   । ꥪ  뫪, ⮬   ᫮ ⮢   ॣ 㪠⥫ (ॠ騩 뫪).
㣠 稭 ⮣, 祬 쪨 짮⥫᪨ ⨯  易⥫쭮   ।  祭, 砥  ⮬,   ࠧ থ . ,   ᥣ,    饬, ⮬   ७ ॠ  .     ⮬ 砥, ᫨  ४砥   ॠ C++. ਬ,   ॠ ⨯ string  ⠭⭮ ⥪  ᥬ ࠧ , 祬  㣨.
 , ⢥ ⨯,    ।,  ।  祭 㤥 ண,-  ஥ ⨯,  ⠪   㭪樮 ꥪ STL.  ᥣ ⠫쭮 ᫥ ᮢ ⮣ ࠢ  । ࠬ  뫪  ⠭  ।  祭.

 ᫥ 
 ।  祭 ।⠩ ।  뫪  ⠭. 筮   䥪⨢    ஡ <१>.
  ࠢ  ᠥ ஥ ⨯, ஢  㭪樮 ꥪ⮢ STL.   ।  祭 筮 室 .



ࠢ 21:  ⠩  뫪,    ꥪ

 ⮫쪮 ணࠬ ᮧ ஡ 䥪⨢, 易  ।祩 ꥪ⮢  祭 (. ࠢ 20), ,  ⮭栬, २ᯮ 訬 ᪮७  - ।  祭 - ,     ⠫. ४  ᢮ <⮬> 뢥,    ᪠ ⠫ 訡: 稭 ।  뫪 祭  ꥪ⮢.   ࠢ쭮.
ᬮਬ   ।⠢ 樮 ᥫ, 騩  ᥡ 㦥⢥ 㭪  ६  ⠪ ᥫ:

class Rational {
public:
Rational(int numerator = 0, // . ࠢ 24 - 祬 
int denominator = 1); //   explicit
...
private:
int n, d;
friend
const Rational // . ࠢ 3 -
operator*(const Rational& lhs, // 祬 頥 ⨯ const
const Rational& rhs);
};

᭮,    operator* 頥 १騩 ꥪ  祭,   㦨  ᨮ 室, ᫨   㤥      ᮧ  㤠 ꥪ.      ,     .  :    ?
, ᫨   뫪.  뫪 -   㣮  ண 饣 ꥪ. 直 ࠧ, ⠫   뫪,    ᥡ:  祣 ।祭  ,    ਭ 祬-.  砥 operator*, ᫨ 㭪 頥 뫪, ,    뫪   㦥 騩 ꥪ Rational,   ᮤন ந  ꥪ⮢,  ᫥ ६.
祢,   ᭮ ,  ⠪ ꥪ   맮 operator*. ਬ, ᫨   

Rational a(1, 2); // a = 1/2
Rational a(3, 5); // b = 3/5
Rational c = a*b; // c  ࠢ 3/10

 ࠧ㬭 ,  㦥   樮쭮 ᫮  祭  . ᫨ operator* 㤥  ⠪ ᫮,    ᮧ  ᠬ⥫쭮.
㭪  ᮧ  ꥪ ⮫쪮  ᯮᮡ:  ⥪   .   ⥪ ⢫ ।⢮ । 쭮 ६. ᯮ  ⥣,     operator* ⠪:

const Rational& operator*(const Rational& lhs, // ।०!
const Rational& rhs) // 宩 !
{
Rational result(lhs.n * rhs.h, lhs.d * rhs.d);
return result;
}

 室  ⢥࣭ ࠧ, ⮬  襩 楫 뫮  맮 ,  result   ᮧ,   㣮 ꥪ. ஬ ⮣,  㭪 ஦   쥧 ஡, ᪮ 頥 뫪  result,  result -   ꥪ,   ꥪ ࠧ  襭 㭪樨,  ன  .  ࠧ,   operator* 頥 뫪   Rational,   訩 Rational - ⮩, ⥫,  ᪥ ⮣,  - 뫮 ꥪ⮬ Rational,  㦥   ⠪, ⮬   㭨⮦. ⮨ 맢  㭪 -     । . :  㭪,  頥 뫪   ꥪ, ४⭠ (  ᠥ  㭪権,  㪠⥫   ꥪ).
 ⥯  ᬮਬ  ஢ ꥪ  <>  ⮬ 뫪  . ꥪ  <> ᮧ ।⢮ new.     룫拉 operator*  ⮬ 砥:

const Rational& operator*(const Rational& lhs, // ।०!
const Rational& rhs) //  宩 !
{
Rational *result = new Rational(lhs.n * rhs.h, lhs.d * rhs.d);
return *result;
}

,    ਤ ᯫ稢  맮 , ᪮ , 뤥塞 new, 樠 맮 ᮮ⢥饣 ,  ⥯   ஡:  믮 delete  ꥪ, ᮧ   ᯮ짮 new?
 ᫨ 뢠 ணࠬ ᠭ ⭮  ᮢ⭮,   ⭮,   ।   ᫥饬  ⢥ 業ਨ:

Rational w, x, y, z;
w = x * y * z; //  ,  operator*(operator*(x, y), z)

 믮  맮 operator*   ।, ⮬   맮 new,   ᮮ⢥⢮  delete.   짮⥫ operator*    ᤥ, ⠪      㪠⥫,   뫪,  頥 㭪 operator*.  ࠭஢ 窠 ᮢ.
, ,  ⨫,   室 ( ᭮ ⥪   ᭮ ) ࠤ  室 맮 ஢   頥 祭 operator*. ᯮ,  室  ⠢ ᥡ 楫   뢠 .  ,  㬠,  ,   ᥣ,  맮 , ஬ .  ᪫祭,   ਤ㬠 ᫥ ॠ 㭪樨 operator*,  頥 뫪  ᪨ ꥪ Rational, ।  㭪樨:

const Rational& operator*(const Rational& lhs, // ।०!
const Rational& rhs) //   㦥!
{
static Rational result; // ᪨ ꥪ,
//   頥 뫪
result = ...; // 㬭 lhs  rhs  
// ந  result
return result;
}

 ᥬ ஥ 襭  ᭮ ᪨ ꥪ⮢,  ࠧ 뢠 , 易  ᭮ ⭮⥫쭮 ⮪,     祢 ⮪. ⮡ ࠧ拉 , ᬮਬ ᫥騩 ᮫⭮ ࠧ㬭 :

bool operator==(const Rational& lhs, //  ==  Rational
const Rational& rhs);
Rational a, b, c, d;
...
if ((a*b) == (c*d)) {
⢨, 室  砥, ᫨  ந ࠢ;
} else {
⢨, 室  ⨢ 砥;
}

뢠,   ⠪? ࠦ ((a*b) == (c*d)) 㤥 ᥣ ࠢ true ᨬ  祭 a, b, c  d!
 ᥣ  ᭥ ⠪ ⭮ , ९ ஢  ࠢ⢮  ⭮ 㭪樮쭮 ଥ:

if(operator==(operator*(a, b), operator*(c, d)))

,   뢠 operator==, 㦥   ⨢ 맮 operator*,    㤥  뫪  ᪨ ꥪ Rational  operator*.  ࠧ, operator== 㤥 ࠢ ᪨ ꥪ Rational, ।  㭪樨 operator*,  祭 ᪮ ꥪ Rational  ⮩  㭪樨. 뫮  㤨⥫쭮, ᫨     ࠢ ᥣ.
⮣   筮, ⮡ 㡥 ,  饭 뫪  㭪樨,  operator*,-   ६,    ⮫쪮 , ⮡   . -  騩  㬠: <, ᫨ 筮  ᪮ ꥪ, ,  ,  ⮣  ᪨ ᨢ:>
  ᭨  ⮣, ⮡  ⠪ ணࠬ ⤥ ਬ,    ᭨, 祬   ⠪   ࣠   . -,    n - ࠧ ᨢ. ᫨ n ᫨誮 ,       ࠭,   祣  먣ࠥ  ࠢ  襮ᠭ ணࠬ. ᫨  n  ,  㬥蠥 ந⥫쭮 襩 ணࠬ, ᪮  ꥪ  ᨢ   ࢮ 맮 㭪樨.  㤥 ⮨  n 맮 ஢  n 맮 ஢,  ᫨  㭪 뢠 ᥣ  ࠧ. ᫨  襭 ந⥫쭮 ணࠬ ᯥ祭 뢠 ⨬樥, ⮣ ᠬ ୮  ந室饬 - <ᨬ>.  , 㬠  ⮬,   室  祭  ᨢ ꥪ⮢     .  אַ ᯮᮡ । ꥪ⮢ -  ᢠ,   祬  易?  饬 砥  맮  ( 㭨⮦ ண 祭)  맮  ( ஢  祭).   楫 -  맮   !    쬠 㤠筠 (-: ਬ ஢  ᨢ   ).
ࠢ ᯮᮡ ᠭ 㭪樨 砥  ⮬,      ꥪ.  ਬ  operator*   Rational  砥  ᫥騩 ,   宦:

inline const Rational operator*(const Rational& lhs, Rational& rhs)
{
return Rational(lhs.n*rhs.h, lhs.d*rhs.d);
}

筮,  ⮬ 砥  ਤ ᬨ  প  맮     ꥪ⮢, 頥 operator*,   쭮 ⠡   業  ४⭮ . ⮬, ⭮,   ⠪  譮.  ᥬ 몠 ணࠬ஢, C++  ࠧࠡ稪 ஢ ਬ ⨬  襭 ந⥫쭮 㥬 , ,  뢠,    맮    頥 operator* 祭  ᭮ ࠭.    ⮩  (   ⠪  㯠),  ணࠬ த  , 祣    ,   ॥, 祬 .
 ⮣:   롨ࠥ  ⮬ 뫪  饭 ꥪ,   砥  ⮬, ⮡  ࠡ⠫ ࠢ쭮.  ⮬,  ᤥ  롮  ,   ࠧࠡ稪 .

 ᫥ 
   頩 㪠⥫  뫪   ꥪ, 뫪  ꥪ, ।  <>,  㪠⥫  뫪   ᪨ ꥪ, ᫨  蠭,   , 祬   ⠪ ꥪ.  ࠢ 4 ਢ ਬ 樨,   뫪   ᪨ ꥪ  ,  ࠩ ,   ।.



ࠢ 22:  -童 묨

 ⮬ ࠢ  ਬ  ⮬, 祬 -童    묨 (public). ⥬  㡥,   㬥 ⨢  -童  ⠪ 饭 (protected).  ਢ   뢮,  -童   묨 (private),   ⮬  ⠢ .
⠪,  -童. 祬 ?
筥  ᨭ⠪᪮ ⨢稢 (. ⠪ ࠢ 18). ᫨ -童   묨,  ⢥ ᯮᮡ  짮⥫   ꥪ - १ 㭪樨-童. ᫨   䥩 㤥   㭪権,  짮⥫  㦭 㤥  ,  ᯮ,  㦭 ਬ ᪮,   - ,      童 .   ⠢ ᪮, ᪮ 祣, ஬ 㭪権,  .    .
,  ,   ⠥ 㬥  ⨢稢 㡥⥫.   ⮣ 䠪,  ਬ 㭪権 ᯥ稢  ⮭ ன 㯠  -童? ᫨  ᤥ -童 묨,  㤥      ⥭  ,  ᫨  ᯮ 㭪樨  祭  ⠭ 祭,  ᬮ   , ࠧ ⮫쪮 ⥭  ⥭-.   ᬮ ॠ  ⮫쪮  , ᫨ :

class AccessLevels {
public:
...
int getReadOnly() const { return readOnly;}
void setReadWrite(int value) { readWrite = value;}
int getReadWrite() { return readWrite;}
void setWriteOnly(int value) { writeOnly = value;}
private:
int noAccess; //  㯠  ⮬ int
int readOnly; //   ⮬ int ⮫쪮  ⥭
int readWrite; //   ⮬ int  ⥭  
int writeOnly; //   ⮬ int ⮫쪮  
};

  ஫ 㯠 , ⮬   -童   .  뢠 ⠪, ⮡ 童 㦤   㭪樨 祭,   㭪樨 ⠭ 祭.
   㡥?  ᠬ ६ ⠢ 殮 ⨫: ! ᫨  ॠ    १ 㭪樨,   ᬮ  ࠭,   -童,    짮⥫ 襣  ⮣  .
ਬ, ।,    ਫ,  ஬ ⮬᪮ ன⢮ ᫥ ᪮ 室 ⮬.  ⮬ ஥ ,  ᪮        ᪮:

class SpeedDataCollection {
...
public:
void addValue(int speed); //   祭
double averageSoFar() const; //  । ᪮
...
};

 ᬮਬ ॠ 㭪樨-童 averageSoFar. , ਬ,    童,  ।⠢ । 䬥᪮ 祭 ᪮⥩,    .  㭪 averageSoFar 㤥   祭 ⮣ 童 . 㣮 室 砥  ⮬, ⮡  । 祭 ᪮  㭪樨 averageSoFar   맮,  祣  ਤ ᬮ    樨.
 室 (࠭ ⥪饩 । ᪮) 㢥稢 ࠧ  ꥪ SpeedDataCollection, ⮬  室 뤥   童 , ࠭饣 ⥪饥 ।,  ⮣  ⢮ ⮢ .  ⮬ averageSoFar   ॠ 祭 䥪⨢:  㤥  ஥ 㭪 (. ࠢ 30),  頥 祭 ⥪饣 ।.  ⨢ ⮬ ᫥   ᤥ  㭪 ,   ꥪ SpeedDataCollection ⠭ .
 ᪠ -  ?  設  쪮  (ਬ, ஥ ன⢠, ⠭  ண),   ਫ,  । 祭 ॡ ,  ᫥   맮, , ।⠢ 襥 襭.   ਫ,  । 祭 㤥 訢 , ᪮ ॠ樨 ⢥,   -  ஡, ࠭ ⥪饣 । 筮 ।⥫쭥.  ⬥, ,    । १ 㭪-童 (  㫨஢ ),     ॠ,  ⮬ ணࠬ- ਤ ᥣ  ४஢.    ⮣ 㤮⢠, ᫨ ᫥ 孨, ᠭ  ࠢ 31.
⨥ -童  䥩ᮬ 㭪権  ᯥ  ॠ樨  ࠧ ⭮襭. ਬ,  砥 饭 㣨 ꥪ⮢  ⮬,   童  ந室 饭  ⥭  , ᯥ稢  ஢ ਠ  믮 ।-  ᫮,  ॠ ᨭ஭  筮 ।  . . ணࠬ,  諨  C++  ⠪ 몮,  Delphi  C#, 㢨  ⮩    <᢮⢠> (<properties>), 騬   몠, ࠢ,   <᢮⢠> 室  ᪮.
砭  樨 , 祬    ࢮ 鸞. ᫨  뢠 -童  짮⥫ (  㫨 ),   ᯥ  ਠ⮢ , ᪮     ⮫쪮 㭪樨-童.  ⮣,  ࠭  ᮡ ࠢ   ॠ. ᫨    뢠 ᢮ 襭,  祭 ᪮ 㦨,   ᫨    室  ,  ᯮᮡ    童 १砩 ࠭祭, ⮬   ⮬ ⠭ ࠡ ᫨誮  ᪨ ணࠬ.  砥 ⢨ 樨,   ࠪ⨪ <㫨஢> 砥 <塞>, ᮡ ᫨    ,  諨 ப ਬ.   ࠧ ப ᯮ㥬   㦤  樨, ᪮   㣨  먣   ன ॠ樨   ᮢ襭.
㬥 ⨢ 饭 (protected) -童 . ᪨     ⫨稩,  砫  ,    ⠪. 㦤  ᨭ⠪᪮ ⨢稢  ⮭ ࠨ 㯥  ⮩    饭 童,   ,    樨?   饭   㫨஢묨, 祬 ?    ࠭,   ࠪ⨪ - .
 ࠢ 23 ,    魮 ⭮ ய樮쭠 ꥬ ,    ࠡ, ᫨  魮 .  ࠧ, ⥯ 樨 童  ⭮ ய樮쭠 ꥬ ,  ⠭ ࠡ, ᫨  童 , ਬ 㤥    (, ⠢ 塞,   ਬ averageSoFar ).
।,     童 ,   ᪫砥   .     ஭?  ᪨ ,  ᯮ , ꥬ ண,  ࠢ, ⥭.  -童, ⠪ ࠧ, ᮫⭮  㫨஢.  ।,  ᪫砥 饭 童 . 쪮  㤥 ஭ ⥯?  ந , ⢮    ⭮.  ࠧ, 饭 童-  㫨஢  ⮩  ⥯,   , ᪮     ࠣ ᪨  ⭮ ꥬ.   祢, ,   ᪠  ࠧࠡ稪 ⥪,  -⠪ ࠢ.  ⮫쪮   童    饭  짮⥫ 砫   ,  - ⠭ 祭 㤭. 誮   㦭 ९뢠, ୮ ஢, 㬥஢  ४஢.  窨 ७ 樨,   ⮫쪮  ஢ 㯠:  (ᯥ稢騩 )   ⠫ ( ᯥ稢騥).

 ᫥ 
  -童 묨 (private).   ⠬ ᨭ⠪᪨ த   , ᯥ稢  ⮭ ࠢ 㯮,  ࠭஢ ਠ⭮  ।⠢ ࠬ ॠ樨 ᮢ .
 饭 童   㫨஢, 祬 .



ࠢ 23: ।⠩ 㭪-童 㭪樨,  騥  童,  ﬨ 

쬥   ।⠢ Web-㧥.  ᫥  ⠪   । 㭪樨,    㦥 ⮢,   饭 URL  㤠  ⥬  <㪨> (cookies):

class WebBrowser {
public:
...
void clearCache();
void clearHistory();
void removeCookies();
...
};

 짮⥫,   믮   ⢨ , ⮬ WebBrowser  ⠪ ।⠢ 㭪   ⮩ 楫:

class WebBrowser {
public:
...
void clearEveryThing(); // 뢠 clearCache(), clearHistory()
//  removeCookies()
...
};

筮, ⠪ 㭪樮쭮   ᯥ祭 ⠪ 㭪樥,  饩 童 ,  맮 ᮮ⢥騥 㭪樨-童:

void clearBrowser(WebBrowser& wb)
{
wb.clearCache();
wb.clearHistory();
wb.removeCache();
}

  - 㭪-童 clearEverything  ᢮ 㭪 clear-Browser?
ਭ樯 ꥪ⭮-ਥ஢ ஥஢ ,    㭪樨,   ,   易 ,   ।,  㭪-童 - 訩 롮.  ᮦ,  । ୮.  ᭮   ⮣,  ⠪ <ꥪ⭮-ਥ஢>. ,  ꥪ⭮-ਥ஢ ணࠬ    㫨஢, ᪮쪮 .  ⨢ ⨢  㭪-童 clearEverything  ⢨⥫쭮  㫨஢, 祬 ᢮ 㭪 clearBrowser.  ⮣, ।⠢ ᢮ 㭪樨  ᯥ    <㯠> 㭪樮쭮  WebBrowser,   ਢ  襬  ᨬ⥩  ⠯ 樨     ७ . ⮬ ᢮ 㭪    稭.   ⫨ .
筥  樨. ᫨  魮 㫨,  뢠  .    魮 㫨஢, ⥬  ⥩ ணࠬ   .   ⥩ ணࠬ    魮, ⥬      ᥭ , ᪮      ⥩,    .  ࠧ, 祬  ⥯ 樨 魮, ⥬       .  稭 ⮣, 祬  ⠢   ࢮ :  ᯥ稢      ⠪ ࠧ,   ࠣ 쭮 ⢮ 짮⥫.
ᬮਬ , 樨஢  ꥪ⮬.    ,     (     ), ⥬  襩 ⥯  㫨஢  ⥬ ᢮     ࠪ⨪, ਬ ⢮ 童-,  ⨯  . . 㡮 業 ꥬ ,     童 ,   ᫮ 㭪権,    : 祬  ⠪ 㭪権, ⥬  㫨஢ .
 ࠢ 22 ,  -童   묨, ⮬   ⨢ 砥     ࠭祭 ᫮ 㭪権.    㫨஢.    -童 ⢮ 㭪権,    , । ⢮ 㭪権-童   ⢮ 㭪権-㧥, ⮬     童 ࠧ襭 ⮫쪮 㭪-童   . ᫨  롮  㭪樥-童 (    ⮫쪮    ,  ⠪    㭪, ᫥, । ⨯ (typedef)  . .)  ᢮ 㭪樥,  饩  ⮬  㣮  (⠪ 㭪樨   㯠   祬  襯᫥),  ᯥ稢饩   㭪樮쭮,  訢 祢 뢮:   ᯥ稢 㭪,    童,  㣮, ⮬    㢥稢 ᫠ 㭪権,      ⮩ ᥪ樨 .  , 祬 clearBrowser (᢮ 㭪) ।⥫쭥, 祬 clearEverything (㭪-童).
 ⮨     . ࢮ -  ᪠ ⭮ ⮫쪮  ᢮ 㭪,  騬 ﬨ .   ⠪     童 ,   㭪樨-童,  ⮬ 筮 ⠪    .  窨 ७ 樨, 롮 ᫥    㭪ﬨ-童  ᢮묨 㭪ﬨ,   㭪ﬨ-童,   ஭,  ᢮묨 㭪ﬨ,  騬 ﬨ,-  㣮. ( 業 ஥⭮ 襭 , 筮,  ⮫쪮  窨 ७ 樨.  ࠢ 24 ,    ᠥ  ਢ ⨯,  롨   㭪ﬨ-童  ᢮묨 㭪ﬨ.)
-,  ⮣,    樨 ॡ, ⮡ 㭪  뫠 童 ,   ᫥,   㭪    童 - 㣮 .     ணࠬ⠬, ਢ訬  몠,    㭪樨   童 ᮢ (ਬ, Eiffel, Java, C#  . .). ਬ,   ᤥ clearBrowser ᪮ 㭪樥-童 ண 㦥 .         ( 㣮)  WebBrowser,    ᪠  樨   童.
 C++  ⢥  clearBrowser ᢮ 㭪樥  ⮬  ࠭⢥ ,    WebBrowser:

namespace WebBrowserStuff {
class WebBrowser {...};
void clearBrowser(WebBrowser& wb);
...
}

    ⮫쪮  ⢥,  ࠭⢠ ,  ⫨稥  ᮢ,   室  ᪮쪨 室 䠩.   , ⮬  㭪樨 த clearBrowser  ᯮ⥫묨.    童,  ﬨ ,    ᯥ樠쭮 㯠  WebBrowser       ,   짮⥫  WebBrowser  ⠪ 㦥 뫨.   㭪樨 clearBrowser, 짮⥫   ᠬ⥫쭮 맢 clearCache, clearHistory  removeCookies.
 ,  WebBrowser,  뫮  ।  ⠪ ᯮ⥫ 㭪権:  ࠡ  , 뢮  , ࠢ <㪠>  . .  , 設 짮⥫   ⮫쪮    㭪権.      짮⥫᪮ ணࠬ,  ன ᯮ ⮫쪮 㭪樨, ⭮騥  ,  , ਬ,   㭪権 ࠢ <㪠>?  ⮩ ᯮᮡ ࠧ  -   㭪樨, ⭮騥  ,   筮 䠩, 㭪樨 ࠢ <㪠> -  㣮, 㭪樨 প  -  쥬  ⠪ :

//  "webbrowser.h" -   ᠬ  WebBrowser,
//  ⠪  㭪樮쭮, 饩   ⭮襭
namespace WebBrowserStuff {
class WebBrowser{...};
... //  㭪樮쭮,  
// 㭪樨-童, 㦭  ᥬ
// ⠬
}
//  "webbrowserbookmarks.h"
namespace WebBrowserStuff {
... // ᯮ⥫ 㭪樨, 騥
} // 
//  "webbrowsercookies.h"
namespace WebBrowserStuff {
... // ᯮ⥫ 㭪樨, 騥
} // "㪮"
...

⬥⨬,   ⠪ ࣠ ⠭⭠ ⥪ C++.  ⢥ ⭮  <++ StandardLibrary>, ᮤঠ饣 ,    ࠭⢥  std,  ⪨    䠩 (ਬ, <vector>, <algorithm>, <memory> . .).       㭪樮쭮  std. 짮⥫, ஬ 㦭 ⮫쪮 ,   ⭮襭  ࠬ,     ᢮ ணࠬ ४⨢ #include <memory>,  짮⥫,  㦤騩  ᯨ᪠,  易  #include <list>. ⮬  ⠯ 樨 짮⥫᪨ ணࠬ  ⮫쪮   ⥩ ⥬,   ⢨⥫쭮 ᯮ (. ࠢ 31 㦤 㣨 ᯮᮡ 㬥襭 ᨬ⥩ 樨).  ࠧ 㭪樮쭮 , ᫨  ᯥ稢 㭪ﬨ-童 , ⮬     । ,   ࠧ  .
饭 ᯮ⥫ 㭪権  ࠧ  䠩,    ࠭⢥  - 砥 ⠪,  짮⥫     ᯮ⥫ 㭪権.  ⮣ 㦭    㭪樨 (童  )    ࠭⢮ . ਬ, ᫨ 짮⥫  WebBrowser   ᯮ⥫ 㭪樨, 騥 ⭮襭  㧪 ࠦ,   㤥 ᮧ  䠩, 騩   㭪権  ࠭⢮  Web-BrowserStuff.  㭪樨 ⠭ ᫥ ⮣ ⠪  㯭,    稥 ᯮ⥫ 㭪樨.    ᢮⢮, ஥   ।⠢ , ⮬  । ᮢ   ७ ⠬. 筮,   ᮧ ந ,      㯠  㫨஢ (  ) 童  , ⮬ ⠪ ࠧ <७> 㭪樮쭮 㦥   ࢮ᭮. ஬ ⮣,  ࠢ 7 ,     ।祭  ⮣, ⮡  묨.

 ᫥ 
 ।⠩ 㭪-童 㭪樨,  騥  童,  ﬨ .  蠥 ⥯ 樨  塞,  ⠪  <㯠> 㭪樮쭮.



ࠢ 24:  㭪樨,  騥 童,  ८ࠧ ⨯   ਬ  ᥬ ࠬࠬ

   ⬥砫,   饬 砥 প ᮬ  ८ࠧ ⨯ - 㤠筠 . , 筮,  ⮣ ࠢ  ᪫祭,      ᠥ ᮧ ᫮ ⨯. ਬ, ᫨  ஥   ।⠢ 樮 ᥫ,   ८ࠧ 楫 ᫠  樮쭮 룫廊  ࠧ㬭.   类 砥   ࠧ㬭, 祬 ஥  C++ ८ࠧ int  double ( 㤠 ࠧ㬭 ஥ ८ࠧ  double  int).  ⠪,     Rational  뫮  ᫥騬 ࠧ:

class Rational {
public:
Rational(int numerator = 0,
int denominator = 1); //  ᮧ⥫쭮  explicit;
// ᪠  ८ࠧ
// int  Rational
int numerator() const; // 㭪樨 㯠  ᫨⥫ 
int denominator() const; // ⥫ - . ࠢ 22
private:
...
};

 ,   ন 䬥᪨ 樨 (᫮, 㬭  . .),   㢥७, ᫥ ॠ뢠  ।⢮ 㭪権-童  ᢮ 㭪権, ,  ﬨ . ⨭ : < - ਤন ꥪ⭮-ਥ஢ 室>.  , , ᪠, 㬭 樮 ᥫ ⭮   Rational, ⮬  ⢥ ॠ operator*  ᠬ ⮬ .  ४ 樨 ࠢ 23 ⢥ত,   饭 㭪樨  ,    樨஢,  ⨢ ꥪ⭮-ਥ஢ ਭ樯. 祬, ⠢  ६  ⥬  ᬮਬ,   뫨  operator* 㭪樥-童 Rational:

class Rational {
public:
...
const Rational operator*(const Rational& rhs) const;
}

᫨   , 祬  㭪   ⠪ ࠧ (頥 ⠭ १  祭  ਭ 뫪  const  ⢥ 㬥),   ࠢ 3, 20  21.
 襭   㫨஢ 樮묨 ᫠:

Rational oneEighth(1, 8);
Rational one Half(1, 2);
Rational result = oneHalf * oneEighth; // ࠢ쭮
result = result * oneEighth; // ࠢ쭮

   㤮⢮७. ⥫  ন ⠪ ᬥ蠭 樨, ⮡ Rational  뫮 㬭, ਬ,  int.   殢,  쭮 ⢥ -   ६  ᫠,  ᫨  ਭ  ࠧ ᫮ ⨯.
 ᫨  ⠥ 믮 ᬥ蠭 䬥᪨ 樨,  㦨,   ࠡ ⮫쪮   砥:

result = oneHalf * 2; // ࠢ쭮
result = 2 * oneHalf; // 訡!

 宩 .    ⨢ (   浪 ᮬ⥫), ?
筨 ஡ ⠭ , ᫨ ९  ᫥ ࠦ  㭪樮쭮 ଥ:

result = oneHalf.operator*(2); // ࠢ쭮
result = 2.operator*(oneHalf); // 訡!

ꥪ oneHalf -   , 饣  ᥡ operator*, ⮬  뢠  㭪.   楫 ᫮ 2  樨஢  ,  ,     㭪樨 operator*.  㤥 ⠪ ᪠ 㭪樨 operator*,  騥 童  ( ⥪饬 ࠭⢥    쭮  ):

result = operator*(2, oneHalf); // 訡!

   砥   ᢮ 㭪樨 operator*,  ਭ  㬥 int  Rational, ⮬   祬.
ᬮਬ  ࠧ  ᯥ 맮. ,  ன ࠬ - 楫 ᫮ 2,  Rational::operator* ਭ  ⢥ 㬥 ꥪ Rational.  ந室? 祬 2 ࠡ⠥   樨   ࠡ⠥  㣮?
ந室  ८ࠧ ⨯.  ,   । int,  㭪 ॡ Rational,   ⠪ ,    室騩 ꥪ, ᫨ 맢  Rational c ।  㬥⮬ int.    㯠. 묨 ᫮,  ࠪ   맮,  ᫨    ᠭ ਬ୮ ⠪:

const Rational temp(2); // ᮧ ६ ꥪ Rational  2
result = oneHalf * temp; //  ,  oneHalf.operator*(temp);

筮,    ⮫쪮 ⮬,   ,   䨪 explicit. ᫨  䨪 explicit ⢮,     ᫥ ।  ᪮஢ :

result = oneHalf * 2; // 訡! ( 稨 explicit-):
//  ८ࠧ 2  Ratinal
result = 2 * oneHalf; //   訡,   ஡

 ᬥ蠭 䬥⨪  ⠪ 室 ਤ , ,  ࠩ , ⠪  ⨢稢.
 楫, ,- ᯥ  ᮣᮢ,  প ᬥ蠭 䬥⨪,   㦭  ⠪ 襭,  ஬  । .  頥     ⮬, 祬   稨 explicit-   Rational    ,  㣮 - :

result = oneHalf * 2; // ࠢ쭮 (  explicit-)
result = 2 * oneHalf; // 訡! (   explicit-)

뢠,   ࠬࠬ ਬ  ८ࠧ, ⮫쪮 ᫨  ᫥  ᯨ᪥ ࠬ஢.  ࠬ, ᮮ⢥騩 ꥪ,  㭪-童 뢠 (,   㪠뢠 this),   ࣠  ८ࠧ.  祬  맮 ,  ன - .  ࢮ 砥 ࠬ 㪠  ᯨ᪥ ࠬ஢ 㭪樨,   ஬ - .
  ⥫   業 প ᬥ蠭 䬥⨪,  ⥯ ᭮,   ᯥ: 㦥 operator*   ᢮ 㭪樨, ⮣  ᬮ 믮  ८ࠧ  㬥⮢:

class Rational {
... //  ᮤন operator*
};
const Rational operator*(const Rational& lhs, // ⥯ ᢮ 㭪
const Rational& rhs)
{
return Rational(lhs.numerator() * rhs.numerator(),
lhs.denominator() * rhs.denominator());
}
Rational oneFourth(1, 4);
Rational result;
result = oneFourth * 2; // ࠢ쭮
result = 2 * oneFourth; // , ࠡ⠥!

  뫮   ⫨ 殬, ᫨    <>.   operator*  㣮  Rational?
  砥 ⢥⮬ 㤥 <>, ⮬  operator*   ॠ   ନ ⮣ 䥩 Rational. ਢ   뢠,    ᤥ.   室   뢮: ⨢ 㭪樨-童  ᢮ 㭪,  㭪 -  .  ணࠬ  C++ ,  ࠧ 㭪  ⭮襭        童 (ਬ, - 室 ८ࠧ뢠 ⨯  㬥⮢),     㣮.  ਬ 뢠,  ⠪ । ࠢ쭮. ᫨    祭 㭪樨 㣮 ,   ⠪  㯨, ⮬ ,    ॠ쭮 ,   ⠢  嫮, 祬 ⥫ . 筮,  ⭮襭 㦥⢥ ࠢ,  䠪 ⠥ 䠪⮬: ᫨ 㭪    童,   砥 ⮬᪨,     㣮.
  ࠢ,  祣, ஬ ࠢ,     ࠢ.   室  <ꥪ⭮-ਥ஢ C++>  <C++  蠡> (. ࠢ 1)  ॢ頥 Rational    蠡 ,  㯠  ᨫ  䠪,  ᯮᮡ  ,    ஥ 襭.    ⥬ ࠢ 46.

 ᫥ 
 ᫨ ८ࠧ ⨯   ਬ  ᥬ ࠬࠬ 㭪樨 (   ࠬ this),  㭪    童 .



ࠢ 25: 㬠  প 㭪樨 swap,  㦤饩 ᪫祭

swap - ᭠ 㭪. 砫쭮    ⥪ STL     ⠫, -, ᭮  ᠭ ணࠬ,   ᫥ ᪫祭 (. ࠢ 29),  -, 騬 堭 襭   ᢠ ᠬ ᥡ (. ࠢ 11).   swap ⮫쪮 ,   ॠ  ࠢ쭮,  㪠    ᮡ    ᮡ ᫮.  ⮬ ࠢ  ᫥㥬,   ᮡ ।⠢     .
⮡  (swap) 祭  ꥪ⮢, 㦭 ᢮    祭 㣮.  㬮砭 ⠪  ⢫ ⠭  swap.  ⨯筠 ॠ  室  訬 ﬨ:

namespace std {
template <typename T> // ⨯筠 ॠ std::swap
void swap(T& a, T& b) //  ⠬ 祭 a  b
{
T temp(a);
a = b;
b = temp;
}
}

 ᪮ ⨯ ন ஢ (   ஢   ᢠ), ॠ swap  㬮砭  ꥪ⠬ ⮣ ⨯  祭ﬨ  直 ⥫ ᨫ  襩 ஭.
⠭⭠ ॠ swap,  ,  ਢ   .  砥 ஢  ꥪ⮢: a  temp, b  a  temp -  b.   ⨯       ⢨⥫쭮   室.  ⠪ ⨯ swap  㬮砭 -     ஦.
। ⠪ ⨯ ࠧ ⮨ 뤥 ,    ᭮  㪠⥫  㣮 ⨯, ᮤঠ騩 ॠ . 饥   ⠪ ஥ 襭: < pimpl> (pointer to implementation - 㪠⥫  ॠ - . ࠢ 31). ஥஢ ⠪  Widget    ᫥騬 ࠧ:

class WidgetImpl { //    Widget
public: // ⠫ ⢥
...
private:
int a,b,c; // ,   -
std::vector<double> v; // ஢  ண
...
};
class Widget { // , ᯮ騩  pimpl
public:
Widget(const Widget& rhs);
Widget& operator=(const Widget& rhs) // ⮡ ᪮஢ Widget, 㥬
{ //  ꥪ WidgetImpl. ⠫
... // ॠ樨 operator=  ⠪
*pimpl = *(rhs.pimpl); // . ࠢ 10, 11  12
...
}
...
private:
WidgetImpl *pimpl; // 㪠⥫  ꥪ  묨
}; // ⮣ Widget

⮡  祭  ꥪ⮢ Widget, 㦭   祭ﬨ  㪠⥫ pimpl,   swap  㬮砭  ⮬   .  ⮣   ⮫쪮 ਦ 믮  ஢ Widget,     ࠧ ᪮ Widgetlmpl. 祭 䥪⨢!
   ⥫ ᮮ 㭪樨 std::swap,    ꥪ⮢ Widget 㦭  祭 ࠭   㪠⥫ pimpl.  ⠪ ᯮᮡ : ᯥ樠஢ std::swap   Widget.  ਢ ᭮ ,   ⠪    ᪮:

namespace std {
template <> //  ᯥ樠஢ 
void swap<Widget>(Widget& a, // std::swap,  T 
Widget& b) // Widget;  ᪮
{
swap(a.pimpl, b.pimpl); //    Widget 
} //   㪠⥫ pimpl
}

ப <template <>>  砫 㭪樨   ⮬,    ᯥ樠 蠡 std::swap,  <<Widget>> ᫥  㭪樨   ⮬,   ᯥ樠  ,  T  Widget. 㣨 ᫮,  騩 蠡 swap ਬ  Widget,    ᯮ짮  ॠ. -  ᪠  ᮤন ࠭⢠  std,  ࠧ襭   ᯥ樠樨 ⠭ 蠡 ( swap)  ᮧ  ⨯ (ਬ, Widget).    .
  㦥 ᪠,  㭪  ᪮.   ⮬,   ⠥    㪠⥫ pimpl  a  b,   .     ᯥ樠 㣮 ,  ᮣ襭 ॡ 㯨 : 㦭    Widget  㭪-童   swap,  ⢨ ॠ  祭ﬨ,  ⥬ ᯥ樠 std::swap 맮  㭪-童:

class Widget { //   ࠭,  ᪫祭
public: //  㭪樨-童 swap
...
void swap(Widget& other)
{
using std::swap; // 室  ⮬ 
//  
swap(pimpl, other.pimpl); // ⮡  祭ﬨ  ꥪ
} // Widget, 㪠⥫ pimpl
...
};
namespace std {
template <> // । 
void swap<Widget>(Widget& a, // std::swap
Widget& b)
{
a.swap(b); // ⮡  祭ﬨ Widget,
} // 뢠 㭪-童 swap
}

 ਠ  ⮫쪮 ,    ᮣᮢ  STL-⥩ࠬ,    ।⠢   㭪-童 swap,  ᯥ樠஢  std::swap,  뢠  㭪-童.
।, ,  Widget  Widgetlmpl -   ,  蠡 . ,    ⮣, ⮡  뫮 ࠬਧ஢ ⨯ , ࠭  Widgetlmpl:

template <typename T>
class WidgetImpl {...};
template <typename T>
class Widget {...};

 㭪-童 swap  Widget (  室  Widgetlmpl)  ⮬ 砥 ⠪  ,   ࠭,   ⠫  ஡, 饩 ᯥ樠樨 std::swap.    ⨬ :

namespace std {
template <typename T>
void swap<Widget<T>>(Widget<T>& a, // 訡! ⨬ 
Widget<T>& b)
{ a.swap(b);}
}

룫廊 ᮢ襭 ࠧ㬭,   ࠢ ࠢ쭮.  ⠥ 筮 ᯥ樠஢ 蠡 㭪樨 (std::swap), ,  C++ ᪠  ᯥ樠 蠡 ,   ࠧ蠥 ⮣  蠡 㭪権.     ஢ (᫨ ⮫쪮    ய   訡).
  㦭 <筮 ᯥ樠஢> 蠡 㭪樨,    ॣ㦥 . ਬ୮ ⠪:

namespace std {
template <typename T>
void swap(Widget<T>& a, // ॣ㧪 std::swap
Widget<T>& b) // (⬥⨬ ⢨ <...> ᫥
{ a.swap(b);} // "swap"),  ᭥, 祬
} //   ४⥭

, ॣ㧪 蠡 㭪権 - ଠ쭮 襭,  std -  ᯥ樠쭮 ࠭⢮ ,  ࠢ,   稭, ⮦ ᯥ樠.   ᯥ樠஢ 蠡  std,     std  蠡 ( ,  㭪樨,  - ). ন std । ᪫⥫쭮 ⮬  ⠭⨧樨 C++,   饭  ᯨ᮪ ⮣,   訫  㤠.  ᮦ, ଠ ⮣   ਢ   ⥭. ணࠬ,   ,  ᥣ   ᯮ,     ।! ᫨   , ⮡  ணࠬ  ᥡ ।᪠㥬 ࠧ,     祣  std.
  ?  -० 㦥 ᯮᮡ, ⮡ ࠧ 㣨  뢠 swap    䥪⨢ 蠡 . ⢥ . ,   ࠭, 塞 ᢮ 㭪 swap,  뢠 㭪-童 swap,   ਬ,   ᯥ樠  ॣ㦥 ਠ std::swap. ਬ, ᫨  㭪樮쭮,  Widget, 室  ࠭⢥  WidgetStuff,   㤥 룫拉 ⠪:

namespace WidgetStuff {
... // 蠡 WidgetImpl  . .
template<typename T> //   ࠭, 
class Widget {...}; // 㭪-童 swap
...
template<typename T> // ᢮ 㭪 swap
void swap(Widget<T>& a, //  室  ࠭⢮  std
Widget<T>& b)
{
a.swap(b);
}
}

 ᫨ - 맮 swap   ꥪ⮢ Widget,  ᮣ᭮ ࠢ ᪠   C++ ( 筥, ᮣ᭮ ࠢ  ᨬ⥩  㬥⮢) 㤥  ᯥ筠  Widget   ࠭⢥  WidgetStuff.    ࠧ ,   ⨬.
 室 ࠡ⠥    ᮢ  蠡 ᮢ, ⮬ ,     ᫥ ᥣ ᯮ짮.  ᮦ,  ᮢ  稭,  ன  ᯥ樠஢ std::swap (   ), ⮬ ᫨    ᮡ⢥ ᯥ    swap, 뢠   ⥪ ( ,  ᮬ, ),  ਤ   ᢮ 㭪 swap  ⮬  ࠭⢥ ,  室  ,  ᯥ樠 std::swap.
, ᫨    ࠭⢠ ,  ᪠ ⠥  ᨫ (   㦭 ᢮ 㭪 swap,  뢠 㭪-童 swap).  祬  쭮 ࠭⢮  訬 ᠬ, 蠡, 㭪ﬨ, ᫥ﬨ  塞묨 ⠭⠬, ।ﬨ ⨯ typedef?       ਫ?
,   ᠫ   , ।⠢   ஢ 㭪樨 swap,  ⮨ ᬮ    窨 ७ 짮⥫. ।,   蠡 㭪樨,  ஬   祭ﬨ  ꥪ:

template <typename T>
void doSomething(T& obj1, T& obj2)
{
...
swap(obj1, obj2);
...
}

  swap   뢠?  -  ࠭⢠ std,  ⢮ ன  筮 ;  ᯥ樠   std,  , ,  , ;  ᯥ筠   T, ⢮ ன ⠪  ᮬ    室  - ࠭⢥  (    std)?  ⥫  맢 ᯥ  T , ᫨  ,   ⨢ 砥  饩 ᨨ  std.   㤮⢮  :

template <typename T>
void doSomething(T& obj1, T& obj2)
{
using std::swap; // ᤥ std::swap 㯭 ⮩ 㭪樨
...
swap(obj1, obj2); // 맢 訩 ਠ swap  ꥪ⮢ ⨯ T
...
}

  砥 맮 swap,  ,   맢. ࠢ ࠧ襭   C++ ࠭,  㤥   ᯥ筠  ⨯ T   쭮     ⮬  ࠭⢥ ,   T. (ਬ, ᫨ T -  Widget  ࠭⢥  Widget-Stuff,  ஠ 㬥     .) ᫨  ᨨ swap, ᯥ筮  T,  ,   쬥 swap  std   using,   std::swap .    ⮬ 砥  । ᯥ樠 std::swap  ⨯ T 饬 蠡.
 ࠧ, ⠢  맢 㦭   swap 筮 . ⢥,  祬 ᫥ ,-  ஢ 맮  ࠭⢠ , ⮬     ᯮᮡ 롮 㭪樨. ਬ, ᫨   맮 ᫥騬 ࠧ:

std::swap(obj1, obj2): // ࠢ ᯮᮡ 맮 swap

 ⠢  ᬠਢ ⮫쪮 swap  ࠭⢠ std (  ᯥ樠樨 蠡), ᪫稢  ᪠  室饩 ᨨ, ᯥ筮  ⨯ T,  ᫨  - ।.  ᮦ,  ணࠬ  訡  맮 swap ⠪ ࠧ, ⮬  ।⠢  ᢮   ᯥ樠 std::swap, ⮣   ⠪, ࠢ쭮 ᠭ  ᯥ筠  ⨯ ॠ swap  㯭. (     ॠ ⠭⭮ ⥪, ⮬    - ᤥ , ⮡  ࠡ⠫ 䥪⨢).
⠪,  㤨 ॠ swap  㬮砭,   㭪樨-童 ,   ᢮ 㭪樨    ᯥ樠樨 std::swap,  ⠪ 맮 swap.   ⮣.
-, ᫨ ॠ swap  㬮砭 ᯥ稢 ਥ 䥪⨢   ᮢ  蠡 ᮢ,    㦭  祣. 直,  ⠥  祭 ꥪ⮢ 襣 ,    㬮砭,   㤥 ४᭮ ࠡ.
-, ᫨ ॠ  㬮砭 swap 筮 䥪⨢ (  ᥣ 砥,     蠡 ᯮ  ਠ  pimpl), ᤥ ᫥饥:
1) ।⠢  㭪-童,  䥪⨢  祭  ꥪ⮢ 襣 ⨯.  稭,   ᥩ ,  㭪    㦤 ᪫祭;
2) ।⠢ ᢮ 㭪 swap  ⮬  ࠭⢥ ,      蠡.   뢠  㭪-童;
3) ᫨    (  蠡), ᯥ樠 std::swap  襣 .   ⠪ 뢠  㭪-童.
, ᫨  뢠 swap, 㡥,  祭 using-, ஥  std::swap    襩 㭪樨,  ⥬ 뢠 swap  䨪樨 ࠭⢠ .
   ।।,   㭪樨-童 swap    㦤 ᪫祭.   ⮬,      ਬ swap -  ᠬ ( 蠡 ᮢ)  ।⠢  ࠭⨩ ᭮ ᪫祭.  ࠢ 29   ஡ ଠ   ⥬,  ᥩ  ભ,   ᭮ ⮣ ਥ  ।  ⮬,  swap, ॠ   㭪樨-童,   㦤 ᪫祭.  ࠭祭 ᠥ ⮫쪮 㭪樨-童!   ⭮  ॠ樨 swap   ᢮ 㭪樨, ᪮ ⠭⭠  swap  㬮砭 ᭮   ஢   ᢠ,  ⨬ 㭪 ࠧ襭 㦤 ᪫祭.    ᮡ⢥  swap,  筮 ।⠢   䥪⨢ ᯮᮡ  祭,  ⠪,  ஬  㦤 ᪫祭. 饥 ࠢ ⠪:   ࠪ⨪ swap  㪠  , ⮬  ᮪䥪⨢ 樨  ᥣ ᭮    ஥묨 ⨯ (⠪  㪠⥫, 騥  ᭮  pimpl),  樨  ஥묨 ⨯   㦤 ᪫祭.

 ᫥ 
 ।⠢ 㭪-童 swap, ᫨ std::swap ࠡ⠥  訬 ⨯ 䥪⨢. ,    㦤 ᪫祭.
 ᫨  ।⠢ 㭪-童 swap,  ⠪ ।⠢ ᢮ 㭪, 뢠 㭪-童.  ᮢ ( 蠡) ᯥ樠 ⠪ std::swap.
  뢠 swap, ᯮ using-, 饥 std::swap   ,  뢠 swap  䨪 ࠭⢠ .
 ᪠ ।⠢  ᯥ樠樨 蠡, 室  ࠭⢥  std,  짮⥫᪨ ⨯,    ⠩   ࠭⢮ std - .




 5



 ᭮ ࠧࠡ⪠ ணࠬ ᢮  ᠭ । ᮢ ( 蠡 ᮢ)   㭪権 ( 蠡 㭪権). ᫨ ᤥ  ࠢ쭮,  ॠ 㦥  ⠪ ᫮.       ⮨  . 誮 ࠭ । ६  ⥫쭮   ந⥫쭮. १୮ ਬ ਢ ⨯ ⠪ ਢ    ࠡ饩 ணࠬ,   ᮯ஢   ன   㤭㫮 訡.  ਯ஢ ७  ꥪ   ਭ樯 樨  ਢ   < ਯ஢>. ᫨  ਭ   ᪫祭, १⮬   窠 ᮢ  ०  . 㯮ॡ ஥묨 㭪ﬨ ਢ  <ࠧ堭> . 讥 ⢮ ᨬ⥩  ࠧ묨 ﬨ ணࠬ   ਥ 訬 ⠬ ६  ᡮ ணࠬ.



ࠢ 26: ⪫뢠 । ६ ᪮쪮 

直 ࠧ   ६, ਭ騩 ⨯,  ஬    , ணࠬ  ६   ஢,  ⮪ ࠢ ⨣ । ६,   㭨⮦ -  室 ६   .   室 室   ⮣,  ६  ᯮ, , ࠧ㬥,  ⥫  .
⭮,  㬠,     ᯮ㥬 ६,  ⠪  ? ᬮਬ ᫥ 㭪,  頥 ஢ ஫  ᫮,      ண 㬠. ᫨ ஫ ᫨誮 ⪨, 㭪 㦤 ᪫祭 ⨯ logic_error, ।  ⠭⭮ ⥪ C++ (. ࠢ 54):

//  㭪  ६ encrypted ᫨誮 ࠭
std::string encryptPassword(const std::string& password)
{
using namespace std;
string encrypted;
if(password.length() < MinimumPasswordLength) {
throw logic_error("誮 ⪨ ஫");
}
... // ᤥ ,  室  饭
// ஢ ஫  ६ encrypted
return encrypted;
}

 ᪠,  ꥪ encrypted  ⮩ 㭪樨 ᮢᥬ   ᯮ,    ᯮ  砥,  㦤 ᪫祭. 㣨 ᫮,    맮    ꥪ encrypted,  ᫨ 㭪 encryptPassword 㦤 ᪫祭.     ⫮ । ६ encrypted  ⮣ ,   㤥 ,   㦭?

//  ⮩ 㭪樨 । ६ encrypted ⫮  ,
//     
std::string encryptPassword(const std::string& password)
{
using namespace std;
if(password.length() < MinimumPasswordLength) {
throw logic_error("誮 ⪨ ஫");
}
string encrypted;
... // ᤥ ,  室  饭
// ஢ ஫  ६ encrypted
return encrypted;
}

     ⮫쪮 ,    , ⮬  ६ encrypted ।  砫쭮 祭.  , 㤥 ᯮ짮    㬮砭.  ࢮ,  㦭 ᤥ  ꥪ⮬,-    - 祭, । ।⢮ ᢠ.  ࠢ 4 ᭥, 祬 ஢ ꥪ⮢  㬮砭  ᫥騬 ᢠ 祭  䥪⨢, 祬 樠 㦭 祭  ᠬ 砫.  ⭮    . ਬ, ।,   믮 <㤭>  ࠡ 㭪 encryptPassword 뢠 ᫥ 㭪:

void encrypt(std::string& s); //  s  

 encryptPassword   ॠ ᫥騬 ࠧ,      ⨬ ᯮᮡ:

//  ⮩ 㭪樨 । ६ encrypted ⫮  ,
//     ,    ਠ  筮
// 䥪⨢
std::string encryptPassword(const std::string& password)
{
... // ஢ઠ 
string encrypted; //   㬮砭
encrypted = password; // ᢠ encrypted
encrypt(encrypted);
return encrypted;
}

  樠஢ encrypted ࠬ஬ password,  ⠪ ࠧ ⥭樠쭮 ண饣   㬮砭:

//   ⨬ ᯮᮡ ।  樠樨 encrypted
std::string encryptPassword(const std::string& password)
{
... // ஢ઠ 
string encrypted(password); // ।  樠
// ஬ ஢
encrypt(encrypted);
return encrypted;
}

  砥 <⪫뢠 ᪮쪮 > ( ᪠   ࠢ).   ⮫쪮  ⪫뢠 । ६  ⮣ ,   ᯮ, 㦭   ⫮ ।  祭 㬥⮢  樠樨. 㯨 ⠪,   ஢  ࠧ襭 㦭 ꥪ⮢,  ⠪ 譨 맮 ஢  㬮砭.  ⮣,   㬥஢ 祭 ६   樠樨   ⮬ ⥪,  ஬  祭 ⭮  ᫮.
<   横?> -  㤨 . ᫨ ६ ᯮ ⮫쪮  横,   : ।   横  믮 ᢠ   樨  ।   横? 㣨 ᫮,   ᫥ 権 ।⥫쭥?

// 室 A: ।  横
Widget w;
for(int i=0; i<n; ++i) {
w = ஥ 祭, 饥  i;
...
}
// 室 B: ।  横
for(int i=0; i<n; ++i) {
Widget w(஥ 祭, 饥  i);
...
}

  宦  ꥪ ⨯ string  ꥪ ⨯ Widget, ⮡   । ⭮⥫쭮 ⮨ ஢, ࠧ襭  ᢠ.
 ନ 権 Widget  室  ⠪:
 室 A: 1  + 1  + n ᢠ
 室 B: n ஢ + n ஢
 ᮢ,   ⮨ 樨 ᢠ , 祬  -, 室 A 筮  䥪⨢. ᮡ  ୮,  祭 n 筮 .  ⨢ 砥, , 室 B .  ⮣,  砥 A  w    ப  (饩  ᥡ 横), 祬  砥 B,     ணࠬ  ⭮  㤮  ᮯ஢. ⮬ ᫨ (1)  ਮ୮ ଠ樨  ⮬,  ᢠ 室 襢, 祬  -,  (2)     ணࠬ, ந⥫쭮 ன 筠,   㬮砭 ४ ᯮ짮 室 B.

 ᫥ 
 ⪫뢠 । ६ ᪮쪮 .   ணࠬ ᭥  蠥  䥪⨢.



ࠢ 27:  㯮ॡ ਢ ⨯

ࠢ C++ ࠧࠡ⠭ ⠪, ⮡ ࠢ쭮 ࠡ  ⨯ 뫮 . ᪨, ᫨  ணࠬ   訡, ,   ⠥ 믮    ᫥ 権  ꥪ⠬.  業 ࠭.     ⪠뢠.
 ᮦ, ਢ 室 ⥬ ⨯.    ਢ  ࠧ ஡,    ᯮ ,   - १砩 㤭. ᫨  諨  C++   C, Java  C#, ਬ ⮪ ᢥ, ᪮  㪠 몠  ਢ ⨯   室,    , 祬  C++.  C++ -   C.   Java.   C#.  ⮬ 몥 ਢ -  ।⢮,  ஬ 㦭 ⭮   ⥭.
筥   ᨭ⠪ ஢ ਢ ⨯, ⮬    ࠧ ᯮᮡ     . ਢ  ⨫ C 룫廊 ⠪:

(T) expression // ਢ expression  ⨯ T

㭪樮 ᨭ⠪ ਢ ⠪:

T( expression) // ਢ expression  ⨯ T

 ⨬  ଠ  ⨬ ࠧ,  ᪮ ⠢ -ࠧ.  뢠   ਢﬨ  ஬ ⨫.
C++ ⠪ ।⠢    ਢ ⨯ ( 뢠 ਢﬨ  ⨫ ++):

const_cast<T>(expression)
dynamic_cast<T>(expression)
reinterpret_cast<T>(expression)
static_cast<T>(expression)

    ᢮ 祭:
 const_cast 筮 ਬ  ⮣, ⮡  ⠭⭮ ꥪ.  㣮 ਢ  ⨫ C++    ᤥ;
 dynamic_cast ਬ  ࠧ  믮 <᭮ 饣 ਢ> (downcasting).    ।, ਭ  ꥪ  ⨯ ன 娨 ᫥.  ⢥  ਢ,     믮  ᯮ짮 ண ᨭ⠪.  ⠪ ⢥ ਢ, ஥  ॡ ⨬   ६ ᯮ (஡ );
 reinterpret_cast ।祭  ஢ ਢ,  ஦ ᨬ  ॠ樨 (  ७ᨬ) १, ਬ ਢ 㪠⥫  int.  ஢  ⠪ ਢ  ᯮ짮 ।.  ᯮ짮   ⮩   ,  㦤 ᠭ ⫠筮 ।⥫  (. ࠢ 50);
 static_cast   ᯮ짮   ८ࠧ ⨯ (ਬ, ⠭ ꥪ⮢  ⠭ (  ࠢ 3), int  double  . .).  ⠪   ᯮ짮  믮  ८ࠧ (ਬ, 㪠⥫ void*  ⨯஢ 㪠⥫, 㪠⥫     㪠⥫  ந).  ਢ ⠭ ꥪ  ⠭⭮     ( 稭 const_cast).
ਬ ਢ  ஬ ⨫ ⠥  ,    ।⥫쭥. -,  ࠧ     (  祫,   㬥,  grep),  頥  ᪠    ,  ⥬ ⨯樨 ࣠ ᭮. -,  㧪 ᯥ樠஢ 祭   ਢ   ࠬ ஢ 訡  ᯮ짮. ਬ, ᫨  ⠥   ⠭⭮, ᯮ   ਢ  ⨫ C++, ஬ const_cast,     ⪮.
 ᯮ ਢ  ஬ ⨫ ⮫쪮 ⮣,   맢 explicit , ⮡ । ꥪ  ⢥ ࠬ 㭪樨. ਬ:

class Widget {
public:
explicit Widget(int size);
...
};
void doSomeWork(const Widget& w);
doSomeWork(Widget(15)); // ᮧ Widget  int
//  㭪樮 ਢ
doSomeWork(static_cast<Widget>(15)); // ᮧ Widget  int
//  ਢ  ⨫ C++

 ७ ᮧ ꥪ  <頥>  ਢ ⨯, ⮬   砥, ୮,  ਬ 㭪樮쭮 ਢ  static_cast.   , , 騩  ਩ 襭, 筮 룫廊 ᮢ襭 ࠧ㬭,    , ⮬      饭  ᥣ 짮 ਢﬨ   ⨫.
 ணࠬ ,  ਢ ⨯ ᥣ   ,  㦭 ࠪ⮢  ⨯  㣮,   㦤. ८ࠧ ⨯  த ( , ।⢮ ਢ, ⠪  , 믮塞 ᠬ ஬)  ਢ   , ᯮ塞  ६ ࠡ ணࠬ. ᬮਬ ਬ:

int x, y;
...
double d = static_cast<double>(x)/y; //  x  y  ᯮ짮
//   饩 窮

ਢ int x  ⨯ double  猪 ஦ ᯮ塞 , ⮬   設⢥ ⥪ ७ ।⠢ int ⫨砥  ।⠢ double. ᫨    ᮡ 㤨,  ﭨ  ᫥騩 ਬ:

class Base {...};
class Derived: public Base {...};
Derived d;
Base *pb = &d; //  ८ࠧ Derived*
//  Base*

  ᥣ  ᮧ 㪠⥫    ꥪ ந,     㪠⥫ 㪠뢠       .  ⠪ 砥  ६ ᯮ  㪠⥫ Derived* ਡ ᬥ饭, ⮡  ࠢ쭮 祭 㪠⥫ Base*.
᫥ ਬ ,      ꥪ (ਬ, ꥪ ⨯ Derived)      (ਬ,   㪠     Base* ⫨砥    㪠   Derived*).    C.    Java.   뢠  C#.   砥  C++. ᪨,  ਬ ⢥ ᫥, ⠪ 砥 ᯫ  冷,   ந   筮 ᫥.   ᥬ 祬 砥, , ணࠬ  C++,    ந ।  ⮬,  ꥪ ᯮ  ,   ⥬    믮 ਢ ⨯    ।. ਬ, ਢ  ꥪ  ⨯ char*  ᫥饥 ᯮ짮 䬥᪨ 権  㪠⥫ﬨ  ᥣ ⠭ 稭 । .
,  ᪠,  ᬥ饭 ॡ ਡ <>. ᮡ ࠧ饭 ꥪ⮢    ᯮᮡ ᫥  ᮢ     .  ,  ⮣,  < ,  ࠭ ꥪ  >   ଥ,   ᫥,   㣨  㤥 ஥ 筮 ⠪ .   ணࠬ⮢,  ᢮  ப, ⨢ ᫨誮 ᮪ 業.
 , 騩 ਢ,-   ⮬,    ,  룫廊 ࠢ (   ࠢ  㣨 몠),   ᠬ  ࠢ  . ਬ,   ઠ  ࠧࠡ⪨ ਫ ॡ, ⮡ 㠫 㭪樨-童, ।  ந , 砫 뢠 ᮮ⢥騥 㭪樨   ᮢ. ।,       Window  ந    SpecialWindow, 祬   । 㠫쭠 㭪 onResize.  ।,  onResize  SpecialWindow 㤥 뢠 ᭠砫 onResize  Window.  ॠ 룫廊 ,    ࠢ쭠:

class Window { //  
public:
virtual void onResize() {...} // ॠ onResize  
... // 
};
class SpecialWindow: public Window { // ந 
public:
virtual void onResize() { // ॠ onResize
static_cast<Window>(*this).onResize(); //  ந ;
// ਢ *this  Window,
// ⥬ 맮  onResize;
//   ࠡ⠥!
... // 믮 ᯥ᪮ 
} // SpecialWindow  onResize
...
};

 뤥  ⮬  ਢ ⨯. ( ਢ   ⨫,  ᯮ짮 ண ⨫ 祣  .)   , *this ਢ  ⨯ Window. ⮬ 饭  onResize ਢ  맮 Window::onResize.  ⮫쪮  㭪  㤥 맢  ⥪饣 ꥪ! ,  ࠢ ?  ⮣  ਢ ᮧ , ६     *this  맮 onResize  ⮩ ! ਢ    맮 Window::onResize  ⥪饣 ꥪ  ᫥騬 믮 ᯥ  SpecialWindow ⢨ -  믮 Window::onResize      ⥪饣 ꥪ । 믮 ᯥ  SpecialWindow ⢨   ꥪ. ᫨ Window::onResize  ꥪ (  , ⠪  onResize -  ⠭⭠ 㭪-童),  ⥪騩 ꥪ  㤥 ஢.  ⮣ 㤥 ஢  ⮣ ꥪ.  ᫨ SpecialWindow::onResize  ꥪ,  㤥 ஢  ⥪騩 ꥪ.   १ ⥪騩 ꥪ ⠥  ᮣᮢ ﭨ, ⮬  䨪 ⮩  ,  ਭ  ,  㤥 믮,  䨪 , ਭ饩 ந , 㤥.
襭 ஡  ⮬, ⮡ ᪫ ਢ ⨯,   ⥬,   ⢨⥫쭮   .  室 믮 -   ஬, ⠢  ஢ *this  ꥪ  .   맢  onResize    ⥪饣 ꥪ.  㯨 ᫥騬 ࠧ:

class SpecialWindow: public Window {
public:
virtual void onResize() {
Window::onResize(); // 맮 Window::onResize  *this
...
}
...
};

ਢ ਬ ⠪ ,   ᪮  頥  믮 ਢ ⨯,   ⮣,  , ,   . ᮡ  ᠥ  dynamic_cast.
० 祬   ⠫ dynamic_cast, ⮨ ⬥,  設⢮ ॠ権 ⮣  ࠡ 쭮 . ,  ࠩ ,   ࠭ ॠ権 ᭮  ࠢ  ᮢ, ।⠢ ப. ᫨  믮 dynamic_cast  ꥪ , ਭ饣 娨   ᫥ 㡨   ஢,   饭  dynamic_cast  ⠪ ॠ樨      맮 strcmp  ࠢ  ᮢ.   㡮 娨  ⠪,  ன  ⢥ ᫥,      ண饩.  稭, -   ॠ樨 ࠡ  ࠧ (⮬    ন  ).  ࠧ,    ஦  ⭮襭  ਢ ⨯  ਭ樯    ᮡ ᪥樧,     ਬ dynamic_cast   ணࠬ,  ன ந⥫쭮 ⮨  ࢮ .
室  dynamic_cast 筮  - ⮣,    믮 樨, ।  ந ,  ꥪ, ,   , ਭ ந ,   ⮬    ⮫쪮 㪠⥫  뫪   , ।⢮ ன 㦭 㫨஢ ꥪ⮬.   ᭮ ᯮᮡ  ⮩ ஡.
 - ᯮ ⥩  ࠭ 㪠⥫ ( <⥫㠫>, . ࠢ 13)  ᠬ ꥪ ந ᮢ, ⮣ ⯠ 室 㫨஢ ⨬ ꥪ⠬ १ 䥩  . ਬ, ᫨  襩 娨 Window/SpecialWindow ⮫쪮 SpecialWindow ন 栭 (blinking),  :

class Window { ...};
class SpecialWindow {
public:
void blink();
...
};
typedef // . ࠢ 13
std::vector<std::tr1::shared_ptr<Window>>VPW; //  tr1::shared_ptr
VPW winPtrs;
...
for (VPW::iterator iter = winPtrs.begin(); // ⥫ :
iter!=winPtrs.end(); // ਬ dynamic_cast
++iter){
if(SpecialWindow psw = dynamic_cast<SpecialWindow>(iter->get()))
psw->blink();
}

஡ ᤥ ⠪:

typedef std::vector<std::tr1::shared_ptr<SpecialWindow>> VPSW;
VPSW winPtrs;
...
for (VPSW::iterator iter = winPtrs.begin(); //  :
iter != winPtrs.end(); //  ᯮ dynamic_cast
++iter)
(*iter)->blink();

筮, ⠪ 室    ࠭ 㪠⥫  ꥪ   ந  Window ᮢ    ⮬  ⥩. ⮡ ࠡ  ࠧ묨 ⨯   ᯥ ᭮  ⭮襭  ⨯,    ᪮쪮 ⥩஢.
ୠ⨢,   㫨஢ ꥪ⠬   ந  Window ᮢ १ 䥩  ,-  ।ᬮ 㠫 㭪樨   ,      ,   㦭. ਬ,  ⮫쪮 SpecialWindow 㬥 ,  ,    㭪     ᯥ ⠬ ॠ  㬮砭,    祣:

class Window {
public:
virtual void blink() {} // ॠ  㬮砭 - 
... // , . ࠢ 34 - 祬
}; // 稥 ॠ樨  㬮砭
//   㤠筮 
class SpecialWindow: public Window {
public:
virtual void blink() {...}
...
};
typedef std::vector<std::tr1::shared_ptr<Window>>VPW;
VPW winPtrs; // ⥩ ᮤন
// (㪠⥫ )  
... // ⨯ 
for(VPW::iterator iter = winPtrs.begin();
iter != winPtrs.end();
++iter) // dynamic_cast  ᯮ
(*iter)->blink();

    室 -  ਬ   ⭮襭  ⨯ ⥩஢  ६饭 㠫쭮 㭪樨   娨 -   㭨ᠫ쭮 ਬ,      ।⠢  ୠ⨢ dynamic_cast.  ,  .
  祣 ⮨  ᥣ -  ᪠  ஢ dynamic_cast,   祣- த ⠪ :

class Window {...};
... //  । ந 
typedef std::vector<std::tr1::shared_ptr<Window>> VPW;
VPW winPtrs;
...
for (VPW::iterator iter = winPtrs.begin(); iter != winPtrs.end(); ++iter)
{
if (SpecialWindow1 *psw1=
dynamic_cast<SpecialWindow1>(iter->get())) {...}
else if (SpecialWindow2 *psw2=
dynamic_cast<SpecialWindow2>(iter->get())) {...}
else if (SpecialWindow2 *psw2=
dynamic_cast<SpecialWindow2>(iter->get())) {...}
...
}

 ⮬ 砥  ꥬ   ,  ⮬   ⠡, ⮬     娨 ᮢ Window    㦭 ᬮ  । . ਬ, ᫨   ந ,  ⭮, ਤ      । if.    ᥣ    祬-  ᭮ 맮 㠫 㭪権.
 襩 ணࠬ  C++ ਢ ⨯ ᯮ 祭 ।,   ⪠뢠   ⮦  ⮨. ,   ਢ int  double  ࠧ㬭,    ᮫⭮ 室 (   ९ᠭ    ६ ⨯ double, 樨㥬 祭 x).   設⢮ ᮬ⥫ 権, ਢ ⨯   ஢ ᪮쪮 . 筮    㭪権, 祩 䥩 뢠  짮⥫  ᨢ ,  ⢮ .

 ᫥ 
  ᪮쪮  ਢ ⨯, ᮡ dynamic_cast,  筮  ந⥫쭮 . ᫨  ॡ ਢ, ⠩ ࠧࠡ ୠ⨢,  ⠪ 室  .
  ਢ ⨯ 室, ࠩ    㭪樨.  짮⥫ ᬮ 뢠  㭪  饭 ਢ   ᮡ⢥ .
 ।⠩ ਢ  ⨫ C++ ஬ ⨫.   㢨,    ⥫.



ࠢ 28:  饭 <ਯ஢> ७ 

।⠢,   ࠡ⠥  ਫ, 騬   אַ㣮쭨.  אַ㣮쭨   ।⠢ ᢮  孨 㣫  ࠢ . ⮡ ꥪ Rectangle ⠢ ,   ,  ᠭ ।  祪 ᫥ 뭥  Rectangle  ᯮ⥫ :

class Point { // , ।⠢騩 窨
public:
Point(int x, int y);
...
void setX(int newVal);
void setY(int newVal);
...
};
struct RectData { // 窨, ।騥 Rectangle
Point ulhc; // ulhc - 孨  㣮
Point lrhc; // lrhc -  ࠢ 㣮
};
class Rectangle {
...
private:
std::tr1::shared_ptr<RectData> pData; // . ࠢ 13
}; // ଠ  tr1::shared_ptr

᪮ 짮⥫  Rectangle  ।  न,   ।⠢ 㭪樨 upperLeft  lowerRight.  Point -  । 짮⥫ ⨯, ⮬,   ⮬,  । ⠪ ⨯  뫪 筮  䥪⨢, 祬 ।  祭 (. ࠢ 20),  㭪樨  뫪  ७ ꥪ Point:

class Rectangle {
public:
...
Point& upperLeft() const { return pData->ulhc;}
Point& lowerRight() const { return pData->lrhc;}
...
};

 ਠ ⪮,   ࠢ! ᪨  ७ ⨢稢.   ஭, upperLeft  lowerRight   ⠭ 㭪樨-童, ᪮  ।祭 ⮫쪮  ⮣, ⮡ ।⠢  ᯮᮡ  ଠ  窠 Rectangle,     ஢ ꥪ Rectangle (. ࠢ 3).  㣮 ஭,  㭪樨  뫪   ७  - 뫪,  짮⥫  ⥬ ᯮ짮  䨪樨  ७ ! ਬ:

Point coord1(0, 0);
Point coord2(100,100);
const Rectangle rec(coord1, coord2); // rec - ⠭ אַ㣮쭨
//  (0, 0)  (100, 100)
rec.upperLeft().setX(50); // ⥯ rec  
// (50, 0)  (100, 100)!

 ,  짮⥫ 㭪樨 upperLeft  ᯮ짮 饭 뫪    -童 ७ ꥪ Point  䨪樨 ⮣ 童.   ,  rec - ⠭!
 ⮣ ਬ ᫥   ப.  - 童  㫨஢  ⮫쪮, ᪮쪮 㯭 㭪,  뫪  .   砥  ulhc  lrhc  묨,   ᠬ   , ⮬     뫪  㭪樨 upperLeft  lowerRight. ன ப  ⮬,  ᫨ ⠭⭠ 㭪-童 頥 뫪  , 樨஢  ꥪ⮬,  ࠭騥  ᠬ ꥪ,  , 뢠騩  㭪,  ஢ . (  ᫥⢨ ࠭祭 ⮢ ⠭⭮ - . ࠢ 3.)
 १ 稫,   ᯮ짮 㭪樨-童, 騥 뫪,  ᫨   㪠⥫  , ஡ ⠥,  稭  . 뫪, 㪠⥫   -   <ਯ> (handles),  饭 ⠪ <ਯ> ७  ꥪ - אַ   襭 ਭ樯 樨.   ⮫쪮  ,   ਢ  ⮬,  ⠭ 㭪樨-童  ஢ ﭨ ꥪ.
筮,   ७ ன⢥ ꥪ,      -童,  㭪樨-童,    ⮣ 㯠 (    ᥪ樨 private  protected), ⠪   ७ ன⢠. ⮬   <ਯ> ⮦  ᫥. 묨 ᫮, , ⮡ 㭪-童 頫 㪠⥫   㯭 㭪-童.  ⨢ 砥 ॠ ஢ 㯠 㤥 ।  㯭 㭪, ⮬   ᬮ  㪠⥫   㯭 㭪  맢  १ ⠪ 㪠⥫.
祬, 㭪樨,   㪠⥫  㭪樨-童,  , ⮬ ୥   Rectangle   㭪-童 upperLeft  lowerRight.  ஡,   ஢   㭪権,   ᪫祭  ਬ 䨪 const   頥 ⨯:

class Rectangle {
public:
...
const Point& upperLeft() const { return pData->ulhc;}
const Point& lowerRight() const { return pData->lrhc;}
...
};

 १ ⠪  짮⥫ ᬮ  ꥪ Point, ।騥 אַ㣮쭨,   ᬮ  .  ,   ⠭묨 㭪権 upperLeft  lowerRight    , ⠪      ⠬ ஢ ﭨ ꥪ.  ᠥ ஡ 樨,    ᠬ 砫 ॢ  ⠬   ꥪ Point, ।騥 Rectangle, ⮬   砥 ᫠ 樨 ७.  ⮬    筮 ᫠: ᬠਢ 㭪樨  ⮫쪮   ⥭.    -० 饭.
   ⠪ upperLeft  lowerRight -०  <ਯ> ७  ꥪ,    맢 ஡  ᢮⢠.  ⭮,   < ਯ஢> (dangling handles),   ਯ஢, 뫠   㦥   ꥪ⮢.  ⨯ 筨 ⠪ 祧 ꥪ⮢ - 祭, 頥 㭪ﬨ. ਬ, ᬮਬ 㭪,  頥 ࠭稢騩 אַ㣮쭨 ꥪ GUI:

class GUIObject {...};
const Rectangle // 頥 אַ㣮쭨  祭;
boundBox(const GUIObject& obj); // . ࠢ 3, 祬 const

 ᬮਬ,  짮⥫  ਬ  㭪:

GUIObject *pgo; // pgo 㪠뢠   ꥪ
... // GUIObject
const Point *pUpperLeft = //  㪠⥫  孨 
&(boundingBox(*pgo).upperLeft()); // 㣮  ࠬ

맮 boundingBox ୥  ६ ꥪ Rectangle.  ꥪ   , ⮬   temp. ⥬ 뢠 㭪-童 upperLeft ꥪ temp,   맮 頥 뫪  ७  temp,   砥    ꥪ⮢ Point.  १ pUpperLeft 㪠뢠   ꥪ Point.     諮 ,     稫, ᪮   । 饭 boundingBox 祭 - temp - 㤥 ࠧ襭,   ਢ  ࠧ襭 ꥪ⮢ Point, ਭ temp.   pUpperLeft ⥯ 㪠뢠  ꥪ,    . ⥫ PUpperLeft ⠭ <稬> 㦥   ।,   ᮧ!
 祬 ᭠  㭪,  頥 <ਯ> ७  ꥪ.  ⮬  ,   <ਯ> 뫪, 㪠⥫  ஬.  ,   ஢ const.  ,  ᠬ 㭪-童,  <ਯ>,  ⠭⭮.  祭   䠪,  <ਯ> 饭, ᪮  ᭮,   <०> ꥪ,   易.
  ,    ᫥  㭪樨-童, 騥 ਯ.   뢠 室. ਬ, operator[]     ⤥쭮  ப  ,  ࠡ⠥ ,  뫪    ⥩ (. ࠢ 3),  㭨⮦   ⥩஬.    ⠪ 㭪樨 - ᪮॥ ᪫祭, 祬 ࠢ.

 ᫥ 
   <ਯ> (뫪, 㪠⥫, ) ७  ꥪ.  蠥 ⥯ 樨,  ⠭ 㭪-童  ⠭묨   ⭮  < ਯ஢>.



ࠢ 29: ६, ⮡ ணࠬ 뫠 ᭠ ⭮⥫쭮 ᪫祭

᭮ ᪫祭  祬-  ६:   ⫮    ஭.  쥧   ९த⨢ 㭪樨,   訫 ⠯ 堦.
।,     , ।⠢騩   䮭묨 ⨭  ᪮ 䥩 짮⥫.   ।祭  ᯮ짮  筮 ।, ⮬  砥 ⥪  ᨭ஭樨 㯠:

class PrettyMenu {
public:
...
void changeBackground(std::istream& imgSrc); // ᬥ 䮭
... // ⨭
private:
Mutex mutex; // ⥪ ꥪ
Image *bgImage; // ⥪ 䮭 ⨭
int imageChanges; // ᪮쪮 ࠧ ⨭ 﫠
};

ᬮਬ ᫥  ॠ 㭪樨-童 change-Background:

void PrettyMenu::changeBackground(std::istream& imgSrc)
{
lock(&mutex); // 墠 ⥪
delete bgImage; //   ன ⨭
++imageChanges; //  稪  ⨭
bgImage = new Image(imgSrc); // ⠭  䮭
unlock(&mutex); // ᢮ ⥪
}

 窨 ७ ᭮ ᪫祭,  㭪 ⮫쪮 , ᪮쪮  .  ᭮ ᪫祭 ।  ॡ,    㤮⢮    .
 㦤 ᪫祭,  ᭠ ⭮⥫쭮 ᪫祭 㭪:
  ᪠ 窨 ᮢ. ਢ   室  , ⮬  ᫨ ࠦ <new Image(imgSrc)> 㤨 ᪫祭,  맮 unlock   믮,  ⥪  墠祭 ᥣ.
  ᪠ ०  . ᫨ <new Image(imgSrc)> 㤨 ᪫祭,  bgImage ⠭ 㪠⥫  㤠 ꥪ. ஬ ⮣, 稪 imageChanges 㢥稢, ᬮ     ⨭  ⠭. ( 㣮 ஭,  ⨭ 㦥  㤠, ⠪  㤭 ᤥ ,  祣  .)
ࠢ  窮 ᮢ  -  ࠢ 13 ᭥,  짮 ꥪ⠬, ࠢ騬 ᠬ,   ࠢ 14 ।⠢  Lock, ࠭騩 ᢮६ ᢮ ⥪ᮢ:

void PrettyMenu::changeBackground(std::istream& imgSrc)
{
Lock ml(mutex); //  ࠢ 14: 墠 ⥪
//  ࠭஢  ᫥饥 ᢮
delete bgImage;
++imageChanges;
bgImage = new Image(imgSrc);
}

  २ ᮢ  ࠢ ᠬ,  Lock,  ,  筮  㬥 ࠧ 㭪権. ⨫,  맮 unlock 㦥  㦥? 饥 ࠢ : 祬  , ⥬ , ⮬   ⥩  訡   ⠭  ᥭ .
 窨 ᮢ ३  ஡  ० .     롮,  ० 祬  ᤥ, 㦭 筨 ନ.
 ⭮⥫쭮 ᪫祭 㭪樨 ।⠢    ࠭⨩.
 㭪樨, ।⠢騥  ࠭, ,  ᫨ ᪫祭 㤥 㦤,    ணࠬ ⠥  ४⭮ ﭨ.  ꥪ     ०,   ꥪ 室  ⨢稢 ﭨ (ਬ,  ਠ ᮢ  襭).  筮 ﭨ ணࠬ   ।᪠㥬. ਬ,    㭪 change-Background ⠪,    ᪫祭 ꥪ PrettyMenu ࠭  䮭 ⨭    㤥 - 䮭  㬮砭,  짮⥫   ࠭ , . (⮡ ᭨ ,  ਤ 맢 - 㭪-童,  ᮮ,  ᥩ ᯮ ⨭.)
 㭪樨, ।⠢騥 ண ࠭, ,  ᫨ ᪫祭 㤥 㦤,  ﭨ ணࠬ  . 맮 ⠪ 㭪樨  ⮬; ᫨  訫 ᯥ譮,   ஢ ⢨ 믮  , ᫨  ,  ணࠬ ⠭  ⠪ ﭨ,   㭪   뢠.
  㭪ﬨ, ।⠢騬 ⠪ ࠭, , 祬  㭪ﬨ,   ⮫쪮  ࠭, ⮬  ᫥  맮   ⮫쪮  ﭨ ணࠬ: , ஥   १  ᯥ譮 襭,  , ஥ 뫮   맮. ⨢, ᫨ ᪫祭   㭪樨, ।⠢饩 ⮫쪮  ࠭,  ணࠬ     ४⭮ ﭨ.
 㭪樨, ।⠢騥 ࠭ ⢨ ᪫祭,    㦤 ᪫祭, ⮬  ᥣ  ,   .  樨  ஥묨 ⨯ (ਬ, 楫묨, 㪠⥫ﬨ  . .) ᯥ稢 ⠪ ࠭.  ᭮ ந⥫  ᭮ ⭮⥫쭮 ᪫祭 . 㬭 ।,  㭪樨  ⮩ ᯥ䨪樥 ᪫祭  㦤 ,    ᥣ ⠪. ਬ, ᬮਬ ᫥ 㭪:

int doSomething() throw(); //    
// ᯥ䨪 ᪫祭

     ⮬,  doSomething   㦤 ᪫祭. ⢥ত ,  ᫨ doSomething 㤨 ᪫祭, , ந諠 쥧 訡    맢 㭪 unexpected[3]. ᪨ doSomething    ।⠢  ࠭⨩ ⭮⥫쭮 ᪫祭.  㭪樨 (饥  ᯥ䨪 ᪫祭) 祣  ᮮ頥 ⭮⥫쭮 ⮣,    ४⭮, ७ᨬ, 䥪⨢,  ࠭⨨ ᭮ ᪫祭  ।⠢  ।⠢   .   ࠪ⨪ । ॠ樥 㭪樨,    .
 ⭮⥫쭮 ᪫祭   ।⠢    ᠭ ࠭⨩. ᫨  ⮣  ,    . 롮, ⠪ ࠧ,  ⮬, ⮡ ।,  ⨯ ࠭⨨  ।⠢   ᠭ  㭪権. ᫨   㭠᫥ ,  ⭮⥫쭮 ᪫祭 ( ⮬  ਬ   饬 ࠢ),  ⢨ ࠭⨩ ⨬ , ᫨  १  ॡ 뫮 襭,  ਫ  易 ᪠  ᮢ  ࠡ  ०묨 ࠬ .
 , 㦭 ६ ।⠢ ᨬ쭮 ண ࠭⨨.  窨 ७ ᭮ ᪫祭 㭪樨,  㦤騥 ᪫祭, 㤥,  祭 㤭,  ⠢  ࠬ 몠 C,   맮 㭪権, 㦤 ᪫祭.  ,  ஬ ᯮ ᪮ ।  (ਬ, STL-⥩),  㦤 ᪫祭 bad_alloc,   㤠  筮 ꥬ ᢮  (. ࠢ 49). ।⠢ ࠭⨨ ⢨ ᪫祭,  ,   設⢠ 㭪権  ⮫쪮 롮    ண ࠭⨥.
 㭪樨 changeBackground ।⠢  ண ࠭ 㤭. -,  ⨯  bgImage   PrettyMenu  ஥ 㪠⥫ *Image    <⥫㠫> ࠢ ᠬ 㪠⥫, ᠭ  ࠢ 13. ஢ ,    砥 , ᪮   祪 ᮢ.  䠪,     ᯥ ண ࠭ ᭮ ᪫祭,  ⢥ত ਢ  ࠢ 13 㬥   ਬ ꥪ⮢ ( ⥫㠫 㪠⥫)  ࠢ ᠬ.   ᯮ짮 ᮬ tr1::shared_ptr, ⮬    ᥡ  ⢥  ஢, 祬 auto_ptr.
-, 㦭  冷 ।  㭪樨 changeBackground ⠪, ⮡ 祭 稪 imageChanges  㢥稢   ,  ⨭  㤥 . 饥 ࠢ ⠪: 砩  ꥪ,  ந諮 ஥ , ⮫쪮 ᫥ ⮣,    ⢨⥫쭮 믮.
  砥  १:

class PrettyMenu {
...
std::tr1::shared_ptr<Image> bgImage;
...
};
void PrettyMenu::changeBackground(std::istream& imgSrc)
{
Lock ml(mutex);
BgImage.reset(new Image(imgSrc)); //  ७ 㪠⥫
// bgImage १⮬ ࠦ
// "new Image"
++imageChanges;
}

⬥⨬,    室  㤠  ⨭, ⮬    <⥫㠫> 㪠⥫.  ⮣, 㤠 ந室 ⮫쪮  ⮬ 砥, ᫨  ⨭ ᯥ譮 ᮧ. 筥 , 㭪 tr1::shared_ptr::reset 㤥 맢, ⮫쪮  ⮬ 砥,   ࠬ (१ ᫥ <new Image(imgSrc)>) ᯥ譮 ᮧ.  delete ᯮ ⮫쪮  맮 reset, ⮬ ᫨ 㭪  砥 ࠢ,   delete  뢠. ⬥⨬ ⠪,  ᯮ짮 ꥪ (tr1::shared_ptr)  ࠢ ᮬ (᪨ 뤥 ꥪ⮬ Image)  ᥬ 祬 㬥蠥 ࠧ 㭪樨 changeBackground.
  ᪠,     changeBackground ।⠢  ண ࠭ ᭮ ᪫祭.  祣   墠⠥?   ࠬ imgSrc. ᫨  Image 㤨 ᪫祭,  ,  㪠⥫ ⥭  室 ⮪ ᬥ,  ⠪ ᬥ饭    ﭨ,  ⠫쭮  ணࠬ.      㭪樨 changeBackground   ⮪,  ।⠢ ⮫쪮  ࠭ ᭮ ᪫祭.
 ⠢  ஭    㤥 ,  changeBackground ।⠢ ண ࠭ ᭮. ( ᥪ ᮮ,   ᯮᮡ  ⮣,  ⨯ ࠬ  istream   䠩, ᮤঠ饣  ⨭.)   ⥣ ஥஢,  ᯥ稢 ண ࠭,    . ⥣ 뢠 <᪮஢  > (copy and swap).  ਭ樯,  祭 .   ꥪ,  ᮡࠥ ஢, ⥬   室   . ᫨   権 䨪樨 㤨 ᪫祭, 室 ꥪ ⠭ .     ᯥ譮 ᥭ,  ஢ ꥪ  室   樨,  㦤饩 ᪫祭.
筮  ॠ 饭   ⭮襭  ꥪ   <ॠ쭮> ꥪ  ⤥ ७ ꥪ,    <ॠ쭮> ꥪ  㪠⥫.   ਥ 뢠 < pimpl>,   ࠢ 31  뢠  ஡.   PrettyMenu   룫拉 ਬ୮ ⠪:

struct PMImpl { // PMImpl = "PrettyMenu Impl":
std::tr1::shared_ptr<Image> bgImage; // .  - 祬 
int imageChanges; // ,   
}
class PrettyMenu {
...
private:
Mutex mutex;
std::tr1::shared_ptr<PMImpl> pimpl;
};
void PrettyMenu::changeBackground(std::istream& imgSrc)
{
using std::swap; // . ࠢ 25
Lock ml(&mutex); // 墠 ⥪
std::tr1::shared_ptr<PMImpl> // ஢  obj
pNew(new PMImpl(*pimpl));
pNew->bgImage.reset(new Image(imgSrc)); // ஢ 
++pNew->imageChanges;
swap(pimpl, pNew); //  祭
} // ᢮ ⥪

 ⮬ ਬ  訫 ᤥ PMImpl ன,   ᮬ, ⮬    PrettyMenu ⨣   ⮣,  童 pImpl  .  PMImpl ᮬ 뫮  祬  㦥,    㤮 ( ୨ <ꥪ⭮-ਥ஢ > 뫨  ). ᫨ 㦭, PMImpl    PrettyMenu,  ⠪ ६饭     ᠭ ᭮ ⭮⥫쭮 ᪫祭 .
⥣ ஢   -  ⫨ ᯮᮡ    ﭨ ꥪ  ਭ樯 <  祣>,   饬 砥  ⮬  ࠭,   㭪  楫 ண ᭠ ⭮⥫쭮 ᪫祭. ⮡  祬, ࠣ㥬  㭪樨 changeBackground  ᬮਬ    㭪 someFunc,  ᯮ ஢  ,    頥   㣨 㭪: f1  f2.

void someFunc()
{
... // ᪮஢ 쭮 ﭨ
f1();
f2();
... //  ஢ ﭨ  
}

  ᭮,  ᫨ f1  f2  ᯥ稢 ண ࠭⨩ ᭮ ᪫祭,  㤥 㤭 ᯥ    someFunc  楫. ਬ, ।,  f1 ᯥ稢 ⮫쪮  ࠭. ⮡ someFunc ᯥ稢 ண ࠭, 室  , ।騩 ﭨ ᥩ ணࠬ  맮 f1, 墠  ᪫祭,   㤨 f1,  ⥬ ⠭ 室 ﭨ.
  ⠭ ⢥ , ᫨  f1,  f2 ᯥ稢 ண ࠭ ᭮ ᪫祭.  ᫨ f1 ଠ쭮 ࠡ⠥  , ﭨ ணࠬ   ந ࠧ, ⮬ ᫨ f2 㤨 ᪫祭,  ﭨ ணࠬ  㤥 ⥬ ,  । 맮 someFunc,  ᫨ f2   祣.
஡   䥪.     㭪  ⮫쪮  ﭨ (  someFunc  ⮫쪮  ﭨ ꥪ,  ண 맢), ⭮⥫쭮  ᯥ ண ࠭.   㭪   䥪, ࠣ騥  ,  ⠭ ᫮. ᫨, ਬ,  䥪⮬ 맮 f1  䨪  , 㤥 㤭 ᯥ ண ࠭  someFunc.   ᯮᮡ ⬥ 䨪  ,  㦥 뫠 ᮢ襭: 㣨   㦥 㢨  ﭨ.
 樨   ।⠢ ண ࠭⨨ ᭮  㭪樨,  ᫨  ⥫   ᤥ. ஬ ⮣,  ਭ   䥪⨢.  <஢  >  ⮬, ⮡ ஢   ꥪ,  ⥬  ஢  室  樥,   㦤 ᪫祭.  ⮣ 㦭 ᤥ   ꥪ,   䨪樨,  ॡ ६  , 묨 , ,  ᯮ. ண ࠭ 쬠 ⥫쭠,    ᯥ稢 ,   ࠧ㬭  ࠪ筮,   易⥫쭮   .
  ।⠢ ண ࠭,   ᯥ .  ࠪ⨪   ⠪,    㭪権  ᯥ ண ࠭, ⮣    㣨  ࠧ㬭  ᮮࠦ 䥪⨢  ᫮. ᫨  ᤥ    ᯥ祭 ண ࠭⨨ ⠬,   ࠢ,   ࠢ ⨪   ,   ⠫   ।⠢ ⮫쪮  ࠭.   㭪権  ࠭ - ᮢ襭 ࠧ㬭 롮.
ᥬ 㣮 , ᫨   㭪,    ।⠢  ࠭⨩ ᭮ ᪫祭.  㯠  ᨫ १㬯 : 㤨 ⠥ ,    ⭮.    ,  ⭮⥫쭮 ᪫祭.     ࠢ  . ᬮਬ  ࠧ ॠ 㭪樨 someFunc,  뢠 f1  f2. ।,  f2  ।⠢  ࠭⨩ ᭮ ᪫祭,  .  ,  ᫨ f2 㤨 ᪫祭,   窠 ᮢ  f2.  ⠪ 砥,  f2  ।  , ਬ ஢ ᨢ   ஢묨, ꥪ,  ஢     ,    . . 㭪 someFunc 祣    ⨬ . ᫨ 뢠  someFunc 㭪樨  ࠭ ᭮ ⭮⥫쭮 ᪫祭,   someFunc   ।⠢  ࠭⨩.
 ⥯     ⥬ ६. 騭  ६,  .   - ६. 筮 ணࠬ ⥬   ᭮  ᪫祭,  .  ⠪ ,  筮 ᭠ ⥬. ᫨ ⥬  ᥣ   ⭮⥫쭮 ᪫祭 㭪,   ᭠   楫, ⮬  맮 ⮩ 㭪樨  ਢ  窥 ᮢ  ०  .  , 設⢮ 㭠᫥   C++ 뫮 ᠭ   ॡ ᭮ ᪫祭, ⮬  ⥬  ᥣ   ⮬ ⭮襭 묨.   , ᠭ  ᭮ .
  稭 ࠭ ⠪   ᥣ.  ᠭ    䨪樨 饣 ⥫쭮 த뢠 ᯮᮡ ⨦ ᭮ ᪫祭. 筨  ਬ ꥪ⮢ ࠢ ᠬ (. ࠢ 13).  ।  ᮢ. ⥬ ।,  ᨬ   ࠭⨩ ᭮ ᪫祭   ᯥ  ࠧࠡ뢠 㭪権, ⠢  묨 ⮫쪮  ⮬ 砥,  맮 㭠᫥   ⠢ 㣮 롮. 㬥  襭   짮⥫  㭪権, ⠪   ᮯ஢  饬. ࠭ ᭮ ᪫祭 㭪樨 -     䥩, ⮬   室   ⮫  ⢥⢥,    㣨 ᯥ⠬ 䥩.
ப   , 騩 ࠬ goto, ⠫  ਥ.    ࠥ  ஢ ணࠬ.          뢠 ࠦ.   ६  㫨஢.    ᠭ 㭪権   ﭨ ᪫祭 뫮 ମ.  ᥩ  ६  ⨦ ᭮ ⭮⥫쭮 ᪫祭.
६ .  .  稬.

 ᫥ 
  ⭮⥫쭮 ᪫祭 㭪樨  ᪠ 窨 ᮢ  ०  ,   砥 㦤 ᪫祭.  㭪樨 ।⠢  ࠭, ண ࠭  ࠭  ⢨ ᪫祭.
 ண ࠭    ॠ ।⢮ ஢  ,  ।⠢    㭪権 ࠪ筮.
 㭪 筮  ।⠢ ࠭  ஦, 祬 ᠬ ᫠ ࠭, ᯥ稢 뢠묨   㭪ﬨ.



ࠢ 30: ⥫쭮 뢠 ᯮ짮 ஥ 㭪権

஥ 㭪樨 -  ⥫쭠 !  룫  㭪,  ࠡ  㭪,    ᮢ (. ࠢ 2).   뢠,    室, 易  맮  㭪権.   ?
 ⢨⥫쭮  砥 , 祬 뢠, ⮬      맮 㭪樨 -  ⮫쪮 . ⨬, 믮塞 ஬, 筮  䥪⨢  ⪥ ,  ᮤঠ饬 맮 㭪権.  ࠧ,     ⨬樨 ⥫ ஥ 㭪樨  ᨬ  ꥬ饣 ⥪.  ᯮ짮 <筮> 㭪樮쭮 맮 設⢮ ஢ ⠪ ⨬樨    믮.
    㤥 ᫨誮 㢫.  ணࠬ஢,    ॠ쭮 ,  뢠 <ᯫ ࠪ>,  ஥ 㭪樨 -  ᪫祭.   ᯮ짮 ⮨    맮 ⠪ 㭪樨  ⥫.  㦭  ஬ ⥬᪨ , ⮡ ,   㢥 騩 ࠧ 襣 ꥪ⭮ . 誮 ⮥ ਬ ஥ 㭪権  設  ࠭祭   ਢ  ᮧ ணࠬ,  ॢ室 㯭 .   稨 㠫쭮  <ࠧ堭> , 맢 ਬ ஥ 㭪権,  ਢ  ⥫쭮   ᪮, 㬥 樥     , ᫥⥫쭮, ᭨ ந⥫쭮 ணࠬ.
 㣮 ஭, ᫨ ⥫ ஥ 㭪樨 祭 ⪮,  ᣥ஢       , ᣥ஢  맮 㭪樨.  ⠪ 砥 ࠨ 㭪樨  ਢ  㬥襭 ꥪ⭮   襭 樥   !
  ,  ४⨢ inline -  ᮢ,    .    㫨஢   .  ᯮᮡ 砥  । ஥ 㭪樨  । :

class Person {
public:
...
int age() const { return theAge;} //    ࠨ;
... // 㭪 age ।  
private:
int theAge;
};

 㭪樨 筮  㭪ﬨ-童,   ࠢ 46 ,  㭪樨- ⮦   ।  .  ⮬ 砥  ⠪   ஥묨.
  ஥ 㭪 , ।ਢ  । 祢 ᫮ inline. ਬ,   筮 ॠ ⠭ 蠡 max ( 筮 䠩 <algorithm>):

template <typename T> //   
inline const T& std::max(const T& a, const T& b) // ࠨ: 㭪樨
{ return a < b ? b : c;} // std::max ।
// ᫮ inline

 䠪,  max -  蠡,   ,  ஥ 㭪樨  蠡 筮    䠩.  ணࠬ   ⮣ 뢮,  蠡 㭪権 易⥫쭮   ஥묨.  祭 ६ ୮  ⥭樠쭮 ᭮, ⮬ ᬮਬ  ⥫쭥.
஥ 㭪樨 筮  室   䠩, ᪮ 設⢮ ࠧࠡ⪨ ணࠬ 믮 ࠨ  ६ 樨. ⮡  맮 㭪樨 ࠨ  ⥫,   㢨  㭪. ( ।  ࠨ 㭪樨  ६ ,    ⠪ - ਬ, । ࠧࠡ⪨  . NET Common Language Infrastructure (CLI),-  ⢫ ࠨ  ६ ᯮ.   ᪮॥ ᪫祭, 祬 ࠢ. ࠨ 㭪権  設⢥ ணࠬ  C++ ந室  ६ 樨.)
 筮 室   䠩, ⮬    ,  蠡 룫廊, ⮡ ⨧஢    ᯮ짮. (   ࠢ   㭨ᠫ.  । ࠧࠡ⪨ 믮 ⨧ 蠡  ६ .  ⨧  ⠯ 樨 砥 .)
⨧ 蠡   易  ࠨ. ᫨  ,   㭪樨, ⨧஢  襣 蠡,   ஥묨,  蠡 ஥ (inline);  ⠪ ࠧࠡ稪 ⠭⭮ ⥪ 㯨  蠡 std::max (. ਬ ).  ᫨   蠡  㭪樨,   ᫠  ஥,   ஥   蠡 (  ). ࠨ 室 ண,           ࠧ諥.  㦥 㯮,  ࠨ ࠧ㢠  (ᮡ    ࠧࠡ⪥ 蠡 - . ࠢ 44),    㣨 ,   ᪮ 㤨.
 ० ,  ࠨ -  ᮢ,    ந஢. 設⢮ ஢ ⢥࣠ ࠨ 㭪権,  ।⠢ ᫨誮 ᫮묨 (ਬ, ᮤঠ 横  ४),   ᪫祭  ਢ 砥, 맮 㠫쭮 㭪樨 ⬥ ࠨ.  ⮬  祣 㤨⥫쭮: virtual 砥 < 筮 㭪 맢, ।   ᯮ>,  inline - <। ᯮ  맮 㭪樨  >. ᫨   ,  㭪 뢠,  㤭    ⮬,   ⪠뢠  ࠨ.
   筮  ᢮  ᫥饬:  ॠ樨 ᯮ㥬  , ࠨ   ⢨⥫쭮 ஥ 㭪.  , 設⢮ ஢  묨 ᪨ ﬨ  뤠 ।० (. ࠢ 53), ᫨   믮 襭  ࠨ.
   ⥫ ஥ 㭪樨,  ᫨   蠥  ந. ਬ, ᫨  ணࠬ 砥  ஥ 㭪樨,  ,  ࠢ,  ᣥ஢ 饥 ⥫ 㭪樨.       㭪樨, ᫨   ?  ᮢ㯭  ⥬ 䠪⮬,  筮   믮 ࠨ, ᫨ 㭪 뢠  㪠⥫,  ,  맮 ஥ 㭪権  ࠨ   ࠨ  ᨬ  ⮣,    ந 饭:

inline void f() {...} // ।,    ந 맮 f
void (*pf)() = f; // pf 㪠뢠  f
...
f(); //  맮 㤥 ஥, ⮬  
// <ଠ>
pf(); //  맮, ⭮,  㤥 ஥, ⮬ 
// 㭪 맢  㪠⥫

ਧࠪ ࠨ inline-㭪権  ᫥ ,  ᫨    ᯮ 㪠⥫  㭪樨, ⮬  㪠⥫  㭪樨  訢  ⮫쪮 ணࠬ.    ࠨ  ஢  ஢ ⠪,   訢 㪠⥫  㭪樨  ६ ஢  ࠧ襭 ꥪ⮢  ᨢ.
᪨      訬 ⠬  ࠨ. ਬ, ᬮਬ   Derived:

class Base {
public:
...
private:
std::string bm1, bm2; // 童   1  2
};
class Derived: public Base {
public:
Derived(){} //  Derived  -  ⠪ ?
...
private:
std::string dm1, dm2, dm3; // 童 ந  1-3
};

  룫廊  ⫨   ࠨ, ᪮   ᮤন  .  ⫥ 稢.
C++  ࠧ ࠭⨨  ⮬,   ந室  ஢  ࠧ襭 ꥪ⮢. ਬ,   ᯮ  new, ᪨ ᮧ ꥪ ⮬᪨ 樠 ᢮ ࠬ,   饭  delete 뢠 ᮮ⢥騥 .   ᮧ ꥪ,  ⮬᪨  童    ᮢ,  ࠢ  ᮡ⢥ -童,   ६ 㤠 ꥪ ⮬᪨ ந室  . ᫨  ६ ஢ ꥪ 㦤 ᪫祭,    ꥪ,  뫨  ⮬  ᪮஢, ⮬᪨ ࠧ.     C++ ,   ,    - .    ॠ樨 ,    ⭮,  ⠪   ந室 ᠬ  ᥡ.  襩 ணࠬ   - ,    ॠ,   ,   ஬  ⠢   ணࠬ,  - 室.   頥    , ⮬  ।⠢ ᥡ ᫥ ॠ ᣥ஢   类 ⮬   Derived:

Derived::Derived() // 楯㠫쭠 ॠ
{ // <⮣>   Derived
Base::Base(); // 樠஢  Base
try {dm1.std::string::string();} // ⪠ ᪮஢ dm1
catch(:) { // ᫨ 㦤 ᪫祭,
Base::~Base(); // ࠧ   
throw; // ࠭ ᪫祭 
}
try {dm2.std::string::string();} // ⪠ ᪮஢ dm2
catch(:){ // ᫨ 㦤 ᪫祭,
dm1.std::string::~string(); // ࠧ dm1
Base::~Base(); // ࠧ   
throw; // ࠭ ᪫祭
}
try {dm3.std::string::string();} // ᪮஢ dm3
catch(:){ // ᫨ 㦤 ᪫祭,
dm2.std::string::~string(); // ࠧ dm2
dm1.std::string::~string(); // ࠧ dm1
Base::~Base(); // ࠧ   
throw; // ࠭ ᪫祭
}
}

 ⢨⥫쭮   ᮢᥬ  ,  ஦ , ⮬  ॠ  ࠡ뢠 ᪫祭  ᫮ ࠧ.     ਬ 쭮 筮 ࠦ  <⮣>   Derived. ᨬ  ⮣, ᪮쪮 㬭 室  ᪫祭ﬨ ,  Derived ,  ࠩ , 뢠  ᢮ -童   ,   맮 ( ᠬ  ᥡ   ஥묨)  ᢥ २⢠ ࠨ  .
  ᠬ ⭮     Base, ⮬ ᫨  ஥,   ⠢    ⠢ ⠪    Derived (᪮  Derived 뢠  Base).  ᫨   string ⮦  ஥,    Derived     ࠧ -          Derived ப ( 㭠᫥  ,    ᠬ). ୮, ⥯  ᭮, 祬 襭  ࠨ  Derived  ⮨ ਭ   楬. 筮     ஬  Derived,  - ࠧ  ࠭஢ ࠢ쭮 㭨⮦  ꥪ⮢, 樠஢ ஬.
ࠡ稪 ⥪  ਭ  ,  ந   㭪権 ஥묨, ⮬   ।⠢ 筮    ஥  㭪権. 㣨 ᫮, ᫨ f - ஥ 筠 㭪,  짮⥫ ⮩ ⥪ ࠨ  ⥫  ᢮ ਫ. ᫨ ࠧࠡ稪 ⥪    f,   ணࠬ,   ᯮ짮, ਤ ⪮஢ .   ⥫쭮.  㣮 ஭, ᫨ f  㤥 ஥ 㭪樥,  ᫥  䨪樨 ᪨ ணࠬ 㦭 㤥     ⥪.  ⨬ ॥, 祬 ४,  ᫨ ⥪, ᮤঠ 㭪,  ᪮,       ஧  짮⥫.
 ࠧࠡ⪥ ணࠬ       ᮮࠦ,   ࠪ᪮ 窨 ७  ⢥ ᫥騩 䠪:  設⢠ ⫠稪  ஡  ஥묨 㭪ﬨ.  ᮢᥬ  㤨⥫쭮.  ⠭  ⠭  㭪樨, ன  ?   । ࠧࠡ⪨  ন ⫠ ஥ 㭪権,   ࠨ  ⫠ ᨩ  ⪫砥.
 ਢ   ᫥饩 ⥣ 롮 㭪権, 室  ࠨ. 砫 ⪠  ஥ 㭪権 , ,  ࠩ , ࠭ ⥬,  易  ஥묨 (. ࠢ 46)   ਢ묨 (⠪  Person::age ). ਬ ஥ 㭪樨   ⭮,   ⮫쪮 砥  짮 ⫠稪,   । ࠨ 饥 : ⮭ ⨬ .  뢠  ᪮ ࠢ <80-20>, ஥ ⢥ত,  ⨯筠 ணࠬ  80 % ६  ᯮ 20 % .   ࠢ, ᪮  ,  楫 ࠧࠡ稪 ணࠬ ᯥ祭 - ஢  20 % ,  ⢨⥫쭮 ᯮᮡ  ந⥫쭮 ணࠬ.   ᪮筮 ⨬஢   㭪樨 inline,    㤥 ⮩ ⮩ ६, ᫨ ⮫쪮   । ᨫ  㦭 㭪.

 ᫥ 
  ࠨ묨 ⮫쪮 訥,  뢠 㭪樨.   ⫠,   믮  ⥪  筮 ஢, 㬥 䥪 <ࠧ堭>     த⢨ ணࠬ.
   蠡 㭪権 ஥묨 ⮫쪮 ⮬,      䠩.



ࠢ 31: 蠩 ᨬ 䠩  樨

ᬮਬ ᠬ 몭 .  뢠 ᢮ ணࠬ  C++   ⥫   ॠ . ,   䥩 ,    ॠ - ⮫쪮   童. ᫥ ⮣  稭  ᮡ ணࠬ, 뢠,     ᪮쪮 ᥪ㭤.   殢,   ஢ ᥣ  .  饫   Build  ࠥ make ( - ), : 㤨,  ⥬ - ,  㦨,  ४     !  ࠢ ,   ᪮ ?
஡ 易  ⥬,  C++  ஢ ᪮쪮- ⥫쭮 ࠧ  䥩ᮬ  ॠ樥.  ⭮, । ᮢ   ᥡ  ⮫쪮 ᯥ䨪 䥩,  ⠪  楫  ⠫ ॠ樨. ਬ:

class Person {
public:
Person(const std::string& name, const Date& birthday,
const Address& addr);
std::string name() const;
std::string birthDate() const;
std::string address() const;
...
private:
std::string theName; // ⠫ ॠ樨
Date theBirthDate; // ⠫ ॠ樨
Address theAddress; // ⠫ ॠ樨
};

 Person  ᪮஢,   㯠  । ᮢ,     ॠ,   string, Date  Address.  । 筮 ।⠢ ।⢮ ४⨢ #include, ⮬ 쬠 ⭮,   砫 䠩, ।饣  Person,    த:

#include <string>
#include "date.h"
#include "address.h"

 ᮦ,  ⠭ ᨬ ६ 樨  䠩 । Person  砥묨 䠩. ᫨     䠩    䠩,    ,    ४஢ 䠩, ᮤঠ騩 । Person,  ࠢ   䠩,   Person ᯮ.  ᪠ ᨬ   쬠 ६⥫  짮⥫.
  ᮬ, 祬 C++ ⠨  ࠧ饭 ⠫ ॠ樨   । . ਬ, 祬  । Person ᫥騬 ࠧ:

namespace std {
class string; // ०饥 
} // (४⭮ - . )
class Date; // ०饥 
class Address; // ०饥 
class Person {
public:
Person(const std::string& name, const Date& birthday,
const Address& addr);
std::string name() const;
std::string birthDate() const;
std::string address() const;
...
};

᫨  ⠪ 뫮 ,  짮⥫  Person  뫨 ४஢ ᢮ ணࠬ ⮫쪮    䥩.
,  ॠ樨 ⮩   ⠫   ஡. ࢠ: string -   ,  typedef (ᨭ 蠡 basic_string<char>). ⮬ ०饥  string ४⭮. ࠢ쭮  ࠧ ᫮, ⠪     ⥫ 蠡. 祬,   , ⮬     砥     -  ⠭⭮ ⥪.  ⮣     #include ࠢ   ᯮ. ⠭    ⠭ 㧪 ⮬  樨, ᮡ ᫨  । ࠧࠡ⪨ ন ।஢  䠩. ᫨   ⠭    室  ६,        ᯮ짮  ⥩ ⠭⭮ ⥪,   ⥫쭮.
 (  ⢥) ⭮, 易  ०騬 , ⮨  ⮬,   室  ࠧ ꥪ⮢  ६ 樨. ᬮਬ ਬ:

int main()
{
int x; // ।塞 int
Person p(params); // ।塞 Person
...
}

   । x,  ,   뤥 筮  (筮  ⥪)  ࠧ饭 int.  ஡:   ,   int.  । p,  뢠,  㦭 뤥   Person,  㤠  , ᪮쪮   ॡ? ⢥ ᯮᮡ   ଠ - ࠢ  । ,  ᫨   । ᮢ  뫮 ᪠ ⠫ ॠ樨,   ᭨ , ᪮쪮  室 뤥?
     몠 ⨯ SmallTalk  Java, ⮬   । ꥪ  뤥 ⮫쪮 ,   ࠭ 㪠⥫   ꥪ.  ,  모  襯ਢ ,  ᫨    ᠭ ᫥騬 ࠧ:

int main()
{
int x; // ।塞 int
Person *p; // ।塞 㪠⥫  Person
...
}

     C++, ⮬   ᠬ ᬮ ஢ <᮪⨥ ॠ樨 ꥪ  㪠⥫>.  砥  Person   ᤥ, ਬ, ࠧ    :  -  ।⠢ 䥩,  㣮 -   ॠ樨. ᫨ , ᮤঠ騩 ॠ,  Personlmpl,  Person   ᠭ ᫥騬 ࠧ:

#include <string> //  ⠭⭮ ⥪
//     ।⥫쭮
#include <memory> //  tr1::shared_ptr; . 
class PersonImpl; // ०饥  PersonImpl
class Date; // ०饥  ᮢ,
class Address; // ᯮ㥬  䥩 Person
class Person {
public:
Person(const std::string& name, const Date& birthday,
const Address& addr);
std::string name() const;
std::string birthDate() const;
std::string address() const;
...
private: // 㪠⥫  ॠ:
std::tr1::shared_ptr<PersonImpl> pImpl; // . ࠢ 13 ଠ
}; //  std::tr1::shared_ptr

   (Person)  ᮤন  -童, ஬ 㪠⥫ (  砥 tr1::shared_ptr - . ࠢ 13)  ᢮  ॠ樨 (Personlmpl).    뢠 < pimpl> (<pointer to implementation> - 㪠⥫  ॠ).    㪠⥫  뢠 pImpl,   ਢ ਬ.
 ⠪  짮⥫  Person    ⠫ - , ᮢ  .    ஢  㣮,  ⮬ ४஢ ணࠬ,   ᯮ Person,  ਤ. ஬ ⮣, ᪮ 짮⥫   ⠫ ॠ樨 Person,     ,  - ࠧ 㤥    ⠫.     뢠 ⤥ 䥩  ॠ樨.
箬  ⮬ ࠧ 㦨  ᨬ  । (definition)  ᨬ   (declaration).    魮 樨 ᨬ⥩  ⠯ 樨:   楫ᮮࠧ,   䠩 ᠬ묨; ⨢ 砥 ᯮ ᨬ  ,    ।.  ⠫쭮 ⥪  ⮫쪮   ⥣ ஥஢. 㫨㥬  ࠪ᪨ ᫥⢨:
  ᯮ짮 ꥪ⮢, ᫨  蠭  뫪  㪠⥫ﬨ.   । 뫪  㪠⥫,  ⮫쪮  ⨯. । ꥪ⮢ ॡ  । ⨯.
   ᯮ ᨬ  ,    । . ⬥⨬,    㭪樨, ᯮ饩  ,   ॡ । ⮣ ,  ᫨ 㭪 ਭ  頥 ꥪ   祭:

class Date; //  
Date today(); // ࠢ쭮, 室
void clearAppointments(Date d); //  । Date 

筮, ।  祭 -  祭   (. ࠢ 20),  ᫨  ⮩   稭  㤥 㦤  ᯮ짮,    ࠢ  㦭 ᨬ⥩.  ᪫祭,    㭪樨 today  clearAppoinments  । Date ࣫   㤨,   ᠬ    ⠪  ࠭. । Date   㯭   맮  㭪権. ,  ,  祬  㬠: 祬  㭪樨,    뢠? ⢥ .    ⮬,    뢠 ,   ⮬,   뢠  . ਬ, ᫨  ⥪, ᮤঠ ⪨  㭪権,  ⭮,   짮⥫ 뢠  㭪. ७ ६ ⢥⢥  ।⠢ ।     䠩, ᮤঠ  㭪権,  짮⥫᪨ 䠩, ᮤঠ騥  맮,  ᪫砥 ⢥ ᨬ 짮⥫  । ⨯,    ⢨⥫쭮  㦭.
 頩   ।  ࠧ  䠩. ⮡ 뫮  ਤন ᠭ  ਭ樯, 䠩   ⠢ ࠬ:  -  , ன -  ।. 筮, 㦭, ⮡  䠩 뫨 ᮣᮢ. ᫨     ,  㦭     ஬.  १ 짮⥫ ⥪ ᥣ   䠩 ,    ᠬ⥫쭮 ०騥 , ⮣   ⥪  ⠢   䠩.
ਬ, ᫨ 짮⥫  Date   㭪樨 today  clearAppointments,   ᫥   ०饥   Date,  뫮  .  ⮣    ४⨢ #include ᮮ⢥騩 䠩  ﬨ:

#include "datefwd.h" //  䠩,  ஬ 
// (  ।)  Date
Date today(); //  ࠭
void clearAppointments(Date d);

  ﬨ  <datefwd.h>     䠩 <iosfwd>  ⠭⭮ ⥪ C++ (. ࠢ 54). <iosfwd> ᮤন  ⮢ iostream, ।  室  ᪮쪨 ࠧ ,  <sstream>, <streambuf>, <fstream>  <iostream>.
ਬ <iosfwd> ⥫    㣮 稭.   ᫥,  ᮢ ⮣ ࠢ ⭮  ࠢ   蠡   ᠬ.   ࠢ 30 ᭥,    । ࠧࠡ⪨ ணࠬ । 蠡 筮 室   䠩,    த ᪠ ࠧ饭 । 蠡   㣨 , ⮬     ।⠢  䠩, ᮤঠ騥 ⮫쪮 ,   蠡. <iosfwd>-  ࠧ ਬ ⠪ 䠩.
 C++  ⠪ 祢 ᫮ export, 饥 ⤥  蠡   ।.  ᮦ, প ࠬ ⮩  ࠭祭,  ࠪ᪨   ਬ ᮢᥬ .   ᫨誮 ࠭ ,  ஫ 㤥  ᫮ export  䥪⨢ ணࠬ஢  C++. ,  Person,   ᯮ  pimpl,  뢠 ᠬ-ਯࠬ (handle classes). ⢥  ,  ࠧ ࠡ ⠪ , :  ॠᮢ뢠  맮 㭪権 ᮮ⢥騬 ᠬ ॠ権,   믮  ॠ ࠡ. ਬ,     ॠ  㭪樨-童 Person:

#include "Person.h" // ᪮  ॠ㥬  Person,
//     ।
#include "PersonImpl.h" //   ⠪  । 
// PersonImpl,   ᬮ 뢠 
// 㭪樨-童; ⬥⨬,  PersonImpl 
//  筮   㭪樨-童,  
// Person:  䥩 
Person::Person(const std::string& name, const Date& birthday,
const Address& addr)
: pImpl(new Person(name, birthday, addr))
{}
std::string Person::name() const
{
return pImpl->name();
}

   ,   Person 뢠  Personlmpl (ᯮ new - . ࠢ 16),   Person::name 뢠 PersonImpl::name.   . ॢ饭 Person  -ਯ     -  ⮫쪮 ,  ஬   ॠ.
ୠ⨢ 室  ᯮ짮 -ਯ - ᤥ Person ࠪ  ᮬ ᯥ樠쭮 , 뢠 䥩 ᮬ.  祭 - ᯥ஢ 䥩  ந ᮢ (. ࠢ 34).  १  筮  ᮤন  -童,  ஢,   㠫  (. ࠢ 7)    㠫 㭪権, । 䥩.
䥩  த 䥩ᠬ Java . NET,  C++  뢠  䥩   ࠭祭,   ⨬ 몠. ਬ,  Java, . NET  ᪠  䥩  童-  ॠ権 㭪権-童. C++ ⮣  頥.   C++  ⮬ ⭮襭   .    ࠢ 36, ॠ 㠫 㭪権      ᮢ  娨, ⮬   ॠ ⠪ 㭪樨,   䥩᭮ ,  ஬  .
䥩  Person  룫拉 ਬ୮ ⠪:

class Person {
public:
virtual ~Person();
virtual std::string name() const = 0;
virtual std::string birthDate() const = 0;
virtual std::string address() const = 0;
...
};

짮⥫ ⮣   ணࠬ஢  ନ 㪠⥫  뫮  Person, ⮬   ᮧ  , ᮤঠ饣  㠫 㭪樨 (  ᮧ  ᮢ, ந  Person - . ). 짮⥫ 䥩 ᮢ,   짮⥫ ᮢ-ਯ஢,  㦤 ஢ ४   ,    䥩.
筮, 짮⥫ 䥩 ᮢ   ᯮᮡ ᮧ  ꥪ. 筮   , 뢠 㭪,  ஫   ந ᮢ,   室 ᮧ.  㭪樨  뢠 㭪ﬨ-䠡ਪ (. ࠢ 13),  㠫묨 ࠬ.   㪠⥫ (   ⥫㠫, . ࠢ 18)  ᪨ । ꥪ,  ন 䥩 䥩᭮ . ।  㭪樨   ᪨  䥩᭮ :

class Person {
public:
...
static std::tr1::shared_ptr<Person> // 頥 tr1::shared_ptr
create(const std::string& name, //    Person,
const Date& birthday, // 樠஢ 묨
const Address& addr); // ࠬࠬ: . ࠢ 18,
... // 祬 頥
}; // tr1::shared_ptr

 ᯮ ⠪:

std::string name;
Date datefBirth;
Address address;
...
// ᮧ ꥪ, ন騩 䥩 Person
std::tr1::shared_ptr<Person> pp(Person::create(name, dateOfBrth, address));
...
std::cout << pp->name() // ᯮ짮 ꥪ १
<< "த " // 䥩 Person
<< pp->birthDate()
<< " ⥯    "
<< pp->address();
... // ꥪ ⮬᪨
// 㤠,  pp 室
//  ⥪ - . ࠢ 13

㬥, -   ।  , ন騥 䥩 ⠪ 䥩᭮ ,  맢 ॠ .   ந室 < 㫨ᠬ>,  䠩, ᮤঠ ॠ 㠫 ஢. ਬ, 䥩  Person    ந  RealPerson, ।⠢騩 ॠ 㭠᫥ 㠫 㭪権:

class RealPerson public Person {
public:
RealPerson(const std::string& name, const Date& birthday,
const Address& addr)
: theName(name), theBirthDate(birthday), theAddress(addr)
{}
virtual ~RealPerson() {}
std::string name() const; // ॠ  㭪権
std::string birthDate() const; //  ,  
std::string address() const; //  ।⠢
private:
std::string theName;
Date theBirthDaye;
Address theAddress;
};

  RealPerson, 祭   Person::create:

std::tr1::shared_ptr<Person> create( const std::string& name,
const Date& birthday,
const Address& addr)
{
return std::tr1::shared_ptr<Person>(new RealPerson(name, birthday, addr));
}

 ॠ᪠ ॠ Person::create  ᮧ ࠧ ⨯ ꥪ⮢ ᮢ-᫥,  ᨬ, ਬ,  ⥫ ࠬ஢ 㭪樨, , ⠭  䠩   , ६ 㦥  . .
RealPerson      ࠭ 堭 ॠ樨 䥩 ᮢ:  ᫥ ᯥ䨪樨 ᢮ 䥩  䥩᭮  Person,  ⥬ ॠ 㭪樨 ⮣ 䥩. ன ᯮᮡ ॠ樨 䥩᭮  । ᯮ짮 ⢥ ᫥ (. ࠢ 40).
⠪, -ਯ  䥩  ⤥ 䥩  ॠ樨, 㬥 ⥬ ᠬ ᨬ  䠩  ⠯ 樨. ,  㢥७,   ਬ砭  ⮬: < ᪮쪮    䮪?>   筠   ணࠬ஢: ஥ 㬥襭 ᪮ 믮 ணࠬ  ⥫ 室    ꥪ.
ਬ⥫쭮  ᠬ-ਯࠬ 㭪樨-童  ᯮ짮 㪠⥫  ॠ (pImpl), ⮡    ᠬ ꥪ.   饭    ஢ ᢥ 樨. ஬ ⮣,  ꥬ , 室  ࠭  ꥪ, 㦭  ࠧ 㪠⥫.  , 㪠⥫  ॠ   樠஢ (  -ਯ), ⮡  㪠뢠  ᪨ । ꥪ ॠ樨; ᫥⥫쭮,    ᥡ    室, ᮯ஢騥 ᪮ 뤥   ᫥饥  ᢮,  ⠪   ᪫祭 bad_alloc (- ⪠ ).
 䥩 ᮢ  맮 㭪樨 㤥 㠫, ⮬ 直 ࠧ    ᢥ 室 (. ࠢ 7). ஬ ⮣, , ந  䥩᭮ ,  ᮤঠ 㪠⥫  ⠡ 㠫 㭪権 ( ᭮ . ࠢ 7).  㪠⥫  㢥 ꥬ , 室  ࠭ ꥪ,  ᨬ  ⮣,   䥩  ⢥ 筨 㠫 㭪権  ꥪ.
 ,  -ਯ,  䥩     룮  ᯮ짮 ஥ 㭪権.  ࠢ 30 ᭥, 祬 ⥫ ⥭樠쭮 ࠨ 㭪権     䠩,  -ਯ  䥩  ᯥ樠쭮 ।祭  ⮣, ⮡  ⠪ ⠫ ॠ樨,  ⥫ 㭪権.
 뫮  쥧 訡 ⪠뢠  ᮢ-ਯ஢  䥩 ᮢ ⮫쪮 ⮬,   ᯮ짮 易  ⥫묨 室.   ᠬ  ᪠   㠫 㭪,     ⪠뢠   ਬ. ( ⨢ 砥  ⠥   .) ᬮ  ᯮ짮 । ਥ   樨  ணࠬ. ਬ -ਯ  䥩    ࠧࠡ⪨, ⮡ 㬥 ﭨ   ॠ樨  짮⥫. ᫨   ,  ࠧ稥  ᪮ / ࠧ ணࠬ ⮫쪮 ⢥,    襭 䥪⨢  ࠢ뢠 㢥祭 ᨬ  ᠬ,   筮 ⠤ ॠ樨   묨 ᠬ.

 ᫥ 
 ᭮  㬥襭 ᨬ⥩  ⠯ 樨 ⮨  ⮬, ⮡  ᨬ  । ᨬ  .     ᭮  室: ᮢ-ਯ஢  䥩 ᮢ.
  䠩 ⥪  ⢮   ଠ:   ᮤঠ饩 ⮫쪮 .  ࠢ ᨬ  ⮣,   蠡  .




 6
᫥  ꥪ⭮-ਥ஢ ஥஢


ꥪ⭮-ਥ஢ ணࠬ஢ ()   20 , ⮬, ⭮,   ஥ ।⠢  ᫥, ந   㠫 㭪.  ᫨  ணࠬ஢ ⮫쪮  C, 祣        .
     C++, ᪮॥ ᥣ, ᪮쪮 ⫨砥  ⮣,  祬  ਢ몫. ᫥     ⢥,  ⤥  ᫥    (public), 饭 (protected)   (private).  ⠪   㠫  㠫.  㭪権-童 ⮦  ਠ. 㠫? 㠫?  㠫?   ⢨  㣨 ।⢠ 몠.  ᮮ⭮ ࠬ  㬮砭  㠫묨 㭪ﬨ?   ᫥  ࠢ ࠧ襭   C++?    ᪠   ⮤ ஥஢? ᫨     㥬,   㠫 㭪樨 訬 ᯮᮡ ⨦ ⮣?
 ᥬ ⮬    饩 .  ,   ᠬ  ⮨  ⥬  묨 ﬨ C++:    ࠦ,  ᯮ  . ਬ, ⮥ ᫥  ⭮襭 <>,  ᫨  ⠥ ਤ  -  ᥬ⨪,  ⮫  ஡. 筮, 㠫쭠 㭪 砥 <  㭠᫥ 䥩>,   ६  㠫쭠 㭪 砥 < ᫥  䥩,  ॠ>. ᫨   ࠧ稩  ⨬ ᫠,  ⭮⥩  .
   ⨭ 祭 ࠧ । C++,  㦨,      .  ⮣ ࠦ  宦 ⫨稩  몠  ⠭ ।⢮ ࠦ ⮣,    ᪠  ᢮ ணࠬ ⥬.  ,     ⢨⥫쭮   , 㦥  ⠢ 讣 㤠 ॢ ᢮ ᫨ ⮣  C++.



ࠢ 32: ᯮ ⮥ ᫥  ஢ ⭮襭 <>

  (William Dement)  ᢮  <-  ⢮,  ⠫ > (W. H. Freeman and Company, 1974) ᪠뢠  ⮬,   ⠫   㤥⮢    ᢮ . ⢥ত, ਫ  ᢮ 㯯,  । ⠭᪨ 誮쭨   ப ਨ  ,  ⢠  ⨭ ந諠  1066 .  ᫨ 祭  祣     ਨ, ન , 1066  ⠥   .  ⠫  ⥫ ᪮쪮 ᭮ ,  ⭮   ⨭,  ᭮⢮୮ 뢠 ᮭ.  ਧ뢠 ᢮ 㤥⮢   祢 䠪⮢,  ᫨ 㤥 ,  㦤  殮 ,   祭 ᥬ 頫  ᪮쪨 㭤⠫ .
᫥  ⥫쭮   : <,  䠪  ,  㦤  ,     >. ஢ ࠡ,   襫.   㯮㫨 1066 .
   ९⮬  ஢,  ᠬ  ࠢ  ꥪ⭮-ਥ஢ ணࠬ஢  C++  ⠪: ⮥ ᫥ 砥 <>. म  .
᫨    D (derived - <ந>)  ᫥  B (<base> - <>),  ⥬ ᠬ ᮮ頥  C++ (   , 騬  ),   ꥪ ⨯ D  ⠪ ꥪ⮬ ⨯ B,   .  ,  B ।⠢ ᮡ   楯, 祬 D,  D -   楯, 祬 B.  ⢥ত,  ,    ᯮ짮 ꥪ B,  ᯮ짮 ⠪ ꥪ D, ⮬  D  ꥪ⮬ ⨯ B.  㣮 ஭, ᫨  㦥 ꥪ ⨯ D,  ꥪ B  , ᪮  D < ࠧ> B,   .
 樨 ⮣ ᫥ ਤন C++. ᬮਬ ᫥騩 ਬ:

class Person {...};
class Student: public Person {...};

ࠢ    ᪠뢠 ,   㤥 - 祫,    祫 - 㤥.  ⠪  ࠧ㬥  .  ,  类 ⢥ত, ࠢ  祫 - ਬ,      ஦,- ࠢ   㤥,   ,  ୮  㤥 - ਬ,     - । ,- ୮  祫  饬 砥.
ਬ⥫쭮  C++  룫廊 ᫥騬 ࠧ:  㭪,  ਭ 㬥 ⨯ Person ( 㪠⥫  Person,  뫪  Person), ਬ ꥪ ⨯ Student ( 㪠⥫  Student,  뫪  Student):

void eat(const Person& p); //    
void study(const Student& s); // ⮫쪮 㤥 
Person p; // p - 祫
Student s; // s - 㤥
eat(p); // ࠢ쭮, p  祫
eat(s); // ࠢ쭮, s -  㤥,
//  㤥 ⠪  祫
study(s); // ࠢ쭮
study(p); // 訡! p -  㤥

 ᪠ ୮ ⮫쪮  ⮣ ᫥. C++ 㤥  ᥡ ⠪,  ᠭ , ⮫쪮  砥, ᫨ Student  ᫥ Person. ⮥ ᫥ 砥  ᮢᥬ  (. ࠢ 39),   饭 ᫥ ᪮짠    ᥩ .
 ⮦⢠ ⮣ ᫥   <>  筮 祢,     . ᬮਬ ᫥騩 ਬ:  -  ,  㬥 . ᫨    ⠥ ࠧ   C++,    :

class Bird {
public:
virtual void fly(); //  㬥 
...
};
class Penguin: public Bird { //  - 
...
};

  ⮫㫨  㤭. ⢥ত,    , ,  ⭮, ୮.  祬  ?
  砥   筮 ࠧ୮ 몠.   ਬ,   㬥 ,     ,    ,  ⮫쪮 ,  筮   ⠪ ᯮᮡ. ᫨   롨ࠫ 㫨஢ 筥,  ᯮ ,   ᪮쪮   ,  諨  ᫥饩 娨,  ⥫쭮   ॠ쭮:

class Bird {
... // 㭪 fly  
};
class FlyingBird: public Bird {
public:
virtual void fly();
...
};
class Penguin: public Bird {
... // 㭪 fly  
};

  ࠧ 筥 ࠦ ॠ쭮, 祬 ࢮ砫쭠.
  ⥯    祭  <쨬 >, ⮬    ਫ     室  ࠧ稥  騬  騬 栬. , ᫨  ਫ  ᭮      ﬨ    ࠦ ᯮᮡ ୠ ,  ᮩ  室 .  , ᮮ⢥,   ⢥ত ⮣,    쭮 ஥,  室     ணࠬ ⥬. 롮 ஥   ⮣,  ⥬   -  ᥩ, ⠪   饬. ᫨  ਫ   易  ⠬   ।,   㤥 易    쭥襬,     ਭ   ࠧ稩  騬  騬 栬.  ᠬ     ஢ ⠪ ࠧ稩, ⮬     ,   ⠥ ஢.  㣠 誮,  ⭮  ᬠਢ ஡.  । ८।   㭪 fly() ⠪, ⮡  ६ ᯮ  頫 訡:

void error(const std::string& msg); // ।  㣮 
class Penguin: public Bird {
public:
virtual void fly() {error("⪠ ⠢  !");}
...
};

 ,        ᮢᥬ ,    .   ਬ: <   >,   ᮮ頥: <  ,    ஭ 뫮  訡  >.
 祬 ࠧ?  ६ 㦥 訡. ⢥ত <   >   ঠ  ஢ ,  ᮮ⢥⢨ ⢥ত <⪠  訡筠  > ॠ쭮     㦥  ६ 믮 ணࠬ.
⮡  ࠭祭 <    -  窠>, ᫥ 㡥,   ꥪ⮢ Penguin 㭪 fly()  ।:

class Bird {
... // 㭪 fly  
};
class Penguin: public Bird {
... // 㭪 fly  
};

᫨ ⥯  ஡ ⠢  ,  ᤥ  룮  襭 ࠢ:

Penguin p;
p.fly(); // 訡!

 ᨫ쭮 ⫨砥  , ஥ 砥, ᫨ ਬ 室, 騩 訡 ६ ᯮ.   ⠪ 砥  祣   ᪠  맮 p.fly().  ࠢ 18 ,  訩 䥩 ।頥  ୮ , ⮬   ஥,  ⢥࣠ ⪨    ६ 樨,    ६ ᯮ.
,  ,   ⠥ 樨 ୨⮫,      ᢮   ୮ ਨ,  ⠪ ?  ⢥  ᫥騩 ⮩ :    Square ()  ᫥  Rectangle (אַ㣮쭨)?
<筮!- ᪠ .-  ,   -  אַ㣮쭨,  ⭮ ⢥ত  饬 砥 ୮>.  , ࠢ쭮,  ࠩ ,  誮.    蠥  ᫮ 誮.

class Rectangle {
public:
virtual void setHeight(int newHeight);
virtual void setWidth(int newWidth);
virtual int height() const; //  ⥪騥 祭
virtual int width() const;
...
};
void makeBigger(Rectangle& r) // 㭪 㢥稢 頤 r
{
int oldHeight = r.height();
r.setWidth(r.width() + 10); // 㢥 ਭ r  10
assert(r.height() == oldHeight); // 㡥,   r
} //  

᭮,  ⢥ত assert    . 㭪 make-Bigger  ⮫쪮 ਭ r.  ⠥ ﭭ.
 ᬮਬ ,  ।⢮ ⮣ ᫥  ᬠਢ    砩 אַ㣮쭨:

class Square: public Rectangle {:};
Square s;
...
assert(s.width() == s.height()); //   ࠢ 
//  ⮢
makeBigger(s); // - ᫥, s 
// Rectangle, ⮬  
// 㢥  頤
assert(s.width() == s.height()); // -०   ࠢ
//   ⮢

   ।饬 ਬ,  ஥ ⢥ত ⠪     襭.  ।, ਭ  ࠢ  .
 ⥯ ।  ⠥ ஡.  ਬ ᫥騥 ⢥ত?
 । 맮 makeBigger  s ࠢ ਭ.
  makeBigger ਭ s ,   - .
 ᫥   makeBigger  s ᭮ ࠢ ਭ (⬥⨬,  s ।  뫪, ⮬ makeBigger   s,    ).
  ?
   㤨⥫  ⮣ ᫥,  , ਮ⥭   㣨  ,  ⥬⨪,  뢠 娬 魨. ᭮ 㤭   砥 砥  ⮬,   ⢥ত, ࠢ  אַ㣮쭨 ( ਭ    ᨬ  ),  믮   ( ਭ     ).  ⮥ ᫥ ।,  ,  ਬ  ꥪ⠬  ,- ! - ⠪ ਬ   ꥪ⠬ ந ᮢ.  樨  אַ㣮쭨  ⠬ ( ⠪   ,  ⢠  ᯨ᪨  ࠢ 38), ⢥ত ⮣ ᫮  믮, ⮬ ᯮ짮 ⮣ ᫥  ஢  ४⭮. , 筮, ⮣  頥, ,   ⮫쪮  ,   ࠭⨩,  ⠪  㤥  ᥡ  ࠧ.  ணࠬ   ⭮ (    㣨): ᫨  ,     ,   㤥 ࠡ.
   ⮨ ᯮ,  ਮ⥭     ࠧࠡ⪨ ணࠬ ᯥ祭   ᯮ  室  ꥪ⭮-ਥ஢ ணࠬ஢.    -० 㠫,  ⥯,     ᢮ ᥭ ᫥,  ਤ  ᢮   , 騬 ᮧ ਫ  ᯮ짮 ᫥.  ६  ᫥ Penguin  Bird  Square  Rectangle 㤥   ⮫  ,  㭪 ꥬ  ᪮쪮 ࠭.  襭   ࠢ,   ⭮.
⭮襭 <> -  ⢥,   ᠬ.  㣨, 筮 ࠭ ⭮襭 -  <ᮤন>  <ॠ ।⢮>.  ᬠਢ  ࠢ 38  39. 祭   ஥஢  C++  ஥  ਢ   - ⮣,   裡  ⭮襭 <>. ⮬    㢥७,   ࠧ  ⨬ ⭮襭ﬨ  ,  ࠧ   ᥣ ஢  C++.

 ᫥ 
 ⮥ ᫥ 砥 <>. ,  ਬ   ,   ਬ ⠪  ந  , ⮬   ꥪ ந   ⠪ ꥪ⮬  .



ࠢ 33:  뢠 㭠᫥ 

ᯨ  ࠧ  .  ᠫ: <   ⥡?  孥 ஧,  ஧  ,  >.   ᠫ : < ஥   ,  ᤥ  :> ࠢ쭮.   ⠢     㭠᫥   C++.
-  ⥬ ⭮  ⮫쪮  ᫥, ᪮쪮   .   ,   ⠪ :

int x; // 쭠 ६
void someFunc()
{
double x; // 쭠 ६
std::cin >> x; //   祭 쭮 ६ x
}

 x  । 뢠 ⭮  쭮,    쭮 ६, ⮬       뢠 (<⥭>)   譨 ⥩.   ।⠢   㠫쭮:
  砥  x  㭪樨 someFunc,  ᬮ, ।  -  ⠪   쭮  . ᫨ ,  ꥬ騥    ᬠਢ.   砥  x  㭪樨 someFunc ਭ ६ ⨯ double,  쭠 ६  ⥬   x  ⨯ int,   ⢥. ࠢ ᮪   C++ ।祭  -⢥ 楫: 뢠 . ⭮     ꥪ⠬   ࠧ ⨯,   祭.  襬 ਬ ६ x ⨯ double 뢠 ६ x ⨯ int.
୥  ᫥.  ,   室  㭪樨-童 ந   뫠  -    (ਬ, 㭪-童, typedef  童 ),  ᬮ  ,    뫠, ⮬  ந  ᫥ ᢮⢠,    . 堭 ᭮  ⮬,    ந       . ਬ:
class Base {
private:
int x; 
public:
virtual void mf1() = 0;
virtual void mf2();
void mf3();
...
};
class Derived: public Base {
public:
virtual void mf1()
void mf4();
...
};

 ⮬ ਬ   , ⠪   ,   童 , ⠪  㭪権-童.   㭪権-童 -  㠫쭠, 㣠 -  㠫쭠,   - 㠫쭠.    ⮬,   ਬ   ,    祬- 㣮.      ਬ   ⨯, ਬ ᫥,  ᮢ  typedef.   ⥪   ,    .    - ⢥.  ਬ ᯮ 筮 ᫥, , ,  ந室  筮 ᫥,  㤥 ࠧ   ⮬,  C++  ᥡ  ⢥ ᫥.
।,  㭪-童 mf4  ந  ॠ ਬ୮ ⠪:

void Derived::mf4()
{
...
mf2();
...
}

    mf2,   ,    뫠.  ⮣  ࠧ   ந   mf2. 砫    쭮   (   mf4),  ⠬ ⠪  .  ᬠਢ ꥬ  ,      Derived.   ⠪  , ⮬  室  ᫥饩  , ன   .  室 ⠬    mf2, ᫥ 祣  蠥. ᫨  mf2  뫮    Base,   த  ᭠砫  ࠭⢥ , ᮤঠ饬 Base, ᫨ ⠪ , , ,  쭮  .
  ᠭ ࠢ쭮,   뢠  ᫮  ᪠   C++.  楫, ,   ⮬, ⮡ 㧭  ᪥  ⮫쪮, ⮡ ᠬ⥫쭮  . 筮 㤥, ᫨  㬥   ਧ,   ⮣  ଠ樨  墠.
 ୥  ।饬 ਬ,    ࠧ ॣ㧨 㭪樨 mf1  mf3,  ⠪   mf3   Derived.    ࠢ 36, ॣ㧪 mf3  ந  Derived ( ᫥ 㠫쭠 㭪) ᠬ  ᥡ ⥫쭠,  ⮡  ࠧ   , ஥   .
class Base {
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf1(int);
virtual void mf2();
void mf3();
void mf3(double);
...
};
class Derived: public Base {
public:
virtual void mf1()
void mf3();
void mf4();
...
};

  ਢ  , ஥ 㤨  ணࠬ C++,  ⮫襣  . ᭮    ࠢ ᮪  㤠  , ⮬  㭪樨   mf1  mf3      묨 㭪ﬨ  ந .  窨 ७ ᪠ , Base::mf1  Base::mf3   ᫥ ᮬ Derived!

Derived d;
int x;
...
d.mf1(); // ࠢ쭮, 뢠 Derived::mf1
d.mf1(x); // 訡! Derived::mf1 뢠 Base::mf1
d.mf2(); // ࠢ쭮, 뢠 Base::mf2
d.mf3(); // ࠢ쭮, 뢠 Derived::mf3
d.mf3(x); // 訡! Derived::mf3 뢠 Base::mf3

 ,  ᠥ   砥,  㭪樨    ந  ਭ ࠬ ࠧ ⨯, ᨬ  ⮣,     㠫  㠫 㭪.  筮 ⠪ ,   襬 ࢮ ਬ double x  㭪樨 someFunc 뢠 int x  쭮 ⥪, ⠪   㭪 mf3   Derived 뢠 㭪 mf3   Base,   㣮 ⨯.
᭮ ⠪   ⮬,     ﭭ 㭠᫥ ॣ㦥 㭪樨   , ᯮ    娨 ᫥, ⠭  ⥪  ઠ ਫ.  ᮦ, 筮   㭠᫥ ॣ㦥 㭪樨. ᪨ ᫨  ᯮ ⮥ ᫥   ᫥ ॣ㦥 㭪権,  蠥 ᥬ⨪ ⭮襭 <>    ந ᠬ, ஥  ࠢ 32 ஢襭 㭤⠫ ਭ樯 ⮣ ᫥.     砩,    ᥣ   ਭ⮥  C++  㬮砭 ࠢ ᮪ .
  ᤥ   using-:
class Base {
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf1(int);
virtual void mf2();
void mf3();
void mf3(double);
...
};
class Derived: public Base {
public:
using Base::mf1; // ᯥ   () 
using Base::mf3; // mf1  mf3   Base   Derived
virtual void mf1()
void mf3();
void mf4();
...
};

 ᫥ 㤥 ࠡ,   .

Derived d;
int x;
...
d.mf1(); // -० ࠢ쭮, 뢠 Derived::mf1
d.mf1(x); // ⥯ ࠢ쭮, 뢠 Base::mf1
d.mf2(); // -० ࠢ쭮, 뢠 Base::mf2
d.mf3(); // -० ࠢ쭮, 뢠 Derived::mf3
d.mf3(x); // ⥯ ࠢ쭮, 뢠 Base::mf3

 砥,  ᫨  ᫥    ॣ㦥묨 㭪ﬨ   ८। ⮫쪮   ,    using-   ,   㤥 .
 ।⠢ ᥡ ,     ᫥  㭪樨   ᮢ.  ⮬ ᫥ ⠪    ந室, ⠪   ⨢  ⭮襭 <>   ᮬ  ந  .  祬 using- 室  ᥪ樨 public  ந ; ,     ,  ⠢ 묨    㭠᫥  .   ⮬ ᫥ (. ࠢ 39) ⠪   ᫥. ਬ, ।,   Derived  ᫥  Base,  ⢥  mfl,  Derived  㭠᫥,-  ,   ਭ ࠬ஢. Using-  ⮬ 砥  , ᪮   묨  ந   㭠᫥ 㭪樨   .  ॡ 㣠 孨 -  ७ࠢ 㭪:

class Base {
public:
virtual void mf1() = 0;
virtual void mf1(int);
... //  ࠭
};
class Derived: private Base {
public:
virtual void mf1() // ७ࠢ 㭪
{ Base::mf1();} //  ஥ (. ࠢ 30)
...
};
...
Derived d;
Int x;
d.mf1(); // ࠢ쭮, 뢠 Derived::mf1
d.mf1(x); // 訡! Base::mf1 

㣮 ਬ ஥ ७ࠢ 㭪権 -  䥪   ॢ ,   ন using-   㭠᫥     ந .
 ,   ᪠  ᫥  ᮪⨨ . 祬,  ᫥ ⠥  蠡,  ᮢᥬ 㣮 ਠ ஡ <᮪ 㭠᫥ >.  ஡, 騥 蠡, . ࠢ 43.

 ᫥ 
   ந  뢠    ᮢ.  ⮬ ᫥  ᥣ ⥫쭮.
 ⮡ ᤥ   묨, ᯮ using-  ७ࠢ騥 㭪樨.



ࠢ 34: 砩 ᫥ 䥩  ᫥ ॠ樨

譥   ⮣ ᫥  襬 ᬮ७ 뢠 饩   ࠧ ⥩: ᫥ 䥩 㭪権  ᫥  ॠ樨. 稥  ⨬   ᫥ ᮮ⢥ ࠧ  ﬨ  ।ﬨ 㭪権, 㦤襬    ⮩ .
 ࠧࠡ⪥ ᮢ  ॡ, ⮡ ந  ᫥ ⮫쪮 䥩 () 㭪権-童.  㣨  室, ⮡ ந  ᫥  䥩,  ॠ 㭪権,   ८। 㭠᫥ ॠ.      ᯮ짮 ᫥ 䥩  ॠ樨,    - ८।.
⮡  ⢮ ࠧ  ⨬ ਠ⠬, ᬮਬ  ᮢ  ।⠢ ᪨ 䨣  ᪮ ਫ:

class Shape {
public:
virtual void draw() const = 0;
virtual void error(const std::string& msg);
int objectID() const;
...
};
class Rectangle: public Shape {:};
class Ellipse: public Shape {:};

Shape -  ࠪ ; ⠪    㠫쭠 㭪 draw.  १ 짮⥫   ᮧ ꥪ  Shape,   ᮢ, ந  . ᬮ  , Shape 뢠 ᨫ쭮 ﭨ    ᫥騥    ᫥饩 稭:
 䥩 㭪権-童 ᫥ ᥣ.    ࠢ 32, ⮥ ᫥ 砥 <>, ⮬ ,  ୮   , ⠪ ୮   ந  . ⮬ ᫨ 㭪 ਬ  ,  ⠥ ਬ   ᮢ.
  Shape   㭪樨. ࢠ, draw, 뢮 ⥪騩 ꥪ  ᯫ, ࠧ㬥  㬮砭. , error, 뢠 㭪ﬨ-童, ᫨ 室 ᮮ  訡. , objectID, 頥 㭨 楫᫥ 䨪 ⥪饣 ꥪ.    㭪権  -ࠧ: draw -   㠫쭠; error -   㠫쭠; objectID -  㠫쭠 㭪.  ࠪ᪨ ᫥⢨  ࠧ稩?
ᬮਬ   㠫 㭪 draw:

class Shape {
public:
virtual void draw() const = 0;
...
};

   ࠪ⨪  㠫 㭪権 -        ⭮ ᫥饬  ,   ࠪ⭮   筮  ।. ⠢   ᢮⢠,   ਤ   ᫥饣 ⥫⢠:
    㠫쭮 㭪樨 ⮨  ⮬, ⮡ ந  ᫥ ⮫쪮  䥩.
    ⭮  㭪樨 Shape::draw, ᪮  ࠧ㬭 ॡ  ᥬ ꥪ⠬  Shape 砥  ⮬,     ⮡ࠦ  ᯫ,  Shape   ᯥ ࠧ㬭 ॠ樨 ⮩ 㭪樨  㬮砭.  ᮢ  祭 ᨫ쭮 ⫨砥  ⬠ ᮢ אַ㣮쭨.  Shape::draw  ஢  ᫥饥 ᮮ饭 ࠧࠡ稪  ᮢ: <  ᯥ 稥 㭪樨 draw,      襣 ।⠢,    ᮡࠥ ᤥ>.
 稬,  ।  㠫쭮 㭪樨 . 묨 ᫮,   ।⠢ ॠ  Shape::draw,  ++ 㤥  ஢,  ⢥ ᯮᮡ 맢 - ஢  㭪樨  :

Shape *ps = new Shape; // 訡! Shape - ࠪ
Shape *ps1 = new Rectangle; // ࠢ쭮
ps1->draw(); // 맮 Rectangle::draw
Shape *ps2 = new Ellipse; // ࠢ쭮
Ps2->draw(); // 맮 Ellipse::draw
ps1->Shape::draw(); // 맮 Shape::draw
ps2->Shape::draw(); // 맮 Shape::draw

஬ ᯥ⨢  । ⥫ﬨ-ணࠬ⠬  ६ ਭ,  ⮩ ᮡ     - 業.   ,   㢨 ,  ।  㠫쭮 㭪樨   ᯮ짮  ⢥ 堭 ᯥ祭  ᭮ ॠ樨  㬮砭  㠫 㭪権.
  묨 㠫묨 㭪ﬨ ᪮쪮 ⫨砥  樨   㠫묨 㭪ﬨ.  ᥣ, ந  ᫥ 䥩 㭪樨,   㠫 㭪樨 ࠤ樮 ᯥ稢 ॠ,    ८।. ᫨    㬠  ⨬,  , :
   筮 㠫쭮 㭪樨 - ᫥  ந   䥩, ⠪   ॠ  㬮砭.
ᬮਬ 㭪 Shape::error:

class Shape {
public:
virtual void error(const std::string& msg);
...
};

䥩   ⮬,     ন 㭪,  室 뢠   訡,     ࠡ뢠 訡  室騬  ᥡ ࠧ. ᫨   । ந ᯥ樠 ⢨,      ࠡ 訡  㬮砭,  ।⠢  Shape.    Shape::error  ࠧࠡ稪 ந ᮢ: <  ন 㭪 error,  ᫨    ᢮ ᮡ⢥,   뢠  ᯮ짮   㬮砭   Shape>.
뢠,    ᭮ ᯮ짮  㠫 㭪樨,  ᯥ稢  䥩 㭪樨, ⠪   ॠ  㬮砭.  ⮣ ⮡ , 祬  ⠪ ⭮, ᬮਬ  ᠬ⮢   XYZ Airlines. XYZ ᯮ ᠬ⠬ ⮫쪮  ⨯:  A   B,    .  裡  ⨬ ࠧࠡ稪 XYZ ஥ ⠪ :

class Airport {:}; // ।⠢ ய
class Airplane {
public:
virtual void fly(const Airport& destination);
...
};
void Airplane::fly(const Airport& destination)
{
  㬮砭, 뢠騩  ᠬ

  㭪 祭 - destination
}
class ModelA: public Airplane {...};
class ModelB: public Airplane {...};

⮡ ࠧ  䠪,   ᠬ  ন 㭪 fly,   ⮣ ⮡ ᢨ⥫⢮,   ࠧ ,  ਭ樯,  ॡ ࠧ ॠ樨 fly, 㭪 Airplane::fly  㠫쭮.  ⮬   ᠭ 筮    ModelA  ModelB  ⢥ ⠭⭮  ᯮ ⥫ 㭪樨 Airplane::fly,  ᫥  ModelA, ⠪  ModelB.
 ᪨ ਬ ꥪ⭮-ਥ஢ ஥஢.    饥 ᢮⢮ (ᯮᮡ ॠ樨 fly), ⮬  ॠ     ᫥  ᠬ.  ⮬ ஥  ࠧ 뤥 騥 ᢮⢠,    㡫஢,  ஢  ୨権  頥  ᯫ - 묨 ᫮, ᯥ稢 ,   ⠪ 業 ꥪ⭮-ਥ஢ 孮. ணࠬ  XYZ Airlines  ᮡ न.
 ⥯ ।,   XYZ   ,   蠥 ਮ  ᠬ  C.   ⫨砥   A  B,  ⭮, ⥬,  ⠥ -㣮.
ணࠬ  XYZ     ModelC,   ᯥ誥 뢠 ८। 㭪 fly:

class ModelB: public Airplane {
... // 㭪 fly  
};

 ᢮  ⮬   - த ⮣:

Airport PDX(...); // PDX - ய   
Airplane *pa = new ModelC;
...
pa->fly(PDX); // 뢠 Airplane::fly!

ॢ :  ⪠ ࠢ   ꥪ ModelC,  ᫨   ਭ   ᮢ ModelA  ModelB.  ࠧ ⢨     ਥ ᠦࠬ.
஡  砥   ⮬,  Airplane::fly  ᥡ । ࠧ  㬮砭,   ⮬,  ⠪ ᫥ ᪠  ਬ ⮩ 㭪樨  ModelC.  ,   । ᠬ   㬮砭,   ।⠢ , ᫨  ᠬ  ⮬  .  ⮨  ⮬, ⮡ ࠧ 䥩 㠫쭮 㭪樨   ॠ  㬮砭.    ᯮᮡ  ⮣:

class Airplane {
public:
virtual void fly(const Airport& destination) = 0;
...
protected:
void defaultFly(const Airport& destination);
};
void Airplane::defaultFly(const Airport& destination)
{
  㬮砭, 뢠騩  ᠬ   㭪 祭
}

 ,  㭪 Airplane::fly ८ࠧ   㠫.  ।⠢ 䥩  .   Airplane   ॠ  㬮砭,  ⥯  ।⠢  ଥ ᨬ 㭪樨 defaultFly. ,  ModelA  ModelB,   ᯮ짮   㬮砭,  믮 ஥ 맮 defaultFly  fly (. ⠪ ࠢ 30  ⢨ ࠨ  㠫 㭪権):

class ModelA: public Airplane {
public:
virtual void fly(const Airport& destination)
{ defaultFly(destination};}
...
};
class ModelB: public Airplane {
public:
virtual void fly(const Airport& destination)
{ defaultFly(destination};}
...
};

   ModelC  砩 㭠᫥ ४ ॠ fly ᪫祭, ᪮  㠫쭠 㭪  Airplane 㦤 ModelC ᮧ ᢮ ᮡ⢥  fly.

class ModelC: public Airplane {
public:
virtual void fly(const Airport& destination)
...
};
void ModelC::fly(const Airport& destination)
{
, 뢠騩  ᠬ ModelC   㭪 祭
}

 奬  ᯥ稢 <  ࠪ> (ணࠬ    ᮧ ᥡ ஡ ஢/⠢),    , 祬 室.   ᠥ 㭪樨 Airplane::defaultFly,    饭, ᪮ ⢨⥫쭮  ⠫ ॠ樨  Airplane  ந  . ᠦ ⥯  ᯮ ⮫쪮  ⮬, ⮡ 㫥,    ⮬,  ந室 .
 ⠪ ,  Airplane::defaultFly   㠫쭠 㭪.  易  ⥬,       ८। - ⥫⢮, ஬ 饭 ࠢ 36. ᫨  defaultFly 뫠 㠫쭮, ।  ᭮ ⠫    ᠬ ஡: , ᫨    ८। defaultFly  ࠧ?
 ᪠뢠 ࠦ ⨢  ࠧ 㭪権  ᯥ稢騥 䥩  ॠ  㬮砭, ⠪, ਬ,  fly  defaultFly. ० ᥣ, ⬥ ⨢ ⮩ ,   ࠭⢮    ﬨ 㭪権.    ᮣ  ⥬,  䥩  ॠ  㬮砭   ࠧ.  ࠧ 饥 ⨢稥?  ⮣ ᯮ  䠪,  ந   ८।  㠫 㭪樨   室 ।⠢ ᢮ ᮡ⢥ ॠ樨.    뫮  ᯮ짮  ।  㠫 㭪権  娨 Airplane:

class Airplane {
public:
virtual void fly(const Airport& destination) = 0;
...
};
void Airplane::fly(const Airport& destination) // ॠ 
{ // 㠫쭮 㭪樨
  㬮砭, 뢠騩 
ᠬ   㭪 祭
}
class ModelA: pubic Airplane {
public:
virtual void fly(const Airport& destination)
{ Airplane::fly(destination);}
...
};
class ModelB: pubic Airplane {
public:
virtual void fly(const Airport& destination)
{ Airplane::fly(destination);}
...
};
class ModelC: pubic Airplane {
public:
virtual void fly(const Airport& destination);
...
};
void ModelC::fly(const Airport& destination)
{
, 뢠騩  ᠬ ModelC   㭪 祭
}

 ࠪ᪨ ⠪  室,   ०,  ᪫祭 ⮣,  ⥫  㠫쭮 㭪樨 Airplane::fly  ᮡ ᨬ 㭪 Airplane::defaultFly.  , fly ࠧ   ᭮ ⠢騥.   䥩 (   ᯮ짮  ந ),  ।    㬮砭 (஥  ᯮ짮 ந ᮬ,  ⮫쪮   ॡ). , ந ᫨ﭨ fly  defaultFly,  塞     㭪権 ࠧ ஢ 㯠: ,    饭 (㭪 defaultFly), ⠭  (⮬  ⥯  室  fly).
 , 諠 । 㠫쭮 㭪樨  Shape - objectID:

class Shape {
public:
int objectID() const;
...
};

 㭪-童  㠫쭮,  ।,   㤥  ᥡ   ந .  ⢨⥫쭮 㠫 㭪樨-童 ࠦ ਠ ⭮⥫쭮 ᯥ樠樨, ᪮ । , ஥  ࠭ ᨬ  ⮣,  ᯥ樠 ந . ࠢ ᫥饥:
   㠫쭮 㭪樨 - ⠢ ந  ᫥   䥩, ⠪  易⥫ ॠ.
  ।⠢ ᥡ  Shape::objectID  ⢥ত: < ꥪ Shape  㭪,   䨪 ꥪ,   䨪 ᥣ    ⥬  ᯮᮡ.  ᯮᮡ  । 㭪樨 Shape::objectID,   ந     >. ᪮ 㠫쭠 㭪 । ਠ ⭮⥫쭮 ᯥ樠樨,   ᫥ ८।  ந  (. ࠢ 36).
    㠫,  㠫  㠫 㭪権  筮 㪠, ,  襬 ,  ᫥ ந : ⮫쪮 䥩, 䥩  ॠ  㬮砭  䥩  易⥫ ॠ ᮮ⢥⢥. ᪮  ⨯   ਭ樯쭮 ࠧ , ᫥ ⥫쭮 室  롮 室饣 ਠ  ﭨ 㭪樨-童.  ⮬     訡,  ᥣ ᮢ蠥 묨 ஥஢騪 ᮢ.
ࢠ 訡 -   㭪権 㠫묨.   ⠢   ஢  ந ;  ⮬  ᥣ ஡ 뢠 㠫  (. ࠢ 7). 筮,  祣 宣  ஥஢ ᮢ,   । ᯮ짮  ⢥ .  ⮬ 砥  㬥⥭    ⮫쪮 㠫 㭪権-童.  祭  ⠪    -  ࠧ稩  㠫묨  㠫묨 㭪ﬨ,   १ ᭮ ᯮ⢠    ந⥫쭮  ᯮ짮 㠫 㭪権.  ⠥ 䠪⮬: ࠪ᪨  ,   ᯮ짮  , 㤥 ᮤঠ 㠫 㭪樨 (. ࠢ 7).
᫨  ᯮ ⥬,   室 ᯮ짮 㠫 㭪権,     ᪮ ࠢ <80-20> (. ⠪ ࠢ 30), ஥ ⢥ত,   ⨯筮 ணࠬ 80 % ६ ᯮ 稢  20 % .  ࠢ ࠩ , ⮬   砥,   । 80 %  㭪権   㠫묨,  뢠 ⨬ ﭨ   ந⥫쭮 ணࠬ. ० 祬  ᯮ  ⮬,     ᥡ ᯮ짮 㠫 㭪権, 㡥,      ⥬ 20 % ணࠬ,    襭  ⢥ ﭨ  ந⥫쭮.
㣠 ࠭ 訡 -   㭪権 㠫묨.   ࠢ쭮,  祬 ᢨ⥫, ਬ, 䥩  (. ࠢ 31).   襭  ⠪   ,   ࠧࠡ稪  ᭮  .  㭪樨   ८।  ந ,   ⠪ 砥 室 ᫥ 㪠  ,  㭪樨 㠫묨.   ᫠  ,       砨 , ⮨  ८।  㭪樨. ᫨   室  ਠ⭮ ⭮⥫쭮 ᯥ樠樨,    ਧ!

 ᫥ 
 ᫥ 䥩 ⫨砥  ᫥ ॠ樨.  ⮬ ᫥ ந  ᥣ ᫥ 䥩  ᮢ.
  㠫 㭪樨 ,  ᫥ ⮫쪮 䥩.
  㠫 㭪樨 ,  ᫥ 䥩  ॠ  㬮砭.
 㠫 㭪樨 ,  ᫥ 䥩  易⥫쭠 ॠ.



ࠢ 35: ᬮ ୠ⨢ 㠫 㭪


।,   ࠡ⠥  ன  ஥  ஢ ᮭ.  襩   ᯮ짮 ࠧ ਠ ࠦ, ᮭ  ࣠ ࠭     ᨫ. ⮬  蠥    㭪-童 healthValue,  頥 楫᫥ 祭, 뢠饥, ᪮쪮  ᨫ ⠫  ᮭ. ᪮ ࠧ ᮭ   ᢮  ᨫ -ࠧ,  ।⠢ ⢥  㭪 healthValue ᫥騬 ࠧ:

class GameCharacter {
public:
virtual void healthValue() const; // 頥  ᨫ ᮭ
... //  ந  
}; // ८।

 䠪,  healthValue     㠫쭠,   ,    ᫥  ᨫ  㬮砭 (. ࠢ 34).
 祢 室  ஥஢,   - ᫥  祢  砥  ᫠. ᪮ 襭  ᮢ襭 ⢥,  ᪫祭,   㤥 㤥   ᬮ७ ୠ⨢. ⮡     , ᬮਬ  㣨 室  ஡.



 ୠ<< ⮤>    㠫쭮 䥩

筥  ᭮ 楯樨,  ⢥ত,  㠫 㭪樨  ᥣ   묨. ஭ ⮩ 誮 ।,  ࠢ쭮 뫮  ⠢ 㭪-童 healthValue ⮩,  ᤥ  㠫쭮  ⠢ 뢠  㠫 㭪,   믮 ॠ ࠡ.   㭪 doHealthValue:

class GameCharacter {
public:
int healthValue() const // ந   ८।
{ //  㭪, . ࠢ 36
... // 믮 ।⥫ ⢨ -
// . 
int retVal = doHealthValue(); // 믮 ॠ ࠡ
... // 믮 騥 ⢨ -
// . 
return retVal;
}
...
private:
virtual int doHealthValue() const // ந  
{ // ८।  㭪
... //   㬮砭  ᫥
} //  ᨫ ᮭ
};

 ⮬  (    ࠢ)  ਢ ⥫ 㭪権  । ᮢ.  ᫥  ࠢ 30, ⥬ ᠬ    ஥묨.  㯠 ⠪   ⮣, ⮡   뫮  . 뢠 室  ஥஢     ⮣,   㭪樨 ஥묨  .
᭮  ⮣ 室 -   ⠬ 뢠  㠫 㭪樨 ।, १  㠫 㭪樨-童 - ⥭    㠫쭮 䥩 (non-virtual interface idiom - NVI).   砩  饣 ୠ ஥஢, 뢠 < ⮤> (Template Method) ( ᮦ,     ⭮襭  蠡 C++).  뢠 㠫 㭪 (healthValue) ⪮ (wrapper) 㠫쭮 㭪樨.
२⢮  NVI ⠨  , ⮬  ﬨ <믮 ।⥫ ⢨>  <믮 騥 ⢨>. ࠧ㬥,    ࠭஢ 㤥 믮 । 맮 㠫쭮 㭪樨, 믮饩 ॠ ࠡ,  ᫥   .  ࠧ, ⪠ ந ⥪ । 맮 㠫쭮 㭪樨 ᮧ,  ᫥  ந . ਬ, <।⥫ ⢨>    墠 ⥪,   ⮪, ஢થ ਠ⮢   믮 ।᫮  . .  ⠢ < ⢨>  室 ᢮ ⥪, ஢ઠ ᫮ 㭪樨, ୠ ஢ઠ ਠ⮢   . . 㤥 㤭⥫쭮 த  , ᫨   ⠬ 뢠 㠫 㭪 ।⢥.
,  ࠧ ᫥ ࠭:  NVI ।,  ந -᫥ ८।  㠫 㭪樨,    뢠-  !    ⨢. ८। 㠫 㭪,  ਬ,    믮 ஥ ⢨. 맮  㠫쭮 㭪樨 । ,   ⢨ 믮.   㣮  .  NVI  ந ᠬ ८। 㠫 㭪 , ⠫ , ࠢ ⥬,  ॠ  㭪樮쭮.    ⠢  ᮡ ࠢ ।,    맢 㭪. 砫    ࠭,  ,  C++ ࠧ蠥  ந  ८।  㠫 㭪樨,  ࠧ㬭.
 NVI  ॡ, ⮡ 㠫 㭪樨 易⥫쭮 뫨 묨.    ᮢ ,  㠫쭠 㭪, ८।  ந , 㤥 뢠  㭪    (  ਬ  ࠢ 27). ⮡ ⠪ 맮 뫨 , 㠫쭠 㭪   饭,   ⮩.      ⮩ (, ਬ,      - . ࠢ 7),   ⮬   NVI 㦥 ਬ.



 ୠ <⥣> ।⢮ 㪠⥫  㭪樨

 NVI -  ᭠ ୠ⨢  㠫 㭪,   窨 ७ ஥஢    ᫨誮 .   殢,  -० ᯮ㥬 㠫 㭪樨  ᫥  ᨫ  ᮭ.  窨 ७ ஥஢ ࠧ  ᨫ 뫮  ⢥ত  ⮬,  ᫥  ᨫ ᮭ    ⨯ ᮭ,  ⠪ ᫥    ᢮⢮ ᮭ  ⠪. ਬ,   ॡ, ⮡   ᮭ । 㪠⥫  㭪,  뢠   ᫥   ᨫ:

class GameCharacter; // ०饥 
// 㭪 ⬠  㬮砭  ᫥  ᨫ ᮭ
int defaultHealthCalc(const GameCharacter& gc);
class GameCharacter {
public:
typedef int (*HealthCalcFunc)(const GameCharacter&);
explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc)
: healthFunc(hcf)
{}
int healthValue() const
{ return healthFunc(*this);}
...
private:
HealthCalcFunc healthFunc;
};

 ⮩ ਬ ਬ 㣮 ࠭ ୠ ஥஢ - <⥣> (Strategy).  ࠢ  室, ᭮묨  㠫 㭪  娨 GameCharacter,  ।⠢   , 騥 :
   ᮭ   ⮣  ⨯   ࠧ 㭪樨 ᫥  ᨫ. ਬ:

class EvilBadGay: public GameCharacter {
public:
explicit EvilBadGay(HealthCalcFunc hcf = defaultHealthCalc)
: GameCharacter(hcf)
{...}
...
};
int loseHealthQuickly(const GameCharacter&); // 㭪樨 ᫥
int loseHealthSlowly(const GameCharacter&); //  ᨫ
//  ࠧ 
EvilBadGay ebg1(loseHealthQuickly); // ⨯ ᮭ
EvilBadGay ebg2(loseHealthSlowly); //  ࠧ 
// ⭮⥫쭮 ஢

 㭪 ᫥  ᨫ    ⮣  ᮭ    ६ ᯮ. ਬ,  GameCharacter   । 㭪-童 setHealthCalculator,    ⥪ 㭪 ᫥  ᨫ.
 㣮 ஭,  䠪,  㭪 ᫥  ᨫ    㭪樥-童 娨 GameCharacter, 砥,     ᯥ樠쭮 㯠  ७ ﭨ ꥪ,   ᨫ . ਬ, defaultHealthCalc   㯠    EvilBadGay.   譮, ᫨  ᨫ ᮭ   ᫥    ⮣ 䥩,   ᨬ쭮  ⮢     ⮩ ଠ樨.  ᠬ  ⠪ ஡   ᥣ,   㭪樮쭮 뭮    (ਬ,  㭪権-童  ᢮ 㭪樨,  騥 ﬨ ,   㭪樨-童 㣮 ,  㦥⢥ ).  㤥   饬 ࠢ  , ⮬   稥 ஥ 襭,    ।⮨ ᬮ, ⮦  ᯮ짮 㭪権, 室  娨 GameCharacter.
饥 ࠢ ⠪: ⢥ ᯮᮡ  㭪,  騬 童 ,    ⮩  - ᫠ ⥯ 樨. ਬ,    㭪樨-童  ⢥ 㧥  ।⠢  㭪樨  㯠  ⥬  ॠ樨,   뫮  ⠢ 묨.    ⢮ 樥 ࠤ 룮  ᯮ짮 㪠⥫  㭪樨  㠫 㭪権 (ਬ, ⮡  ࠧ 㭪樨  ᨫ  ࠧ ꥪ⮢  ᪨  ),     ⭮ 砥.



 ୠ <⥣> ।⢮  tr::function

᫨  ਢ몫  蠡   ਬ  ஥  䥩ᮢ (. ࠢ 41),  ਬ 㪠⥫  㭪樨    ᫨誮  襭. 祬   ᫥  ᨫ 㦭 易⥫쭮 ᯮ짮 㭪,   - 饥 ᥡ  㭪 (ਬ, 㭪樮 ꥪ)? ᫨  㭪樨 㤠  ,  祬  ᤥ  童 ?  祬 㭪   int,   ꥪ,   ८ࠧ  int?
 ࠭祭 祧, ᫨  㪠⥫  㭪 ( healthFunc) ᯮ짮 ꥪ⮬ ⨯ tr::function.    ࠢ 54, ⠪ ꥪ  ᮤঠ  뢠 魮 (㪠⥫  㭪, 㭪樮 ꥪ  㪠⥫  㭪-童),  ᨣ ᮢ⨬  .  ਬ ⠪ 室,   ࠧ  ᯮ짮 tr1::function:

class GameCharacter; //  ࠭
int defaultHealthCalc(const GameCharacter& gc); //  ࠭
class GameCharacter {
public:
// HealthCalcFunction -   뢠 魮, ன 
// ।  ⢥ ࠬ , ᮢ⨬  GameCharacter,
//   頥 , ᮢ⨬  int; ஡ . 
typedef std::tr1::function<int (const GameCharacter&)> HealthCalcFunc;
explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc)
: healthFunc(hcf)
{}
int healthValue() const
{ return healthFunc(*this);}
...
private:
HealthCalcFunc healthFunc;
};

 , HealthCalcFunc -  typedef, 뢠騩 ⨧ 蠡 tr1::function.  ,  ࠡ⠥  饭 㪠⥫  㭪. ᬮਬ ⥫쭥,  । ⨯ HealthCalcFunc:

std::tr1::function<int (const GameCharacter&)>

  뤥 <楫 ᨣ>  ⨧樨 tr1::function.     ⠪: <㭪, ਭ 뫪  ꥪ ⨯ const GameCharacter   int>. ꥪ ⨯ HealthCalcFunc  ᮤঠ  뢠 魮,  ᨣ ᮢ⨬  .  ᮢ⨬   砥 砥,  ࠬ   ८ࠧ  const GameCharacter&,  ⨯ 頥 祭    int.
᫨ ࠢ  ।騬 ਠ⮬  ( GameCharacter 砫  ᥡ 㪠⥫  㭪),    㦨   ⫨稩. ⢥ ࠧ  ⮬,  GameCharacter ⥯ ᮤন ꥪ ⨯ tr1::function - 饭 㪠⥫  㭪.   ⠪ ⥫쭮,      ⢥, ᫨    ⥫⢮,  ⥯ 짮⥫ 砥 襫   ᯥ䨪樨 㭪権,   ᨫ:

short calcHealth(const gameCharacter&); // 㭪 ᫥
//  ᨫ;
//  頥  int
stuct HealthCalculator { //  㭪樮
int operator()(const GameCharacter&) const // ꥪ⮢, 
{...} //  ᨫ
};
class GameLevel {
public:
float health(const GameCharacter&) const; // 㭪-童 
... // ᫥ 
}; // ᨫ; 頥  int
class EvilBadGay: public GameCharacter { //  ࠭
...
};
class EyeCandyCharacter: public GameCharacter { // 㣮 ⨯ ᮭ;
... // । ⠪ 
}; //  
//  EvilBadGay
EvilBadGay ebg1(calcHealh); // ᮭ ᯮ
// 㭪 ᫥
//  ᨫ
EyeCandyCharacter ecc1(HealthCalculator()); // ᮭ ᯮ
// 㭪樮 ꥪ
// ᫥ 
// ᨫ
GameLevel currentLevel;
...
EvilBadGay ebg2( // ᮭ ᯮ
std::tr1::bind(&GameLevel::health, // 㭪-童 
currentLevel, // ᫥ 
_1) // ᨫ; ஡
); // . 

筮  ࠦ ⮬,  㤨⥫    蠡 tr1::function. ᫨   ࠧ  ,   ᪫祭,    ,  祣 ᯮ tr1::bind  । ebg2.   ᭨.
 ⨬ ᪠,   ᫥  ᨫ ᮭ ebg2 ᫥ ᯮ짮 㭪-童  GameLevel.    GameLevel::health ᫥,    ਭ  ࠬ (뫪  GameCharacter),   ᠬ   ਭ , ⮬     ࠬ ⨯ GameLevel - ,     㪠뢠 this.  㭪樨 ᫥  ᨫ ਭ   ࠬ: 뫪  ᮭ GameCharacter,   ᨫ . ᫨  ᯮ㥬 㭪 GameLevel::health,   - ࠧ <஢> , ⮡   ࠬ஢ (GameCharacter  GameLevel)  ਭ ⮫쪮  (GameCharacter).  ⮬ ਬ  ⨬  ᫥ ஢ ebg2  ⢥ ࠬ ⨯ GameLevel ᥣ ᯮ짮 ꥪ currentLevel, ⮬ <ਢ뢠>    ࠬ  맮 GameLevel::health.   ⮬  砥  맮 tr1::bind: 㪠,  㭪 ᫥  ᨫ ᮭ ebg2   ⢥ ꥪ ⨯ GameLevel ᯮ짮 currentLevel.
 ய᪠ 楫  ஡⥩,  ਬ: 祬 <_1> 砥 <ᯮ짮 currentLevel  ⢥ ꥪ GameLevel  맮 GameLevel::health  ebg2>.  ⠫  ⮫ ᫮,  ⮬     אַ ⭮襭  ᭮ ,    த஢,  : ᯮ tr1::function  㪠⥫  㭪,  塞 짮⥫ ਬ  ᮢ⨬ 뢠 魮  ᫥  ᨫ ᮭ. ,  ࠢ ?



<᪨>  <⥣>

᫨     ஥஢, 祬 ᮡ⢥  C++,   ࠤ樮 室  ॠ樨 ୠ <⥣> ⮨  ⮬, ⮡ ᤥ 㭪 ᫥  ᨫ 㠫쭮 㭪樥-童  , ਭ ⤥쭮 娨.    룫拉 ਬ୮ ⠪:
᫨     樥 UML, :  ,  GameCharacter - ७ 娨,  ன EvilBadGay  EyeCandyCharacter  ந묨 ᠬ; HealthCalcFunc - ७ 娨,  ன ந묨 ᠬ  SlowHealthLooser  FastHealthLooser;  ꥪ ⨯ GameCharacter ᮤন 㪠⥫  ꥪ  娨 HealthCalcFunc.     ᮮ⢥騩 :

class GameCharacter; // ०饥 
class HealthCalcFunc {
public:
...
virtual int calc(const GameCharacter& gc) const
{...}
...
};
HealthCalcFunc defaultHealthCalc;
class GameCharacter {
public:
explicit GameCharacter(HealhCalcFunc *phfc = &defaultHealthCalc)
:pHealtCalc(pfhc)
{}
int healthValue() const
{ return pHealthCalc->calc(*this);}
...
private:
HealhCalcFunc * pHealtCalc;
};

 室 ਢ⥫ ⥬,  ணࠬ,   <⠭⭮> ॠ樥 ୠ <⥣>, ࠧ ,   祬.  ⮬   ।⠢  ஢ 騩  ᫥  ᨫ ⥬  ந ᮢ   HealthCalcFunc.





 ⮣ ࠢ     ࠪ ४: ࠧ  ⥬,    ।  ,   ᬠਢ  ⮫쪮 㠫 㭪樨.  ⪨ 祭 । ୠ⨢:
 ਬ  㠫쭮 䥩 (NVI), ਠ ୠ ஥஢ < ⮤>.    ⮬, ⮡  묨 㠫묨 㭪ﬨ-童 맮  㯭 㠫 㭪権.
  㠫 㭪権 童  - 㪠⥫ﬨ  㭪樨.  饭  ୠ ஥஢ <⥣>.
  㠫 㭪権 童  - tr1::function.   ਬ  뢠 魮, ᨣ ன ᮢ⨬  ⮩,   㦭.  ⮦ ଠ ୠ ஥஢ <⥣>.
  㠫 㭪権   娨 㠫묨 㭪ﬨ  㣮 娨.  ࠤ樮 ॠ ୠ ஥஢ <⥣>.
  뢠騩 ᯨ᮪ ୠ⨢ 㠫 㭪,    墠, ⮡ 㡥   ⮬,  ⠪ ୠ⨢ .  ⮣,  ࠢ  ⮨  ⪮   ᭮,  ᬠਢ  ⮨.
⮡      ண ꥪ⭮-ਥ஢ ஥஢, ⮨ ६  ६ १ 稢 . ⥩ .  ६  ⢮  .

 ᫥ 
   ୠ⨢ 㠫 㭪 ⭮  NVI  ࠧ  ୠ ஥஢ <⥣>.  NVI ᠬ  ᥡ -  ਬ ॠ樨 ୠ < ⮤>.
 ⮪ ७ 㭪樮쭮  㭪権-童   砥  ⮬,  㭪-童 ⠥ ࠢ 㯠   童 .
 ꥪ tr1::function ࠡ  饭 㪠⥫  㭪樨.  ꥪ ন  뢠 魮, ᮢ⨬  ᨣன 楫 㭪樨.




ࠢ 36:   ८। ᫥㥬 㠫 㭪樨

।,  ᮮ ,   D  ᫥  B     B ।  㭪-童 mf.  ࠬ  ⨯ 頥 祭  , ⮬   ।,   void. 㣨 ᫮,   ᫥饥:

class B {
public:
void mf();
...
};
class D: public B {...};

 祣    B, D  mf,  ꥪ x ⨯ D,

D x; // x - ꥪ ⨯ D

, ୮, 㤨,  

B *pB = &x; //  㪠⥫  x
PB->mf(); // 맢 mf   㪠

 ᥡ , 祬

D *pD = &x; //  㪠⥫  x
PD->mf(); // 맢 mf १ 㪠⥫

     뢠 㭪-童 ꥪ x. ᪮       ⮩  㭪樥    ⥬  ꥪ⮬,       ,  ⠪ ?
, ⠪  ,   ᥣ 뢠.  ⭮,    १, ᫨ mf 㠫쭠,  D । ᮡ⢥  mf:

class D: public B {
public:
void mf(); // 뢠 B:mf; . ࠢ 33
...
};
PB->mf(); // 맢 B::mf
PD->mf(); // 맢 D::mf

稭 ⠪ <㫨筮>  砥  ⮬,  㠫 㭪樨,  B::mf  D::mf, 뢠 ᪨ (. ࠢ 37).  砥,   pB   㪠⥫  ꥪ ⨯ B, 㠫 㭪樨, 뢠 ।⢮ pB,-  ᥣ 㭪樨, ।     B,  ᫨ pB,    ਬ, 㪠뢠  ꥪ , ந  B.
 㣮 ஭, 㠫 㭪樨 뢠 ᪨ (᭮ . ࠢ 37), ⮬     ⠪ ஡. ᫨  㭪 mf 뫠 㠫쭮,   맮  ।⢮ pB, ⠪  ।⢮ pD 砫  맮 D::mf, ⮬  ⢨⥫쭮 pB  pD 㪠뢠  ꥪ ⨯ D.
 ⮣, ᫨    D  ८। 㠫 㭪 mf, ᫥㥬   B,  ⭮,  ꥪ D   ᥡ ᮢ襭 ।᪠㥬.  ⭮,   ꥪ D   ᥡ  맮 mf   B,   D, 祬 ।騬 䠪஬ 㤥  ⨯ ᠬ ꥪ,   ⨯ 㪠⥫  .  ⮬ 뫪  ⮬ ⭮襭  ᥡ 祬   㪠⥫.
 ,  ⭮  <ࠣ᪮> 㬥樨. ,  㢥७, ॡ ஥ ⥮᪮ ᭮   ८। ᫥㥬 㠫 㭪権.  㤮⢨  ।⠢.
 ࠢ 32 ,  ⮥ ᫥ ᥣ 砥 < ࠧ>,   ࠢ 34 , 祬  㠫쭮 㭪樨   । ਠ ⭮⥫쭮 ᯥ樠樨 ⮣ . ᫨  ਬ    ᠬ B  D  㠫쭮 㭪樨 B: mf,   ᫥饥:
 ,  ਬ  ꥪ⠬ B, ਬ   ꥪ⠬ D, ᪮  ꥪ D ⠪  ꥪ⮬ B;
  B  ᫥  䥩, ⠪  ॠ mf, ⮬  mf 㠫쭠  B.
, ᫨ D ८। mf,  ⨢稥. ᫨  D ⢨⥫쭮  ᮤঠ ⫨  B ॠ mf  ᫨  ꥪ B, 騩 ࠧ B, ⢨⥫쭮  ᯮ짮 ॠ mf  B, ⮣ ୮,   ꥪ  D  ࠧ B.  ⮬ 砥 D    ᫥ B.  㣮 ஭, ᫨  D ⢨⥫쭮   ᫥ B  ᫨ D ⢨⥫쭮  ᮤঠ ॠ mf, ⫨  B, ⮣ ୮,  mf  ਠ⮬ ⭮⥫쭮 ᯥ樠樨 B.  ⮬ 砥 mf   㠫쭮.  , ᫨  ꥪ  D ⢨⥫쭮  ࠧ B  ᫨ mf - ⢨⥫쭮 ਠ ⭮⥫쭮 ᯥ樠樨 B, ⮣ D,  ࠢ ,  㦤  ८। mf      .
ᨬ  ⮣,   㬥⮢ ਬ  襬 砥, 祬- ਤ ⢮,    ⥫⢠   ८। ᫥㥬 㠫 㭪権 ⠥  ᨫ.
᫨  ⥭ ⮣ ࠢ    饭 < >, , ୮,   ᯮ ࠢ 7,   , 祬        㠫묨. ᫨   ᫥ ⮬ ᮢ (   㠫     ),  蠥  ॡ,   饬 ࠢ, ⮬   ந  ⮬᪨ ८। 㭠᫥ 㠫 㭪 -   .  ୮   ந ᮢ,    ஢, ⮬ ,    ࠢ 5,    ⮬᪨, ᫨   ।  ᠬ.  , ࠢ 7 -    砩 饣 ࠢ,   㦨 ⤥쭮   ४権  ਬ.

 ᫥ 
   ८। ᫥㥬 㠫 㭪樨.



ࠢ 37:   ८। ᫥㥬 祭 㬥 㭪樨  㬮砭

  ᠬ 砫 ⨬ 㦤.  ⮫쪮  ⨯ 㭪権,   ᫥: 㠫  㠫.  ८। ᫥㥬 㠫 㭪樨   砥 訡筮 (. ࠢ 36), ⮬    ࠭  㦤 砥 ᫥ 㠫쭮 㭪樨  祭 㬥  㬮砭.
  ⥫⢠ ⨢஢ 饣 ࠢ ⠭ 筮 祢: 㠫 㭪樨 뢠 ᪨,  祭 㬥⮢  㬮砭 - ᪨.
  ?  ,  㦥 뫨,  祬 砥 ࠧ  ᪨  ᪨ 뢠? (, ᪮ 뢠 뢠  ࠭ 뢠,  ᪮ - .)  ,  ᢥ  .
᪨ ⨯ ꥪ -  ⨯,    ⥪ ணࠬ. ᬮਬ ᫥  ᮢ:

//   ।⠢ ᪨ 䨣
class Shape {
public:
enum ShapeColor { Red, Green, Blue };
//  䨣  ।⠢ 㭪  ᮢ
virtual void draw(ShapeColor color = Red) const = 0;
...
};
class Rectangle: public Shape {
public:
// , 㣮 祭 ࠬ  㬮砭 - !
virtual void draw(ShapeColor color = Green) const;
...
};
class Circle: public Shape {
public:
virtual void draw(ShapeColor color) const;
...
};

᪨   ।⠢ ⠪:
 ᬮਬ ᫥騥 㪠⥫:

Shape *ps; // ᪨ ⨯ - Shape*
Shape *pc = new Circle; // ᪨ ⨯ - Shape*
Shape *pr = new Rectangle; // ᪨ ⨯ - Shape*

 ⮬ ਬ ps, pc  pr   㪠⥫  Shape, ⠪       㤥 㯠  ஫ ᪮ ⨯. ⬥⨬,   ᮢ襭 ࠧ筮,    㪠뢠  ⢨⥫쭮, - ᨬ  ⮣   ᪨ ⨯ Shape*.
᪨ ⨯ ꥪ । ⨯ ⮣ ꥪ,    뫠   . 묨 ᫮, ᪨ ⨯ ।  ꥪ.  ਢ  ਬ ᪨ ⨯ pc -  Circle*,  ᪨ ⨯ pr - Recangle*.  ᠥ ps,     ᪮ ⨯, ⮬   㪠뢠    ꥪ ().
᪨ ⨯,  ᫥   ,     ࠡ ணࠬ, 筮 ᫥⢨ ᢠ:

ps = pc; // ᪨ ⨯ ps ⥯ Circle*
ps = pr; // ᪨ ⨯ ps ⥯ Rectangle*

㠫 㭪樨 뢠 ᪨,   ᪨ ⨯ 뢠饣 ꥪ ।,  ⭠ 㭪 뢠:

pc->draw(Shape::Red); // 뢠 Circle::draw(Shape::Red)
pr->draw(Shape::Red); // 뢠 Rectangle::draw(Shape::Red)

 ,     ⭮,  , ᮬ, ࠧࠥ  㠫 㭪.  ᭮ 稭,   室  㠫 㭪  㬥⠬, ਭ騬 祭  㬮砭, ᪮,   㦥 ᪠, 㠫 㭪樨 뢠 ᪨,  㬥  㬮砭 - ᪨. ⥫쭮,   ਩  ⮬,  㤥 뢠 㠫 㭪, ।  ந ,   ⮬ ᯮ짮 㬥  㬮砭,    :

pr->draw(); // 뢠 Rectangle::draw(Shape::Red)!

 ⮬ 砥 ᪨ ⨯ pr -  Rectangle*, ⮬,    , 뢠 㠫쭠 㭪  Rectangle.  㭪樨 Rectangle::draw 祭 㬥  㬮砭 - Green.  ᪮ ᪨ ⨯ pr - Shape*,  祭 㬥  㬮砭    Shape,   Rectangle!  १ 砥 맮, 騩  ࠭, ᮢ襭  樨  draw  ᮢ Shape  Rectangle.
 䠪,  ps, pc  pr  㪠⥫ﬨ,  ࠥ  ஫.   뫪, १ ⠫  ⠪ .  ,  draw - 㠫쭠 㭪,  祭  㬮砭    㬥⮢ ८।  ந .
祬 C++ ⠨  ⠪  ? ⢥    易  䥪⨢ ᯮ ணࠬ. ᫨  祭 㬥⮢  㬮砭 뢠 ᪨,   諮   ᯮᮡ  ६ ᯮ ।,  祭  㬮砭    ࠬ 㠫쭮 㭪樨,    孨᪨ ᫮ 뭥譥 堭. 襭 뫮 ਭ   ᪮   ॠ樨,  १ 祣   짮 २⢠ 䥪⨢ 믮  ணࠬ.  ᫨  ᫥ ᮢ,   饬 ࠢ,  ணࠬ 㤥  ᥡ 筮.
  ४᭮,  ᬮ,  , ᫨,  ᫥ ⮬ ࠢ,   㬥  祭ﬨ  㬮砭  㭪-童,    ,   ந :

class Shape {
public:
enum ShapeColor { Red, Green, Blue };
virtual void draw(ShapeColor color = Red) const = 0;
...
};
class Rectangle: public Shape {
public:
virtual void draw(ShapeColor color = Red) const;
...
};

, 㡫஢ ! 㦥 ⮣: 㡫஢   ᨬﬨ: ᫨ 祭 㬥  㬮砭   Shape, ਤ      ந .  ⨢ 砥   ८। ᫥㥬 祭  㬮砭.  ?
    ஡  ⥬, ⮡ ⠢ 㠫 㭪  ᥡ ⠪,   ,  ࠧ㬭 ᬮ ୠ⨢ 襭,   ࠢ 35 ⠪ ୠ⨢ ਢ .    -  㠫쭮 䥩 (NVI): ।     㠫 㭪,  뢠  㠫 㭪, ८।塞  .   砥  । 㠫 㭪  㬥⮬  㬮砭  㠫 㭪,  믮  ॠ ࠡ:

class Shape {
public:
enum ShapeColor( Red, Green, Blue };
void draw(ShapeColor color = Red) const // ⥯ - 㠫쭠
{
doDraw(color); // 맮 㠫쭮 㭪樨
}
...
private:
virtual void doDraw(ShapeColor color) const = 0; // ॠ쭠 ࠡ
}; // 믮
//  ⮩ 㭪樨
class Rectangle: public Shape {
public:
...
private:
virtual void doDraw(ShapeColor color) const //  
... //  ⢨  㬥
}; // 祭  㬮砭

᪮ 㠫 㭪樨    ८।  ந  (. ࠢ 36),  ᭮,   ⠪ 室 祭  㬮砭  ࠬ color 㭪樨 draw ᥣ 㤥 Red.

 ᫥ 
   ८। ᫥㥬 祭 㬥⮢  㬮砭, ⮬  㬥  㬮砭 뢠 ᪨, ⮣  㠫 㭪樨 -  ⮫쪮    ८।,- ᪨.



ࠢ 38:  ⭮襭 <ᮤন>  <ॠ ।⢮>   樨

 -  ⭮襭  ⨯, ஥  ⮣,  ꥪ  ⨯ ᮤন  ᥡ ꥪ 㣨 ⨯. ਬ:

class Address {...}; //  ஦
class PhoneNumber {...};
class Person {
public:
...
private:
std::string name; //  ꥪ
Address address; //  
PhoneNumber voiceNumber; //  
PhoneNumber faxNumber; //  
};

  砥 ꥪ  Person   ᥡ ꥪ ᮢ string, Address  PhoneNumber. ନ    ᨭ, ਬ: , ॣ஢  ࠨ.
 ࠢ 32 ,  ⮥ ᫥ 砥 <  ࠧ 㣮 >.  樨 ⮦  ᥬ⨪,  : <ᮤন>  <ॠ ।⢮>.   ⮬,   ᢮ ணࠬ      ࠧ묨 ﬨ.  ணࠬ ꥪ 뢠 魮  㥬 : , ⮬,   . .  ꥪ   ।⭮ . 㣨 ꥪ    ॠ樨, ਬ: , ⥪, ॢ ᪠  . .  ⭮   ॠ樨, ᢮⢥  襣 ਫ.  ⭮襭 樨   ꥪ⠬  ।⭮ ,   ᥬ⨪ <ॠ ।⢮>.
襯ਢ  Person  ⭮襭 ⨯ <ᮤন>. ꥪ Person  , ,  ⥫䮭  䠪.  ᪠,  祫 < ࠧ>    祫 < ࠧ> .  ᪠,  祫 <> (<ᮤন>)   . 設⢮   뢠 㤭  ஢  ࠧ稩, ⮬ ⠭  ஫ﬨ <>  <ᮤন>  ࠢ⥫쭮 ।.
 ᫮ ஢ ࠧ稥  ⭮襭ﬨ <>  <ॠ ।⢮>. ਬ, ।,   㦥 蠡  ᮢ, ।⠢ ⢠ ந ꥪ⮢,   ஢  㡫⮢. ᪮ ୮ ᯮ짮 - ४᭠ ,  ࠧ     蠡 set  ⠭⭮ ⥪.   殢, 祬   蠡,    ᯮ짮 㦥 ⮢?
 ᮦ, ॠ樨 set 筮   ᮡ  室 -   㪠⥫  . 易   ⥬,  ⢠ 筮 ॠ   ᡠ஢ ॢ쥢 ᪠, ࠭ 䬨᪮ ६ ᪠, ⠢  㤠.  த⢨ , 祬 ꥬ  ,   ࠧ㬭 襭,  ⭮  襣 ਫ ,     ⢥. ⮬ ⠭ 蠡 set   ਥ. 宦, 㦭  ᢮ ᮡ⢥.
   ୮ ᯮ짮 - ४᭠ .  ᯥ⮬    ,  ,  ।  ਠ⮢ ॠ樨    ⠪,    ਬ 易 ᯨ᪮.  ⠪ ,   ⠭⭮ ⥪ C++  蠡 list, ⮬ 蠥  ᯮ짮 (୮).
 ⭮,  蠥,  ᮧ  蠡 Set  ᫥  list.   Set<T> 㤥 ᫥ list<T>.  ⮣  襩 ॠ樨 ꥪ Set 㤥 㯠  ꥪ list. ⢥⢥,   Set ᫥騬 ࠧ:

template<typename T> // ࠢ ᯮᮡ ᯮ짮
class Set: public std::list<T> {...}; // list  । Set

    த  諮 , , ᫨ ᬮ,   ࠫ 訡.    ࠢ 32, ᫨ D  ࠧ B,  ,  ୮  B,   ୮ ⠪   D.  ꥪ list  ᮤঠ 㡫, ⮬ ᫨ 祭 3051 ⠢  list<int> ,  ᯨ᮪ 㤥 ᮤঠ   3051. ⨢, Set   ᮤঠ 㡫⮢, ⮬, ᫨ 祭 3051 ⠢  Set<int> , ⢮ 㤥 ᮤঠ     祭. ⥫쭮, ⢥ত,  Set  ࠧ list, :   ,   ꥪ⮢ list,   ꥪ⮢ Set.
- ⮣ ⭮襭  ⨬  ᠬ  室  । <>, ⮥ ᫥ - ࠢ ᯮᮡ ஢ ⮩ 裡. ࠢ 室 ᭮   ⮣ 䠪,  ꥪ Set   ॠ ।⢮ ꥪ list:

template<typename T> // ࠢ ᯮᮡ ᯮ짮 list
class Set { //  । Set
public:
bool member(const T& item) const;
void insert(const T& item);
void remove(const T& item);
std::size_t size() const;
private:
std::list<T> rep; // ।⠢ ⢠
};

㭪樨-童  Set    㭪樮쭮, ।⠢塞 list  㣨 ﬨ ⠭⭮ ⥪, ⮬  ॠ 㤭 ,  ᪮   ᭮ ணࠬ஢  ਬ ⥪ STL:

template<typename T>
bool Set<T>::member(const T& item) const
{
return std::find(rep.begin(), rel.end(), item) != rep.end();
}
template<typename T>
void Set<T>::insert(const T& item)
{
if(!member(item)) rep.push_back(item);
}
template<typename T>
void Set<t>::remove(const T& item)
{
typename std::list<T>::iterator it = // . ࠢ 42
std::find(rep.begin(), rep.end(), item); // ଠ  "typename"
if(it != rep.end()) rep.erase(it);
}
template<typename T>
std::size_t Set<T>::size() const
{
return rep.size();
}

 㭪樨 筮 , ⮡  ⠬  ࠨ,  । ਭ⨥ ⥫쭮 襭 ⮨  ࠧ  ࠢ 30.
⮨ ⬥,  䥩 Set  ⢥砫  ॡ ࠢ 18 (஥஢ 䥩 ⠪, ⮡   뫮 ᯮ짮 ࠢ쭮  㤭 - ࠢ쭮), ᫨   ᫥ ᮣ襭, ਭ  STL-⥩஢,   ⮣ 諮     Set ⮫쪮 ,    ⮭㫠  ᭮ : ந஢   Set  list. ᪮ ⥬ 饣 ࠢ -   ,   㥬 ᮢ⨬  STL ࠤ 來. ⪨ 䥩 Set  , , ⥭  ᯮਬ 䠪,  ⭮襭  ᠬ Set  list -  <> (  砫  ),  <ॠ ।⢮>.

 ᫥ 
 ⨪ 樨 न쭮 ⫨砥  ᥬ⨪ ⮣ ᫥.
  ।⭮   砥 <ᮤন>.   ॠ樨  砥 <ॠ ।⢮>.



ࠢ 39: த뢠 室  ᯮ짮 ⮣ ᫥

 ࠢ 32 ,  C++ ᬠਢ ⮥ ᫥  ⭮襭 ⨯ <>.  ⭮, ,  , ⮫  娥,   Student  ᫥  Person,  ८ࠧ ꥪ  Student  ꥪ  Person, ᫨  室  맮 㭪権. 祢, ⮨  ࠧ ਢ ࠣ ,    ⮥ ᫥ :

class Person {...}
class Student: private Person {...} // ⥯ ᫥ ⮥
void eat(const Person& p); //    
void study(const Student& s); // ⮫쪮 㤥 
Person p; // p - 祫 (Person)
Student s; // s - 㤥 (Student)
eat(p); // ଠ쭮, p - ⨯ Person
eat(s); // 訡! Student   ꥪ⮬
// Person

᭮,  ⮥ ᫥  砥 <>.    ⮣  砥?
<⮯!- ᪫栥 .- ० 祬   祭,  ਬ  .   ᥡ ⮥ ᫥?> ࢮ  ࠢ, ॣ ⮥ ᫥,  ⮫쪮    ⢨:  ⨢ ⮬ ᫥   饬 砥  ८ࠧ ꥪ ந  (⠪  Student)  ꥪ   (⠪  Person).  祬 맮 eat  ꥪ s 訡祭. ஥ ࠢ ⮨  ⮬,  童, ᫥㥬  ⮣  , ⠭ 묨,  ᫨     뫨   饭  .
 ,  ᠥ .  ⥯ ୥  祭. ⮥ ᫥ 砥 <ॠ ।⢮:>.   D  ᫥  B,  㯠 ⠪ ⮬,  ᮢ  ᯮ짮 ண , 㦥 ᠭ  B,   ⮬,   ꥪ⠬ B  D   楯㠫쭠 .  ࠧ, ⮥ ᫥ -  ᪫⥫쭮 ਥ ॠ樨. ( 祬  㭠᫥  ⮣   ⠭    襬 :    祬 ⠫ ॠ樨). ᯮ ନ  ࠢ 34,  ᪠,  ⮥ ᫥ 砥 ᫥  ⮫쪮 ॠ樨,  䥩. ᫨ D  ᫥ B,  砥,  ꥪ D ॠ ।⢮ ꥪ⮢ B,  祣 . ⮥ ᫥ 祣  砥  室 ஥஢ ணࠬ ᯥ祭  ⠥  ⮫쪮  ⠯ ॠ樨.
⢥ত,  ⮥ ᫥ 砥 <ॠ ।⢮>, ⭮, ᫥  , ᪮  ࠢ 38 㪠뢠,       ᠬ.   ᤥ 롮  ? ⢥ : ᯮ ,  ,  ⮥ ᫥ -  易 ⠪ 㯨.      易 ᯮ짮 ⮥ ᫥?   । ⮣,   業  饭 童 / 㠫 㭪樨,   ⠪ ࠭ 樨,  ᮮࠦ    த⮢ 롮   ⮣ ᫥.
।,   ࠡ⠥  ਫ,  ஬  ꥪ  Widget,  訫  ᫥ ࠧ  ⥬,   ᯮ. ਬ, ᭮  ⮫쪮 , ᪮쪮  뢠 㭪樨-童 Widget,      饭     ६. ணࠬ,    ᪮쪮 ࠧ 䠧 ᯮ,   ᥡ -ࠧ   䠧. ਬ, 㭪樨, ᯮ㥬 ஬  ⠯ ᨭ⠪᪮ , ⥫쭮 ⫨  㭪権, 뢠  ६ ⨬樨  樨 .
 蠥 ஢  Widget ⠪, ⮡ ᫥, ᪮쪮 ࠧ 뢠  㭪-童.  ६ ᯮ  㤥 ਮ᪨ 뢠  ଠ, ,   祭ﬨ  ꥪ Widget  㣨 묨,  ⥬ 室.  ⮣  ⠭ ⠩,  㤥    ⮬,  㯠 ६ ᮡ ⨪ ᯮ짮.
। ୮ ᯮ짮 饣  ᠭ ,  ⥫쭮 ᬮਬ   㬥⮢   ᫥騩 :

class Timer {
public:
explicit Timer(int tickFrequency);
virtual void onTick() const; // ⮬᪨ 뢠
//   ⨪
...
};

  ࠧ ,   ᪠. ꥪ Timer  ந  ࠡ뢠   ⮩,    <⨪> 㤥 뢠 㠫쭠 㭪.   ८।  㠫 㭪 ⠪, ⮡  ஢﫠 ⥪饥 ﭨ Widget. ⫨筮!
 ⮣ ⮡  Widget ८। 㠫 㭪 Timer,   ᫥ Timer.  ⮥ ᫥   砥  室.  Widget   ࠧ Timer. 짮⥫ Widget     뢠 onTick  ꥪ Widget, ⮬   㭪    楯㠫쭮 䥩 ⮣ . ᫨ ࠧ 맮  㭪樨,  짮⥫   ࠡ  䥩ᮬ Widget ४⭮,  祢 蠥 ४  ࠢ 18  ⮬,  䥩    ਬ ࠢ쭮  㤭 - ࠢ쭮. ⮥ ᫥   砥  室.
⮬  㤥 ᫥ :

class Widget: private Timer {
private:
virtual void onTick() const; // ᬮ   ᯮ짮
... // Widget  . .
};

 ⮬ ᫥  㭪 onTick  Timer ⠭ ⮩  Widget,  ᫥ ८।   ⠪  ⠢.  , ᫨  onTick  ᥪ public,     㦤 짮⥫, ⠢  㬠,    맢,    ࠧ१  ࠢ 18.
 宥 襭,  ⮨ ⬥,  ⮥ ᫥    ண 室.   蠥   ᯮ짮 .        Widget,  㤥  ᫥  Timer  ८। onTick,  ⥬ ⨬ ꥪ ⮣ ⨯  Widget.  ᪨ ⠪ 室:
class Widget {
private:
class WidgetTimer: public Timer {
public:
virtual void onTick() const;
...
};
WidgetTimer timer;
...
};

  ᫮ ⮣,  ᯮ ⮫쪮 ⮥ ᫥, ⮬   ᯮ  ⮥ ᫥,  ,   ᥬ     (WidgetTimer). ⭮ ,    ਠ   ।  ⮣, ⮡   ⢮ ࠧ 室  襭  . ⮨ ਢ몠  ⮬, ⮡  ࠭稢 ⢥ 襭 (. ⠪ ࠢ 35).      ।⠢  稭,      । ⮥ ᫥  ⠭  樥 ⮬ ᫥.
-,   ஥஢  Widget ⠪, ⮡   뫮 ᫥,   ⮬  ந ᠬ ८। 㭪 onTick. ᫨ Widget ᫥  Timer,   ,   砥 ⮣ ᫥. (,  ᮣ᭮ ࠢ 35 ந   ८। 㠫 㭪樨,  ᫨   뢠 ).  ᫨ WidgetTimer -     Widget,  ᫥ Timer,  ந  Widget    㯠  WidgetTimer,  ,    ᫥ ,  ८।  㠫 㭪樨. ᫨  室 ணࠬ஢  몠 Java  C#    ⨫   ,    ந ᠬ ८। 㭪樨  (  祢 ᫮ final  Java  sealed  C#),  ⥯  ,   ਬ୮ ⮣  䥪  C++.
-,    ஢ ᨬ Widget  ⠯ 樨. ᫨ Widget ᫥  Timer,  । Timer   㯭  ६ 樨 Widget, ⮬ 䠩, ।騩 Widget, ⭮,  ᮤঠ ४⨢ #include "Timer.h".  㣮 ஭, ᫨ WidgetTimer 뭥  Widget,   Widget ⠢ ⮫쪮 㪠⥫  WidgetTimer, ⮣ Widget ᬮ     WidgetTimer; ⠪  室   䠩  Timer 㤥 ࠭.   ⥬ ⠪ ࠧ離   . ஡  樨 ᨬ⥩  ⠯ 樨 . ࠢ 31.
 㦥 ⬥砫,  ⮥ ᫥ 㤮 ० ᥣ ⮣,  । ந ᠬ 㦥   饭         ॡ  ८।    㠫 㭪権,  楯㠫쭮 ⭮襭  ⨬ ᠬ ࠦ  ᫮ < ࠧ>,  <ॠ ।⢮>.  ⠪ ਫ,   樨,  ⭮, 易  ⨬樥 ᯮ짮 ,  ⮥ ᫥ 뢠 ।⥫쭥 樨.
࠭ 砩 - ⢨⥫쭮 ࠭:    ,      .      ᪨ 童-,  㠫 㭪権 (᪮ 稥  㭪権 砥  㪠⥫ vptr   ꥪ - . ࠢ 7),  㠫  ᮢ (᪮  ⮬ 砥 ⮦   ⥫ 室  - . ࠢ 40). 楯㠫쭮, ꥪ ⠪  ᮢ    , ⮬     ࠭  .   孨᪨ 稭,   C++ ॡ, ⮡  ⮭ ꥪ   㫥 ࠧ, ⮬  ᫥ :

class Empty {}; //   , ⮬ ꥪ
//    
class HoldsAnInt { // ,  , 㦭 ⮫쪮  int
private:
int x;
Empty e; //    
};

뢠,  sizeof(HoldsAnlnt) > sizeof(int); 童  Empty  - .  設⢠ ஢ sizeof(Empty) 㤥 ࠢ 1, ⮬  ॡ C++  ⮬,     ꥪ⮢ 㫥 , 筮 㤮⢮ 砫 ⠢   (char)  ⠪ <⮩> ꥪ.  - 室 ࠢ (. ࠢ 50)    㦤  ,  HoldsAnInt, ⮬  ⭮,  ࠧ ꥪ⮢ HoldsAnInt 㢥  祬  char, ᪮॥ ᥣ,       ࠧ int.   ,   ஢, ந室  ⠪.
,  ⨫ , ,   㫥 ࠧ,  㯮 <⮭> ꥪ.  ࠭祭  ⭮  ⥬  ந ,  㭠᫥  , ᪮  㦥   <⮭묨>. ᫨  ᫥ Empty  ⮣, ⮡  ,

class HoldsAnInt: private Empty {
private:
int x;
};

  猪 㦨,  sizeof(HoldsAnlnt) = sizeof(int).   ⭮  ⨬ ⮣   (empty base optimization - EBO),   ॠ   ,   ஢. ᫨  ࠧࠡ뢠 ⥪, 짮⥫ ன ࠧ筮 ॡ ,    EBO 㤥 .    ,   饬 砥 ⨬ EBO ਬ ⮫쪮  筮 ᫥. 騥  C++ ࠢ ࠧ饭 ꥪ⮢   筮   ⠪ ⨬, ᫨ ந     .
 ࠪ⨪ <>   ᠬ   ᮢᥬ .     ᮤঠ ᪨ -童,    typedefbi, ᫥, ᪨ 童-,  㠫 㭪樨.  ⥪ STL   孨᪨  ᮢ,  ᮤঠ  童 (筮 typedef).    ⭮,  ⭮,   unary_function  binary_function,  筮 ᫥  ।塞 짮⥫ﬨ 㭪樮 ꥪ⮢.  ப ࠭ ॠ権 EBO ⠪ ᫥ । 㢥稢 ࠧ ந ᮢ.
 ୥  ᭮. 設⢮ ᮢ  , ⮬ EBO ।  㦨 ࠢ ⮬ ᫥.  ⮣,  設⢥ 砥 ᫥ ࠦ ⭮襭 <>,   ਧ ⮣,   ⮣ ᫥.  , ⠪  ⮥ ᫥ ࠦ ⭮襭 <ॠ ।⢮>,     , ⮬ ᯮ  ,  .
⮥ ᫥  ᥣ 뢠 ࠧ㬭 ⥣ ஥஢,       ᠬ,  易묨 ⭮襭 <>, 祬     㦤  㯥  饭 童 㣮,   ८।   ᪮쪮 㠫 㭪権 ᫥.    ⮬ 砥  ,  ⠭ ⮣ ᫥  樨   ॠ  ,   業 ண ᫮.   த뢠 室  ਬ ⮣ ᫥,    ,  ਡ   ⮨  ⮣,  ᬮ७  㣨 ୠ⨢  ᭨,   訩 ᯮᮡ ࠧ ⭮襭   ᠬ  襩 ணࠬ.

 ᫥ 
 ⮥ ᫥ 砥 <ॠ ।⢮>. 筮  ਠ 㦥 樨,    ਮ⠥ ,  ந  㦤  㯥  饭 童     ८। 㭠᫥ 㠫 㭪樨.
  ⫨稥  樨, ⮥ ᫥  ஢ ⨬ ⮣  .      ࠧࠡ稪 ⥪,  ६ ஢ ࠧ ꥪ⮢.



ࠢ 40: த뢠 室  ᯮ짮 ⢥ ᫥

  室  ⢥ ᫥ (multiple inheritance - MI), ᮮ⢮ ࠧࠡ稪  C++ ࠧ    .  ,  ࠧ 筮 ᫥ (SI) -  ,  ⢥ ᫥    . 㣨 ,  筮 ᫥ -   ᠬ  ,  ⢥  ⮨ 嫮.  ⮬ ࠢ  ࠥ ࠧ   窠 ७.
ࢮ,  㦭 ᭨  ᥡ  ⢥ ᫥,-    㭠᫥      (㭪樨, typedef  . .)  ᪮쪨  ᮢ.    稭 筮. ਬ:

class BorrowableItem { // ,   ⢮
//  ⥪
public:
void checkOut();
...
};
class ElectronicGadget {
private:
bool checkOut() const; // 믮 ᠬ஢, 頥
... // ਧ ᯥ譮 
};
class MP3Player: //  ⢥ ᫥ ( 
public BorrowableItem, // ⥪ ॠ 㭪樮쭮,
public ElectronicGadget // 室  MP3-஢)
{...} // ।   
MP3Player mp;
mp.checkout(); // 筮!  checkOut?

⬥⨬,   ⮬ ਬ 맮 㭪樨 checkOut 祭, ᬮ    㯭     㭪権. (checkOut    Borrowableltem     ElectronicGadget.)   ᮣ  ࠢ ࠧ襭  ॣ㦥 㭪権  C++: ० 祬 ஢ ࠢ 㯠, C++ 室 㭪,   ᮮ⢥ 맮.  ⮫쪮 ⮬ ஢, 㯭   室 㭪.   砥  ਠ 㭪樨 checkOut   ᮮ⢥ 맮,        室 , 祬 㣠.  ⠫ ,  ஢ન 㯭 ElectronicGadget::checkOut   室.
⮡ ࠧ 筮,   㪠   ,  㭪 㦭 맢:

mp.BorrowableItem::checkOut(); //   checkOut  㦭!

, 筮, ⠪    맢 ElectronicGadget::check-Out,  ⮣  訡 筮  : < ⠥ 맢  㭪-童>.
⢥ ᫥  砥 ᫥ , 祬    ,     ⠪   , ᮤঠ   ஢.   ਢ  <஬ ᫥>:
class File {...};
class InputFile: public File {...};
class OutputFile: public File {...};
class IOFile: public InputFile,
public OutputFile
{...};

直 ࠧ,   ந  ᫥,  ன     ந     (  ਢ ਬ:  File  IOFile  ன  १ InputFile, ⠪  १ OutputFile),  室 ⠫  ᮬ  ⮬,   -童   㡫஢  ꥪ  ⮫쪮 ࠧ, ᪮쪮  ⥩. ਬ, ।,    File  童 filename. 쪮  ⮣      IOFile?   ஭,  ᫥       ᢮  ᮢ, ᫥⥫쭮, ᥣ 㤥  童    fileName.  㣮 ஭,   ᪠뢠,  ꥪ IOFile  ⮫쪮   䠩, ⮬  fileName, ᫥㥬    ᮢ,   㡫஢.
C++  ਭ  ஭  ⮬ ᯮ.  ᯥ譮 ন  ਠ,   㬮砭 । 㡫஢. ᫨   ,   㦭, ᤥ , ᮤঠ騩  (  File), 㠫  ᮬ.  ⮣  ।⢥ ⮬  ᯮ짮 㠫쭮 ᫥:
class File {...};
class InputFile: virtual public File {...};
class OutputFile: virtual public File {...};
class IOFile: public InputFile,
public OutputFile
{...};

 ⠭⭮ ⥪ C++  宦 , ⮫쪮     蠡묨  뢠 basic_ios, basic_istream, basic_ostream  basic_iostream,   File, InputFile, OutputFile  IOFile.
 窨 ७ ४⭮, ⮥ ᫥ ᥣ   㠫. ᫨   뫠 ⢥ 窠 ७,  ࠢ 뫮  : 直 ࠧ  ⮬ ᫥ ᯮ 㠫쭮 ⮥ ᫥.  ᮦ, ४⭮ -  ⢥,  㦭 ਭ  . ⮡  㡫஢ 㭠᫥ 童,  室 ਡ  ਢ , - 祣 ࠧ ꥪ⮢ ᮢ, ᯮ ⢥ 㠫쭮 ᫥, 筮 뢠   ࠢ  砥,  㠫쭮 ᫥  ᯮ.   -童 㠫  ᮢ ⠪ , 祬   㠫  ᮢ. ⠫ ॠ樨   ,   ⠥ : 㠫쭮 ᫥ ॡ .
 室  ᯫ⭮    㣮 稭. ࠢ, ।騥 樠 㠫  ᮢ, ᫮  ⨢  ⠪ ,  ࠢ  㠫  ᮢ. ⢥⢥  樠 㠫쭮     ᠬ 쭨 ந   娨.  ᫥, : (1) , ᫥騥 㠫쭮   ॡ騥 樠樨,     ᢮ 㠫  , ᨬ  ⮣,      室  娨,  (2)      ந ,   ਭ  ᥡ ⢥⢥  樠 㠫 । ( , ⠪  ).
 ᮢ ⭮⥫쭮 㠫  ᮢ (  㠫쭮 ᫥) . -,  ਬ 㠫  ᮢ   ,   ⮬   ⥫쭠 ॡ.  㬮砭 ᯮ 㠫쭮 ᫥. -, ᫨    㠫  ᮢ  㤠, ࠩ  ࠧ   .   㤥   ࠭ ࠢ 樠樨 (, ,  ᢠ) ⠪ ᮢ.  䥩 Java . NET,     㠫  ᠬ C++,   ᮤঠ  .
 ᬮਬ ᫥騩 䥩  C++ (. ࠢ 31)  ஢ 䨧᪨ :

class IPerson {
public:
virtual ~IPerson();
virtual std::string name() const = 0;
virtual std::string birthDate() const = 0;
};

짮⥫ IPerson  ணࠬ஢  ନ 㪠⥫  뫮  IPerson, ᪮ ᮧ ꥪ ࠪ ᮢ 饭.  ᮧ ꥪ⮢, 묨  㫨஢  ꥪ⠬ IPerson, ᯮ 㭪樨-䠡ਪ (  . ࠢ 31),  ஦ ꥪ  ᮢ, ந  IPerson:

// 㭪-䠡ਪ  ᮧ ꥪ Person  㭨쭮
// 䨪   ; . ࠢ 18,
// 祬 頥 ⨯ -   㪠⥫
std::tr1::shared_ptr<IPerson> makePerson(DatabaseID personIdentifier);
// 㭪   䨪  짮⥫
DatabaseID askUserForDtabaseID();
DatabaseID id(askUserForDtabaseID());
std::tr1::shared_ptr<IPerson> pp(makePerson(id)); // ᮧ ꥪ,
// ন騩
// 䥩 IPerson
... // 㫨஢ *pp
// १ 㭪樨-童
// IPerson

  makePerson ᮧ ꥪ,   頥 㪠⥫? ᭮,    -  , 㭠᫥  IPerson,  makePerson  ⠭஢.
।,   뢠 CPerson.   ᮬ, CPerson  ।⠢ ॠ  㠫 㭪権, 㭠᫥  IPerson.    < >,   ᯮ짮 㦥 ⮢묨 ⠬,     ࠡ. ਬ, ।,  , ਥ஢ ⮫쪮     Person-Info ।⠢   室 CPerson:

class PersonInfo {
public:
explicit PersonInfo(DatabaseID pid)
virtual ~PersonInfo();
virtual const char *theName() const;
virtual const char *theBirthDate() const;
...
private:
virtual const char *valeDelimOpen() const; // 
virtual const char *valeDelimClose() const;
...
};

,    ,    ⮬,  㭪樨-童  const char*  ꥪ⮢ string.  ᫨ ⨭ 室, 祬    ?  㭪権-童    ,  १    㤮⢮⥫.
᪮  室  뢮,   PersonInfo  ஥஢       ࠧ ଠ,  뤥 砫     ᯥ樠묨 ப-ࠧ⥫ﬨ.  㬮砭 뢠騬  뢠騬 ࠧ⥫ﬨ 㦠  ᪮, ⮬ 祭  <Ring-tailed Lemur> 㤥 ଠ஢ ⠪:

[Ring-tailed Lemur]

뢠  䠪,   ᪮  ᥣ ਥ  짮⥫ PersonInfo,   ।ᬮ७ 㠫 㭪樨 valeDelimOpen  valeDelimClose, 騥 ந ᠬ  㣨 뢠騥  뢠騥 ப-ࠧ⥫. 㭪樨-童 PersonInfo 뢠  㠫 㭪樨   ࠧ⥫  頥 祭. , 㭪 PersonInfo::theName   룫拉 ᫥騬 ࠧ:

const char *PersonInfo::valueDelimOpen() const
{
return "["; // 뢠騩 ࠧ⥫  㬮砭
}
const char *PersonInfo::valueDelimClose() const
{
return "]"; // 뢠騩 ࠧ⥫  㬮砭
}
const char * PersonInfo::theName() const
{
// १ࢨ஢   頥 祭; ᪮ 
// ᪨, ⮬᪨ 樠 ﬨ
static char value[Max_Formatted_Field_Value_Length];
// ᪮஢ 뢠騩 ࠧ⥫
std::strcpy(value, valueDelimOpen());
  ப value 祭   name ꥪ ( ஦ -
 ९ !)
// ᪮஢ 뢠騩 ࠧ⥫
std::strcpy(value, valueDelimClose());
return value;
}

-  ⮢  ॢ訩 室  ॠ樨 PersonInfo::theName (ᮡ  ᠥ ᯮ짮 ᪮  䨪஢ ࠧ, ᭮  ९  ⥭樠묨 ஡  筮 । - . ࠢ 21),  ⠢    ஭  ।稬   祬: 㭪 theName 뢠 valueDelimOpen  祭 뢠饣 ࠧ⥫, ⠢塞  頥 ப, ⥬ 뢠     뢠 valueDelimClose.
᪮ valueDelimOpen  valueDelimClose - 㠫 㭪樨, 頥 १ theName   ⮫쪮  PersonInfo,    ᮢ, ந  .
 ࠧࠡ稪 CPerson   , ⮬ , ⥫쭮 ᬠਢ 㬥  㭪    IPerson,  㦨,  㭪樨 name  birthDate   ଠ஢ 祭,     ࠧ⥫. 㣨 ᫮, ᫨ 祫  Homer,  맮 㭪樨 name   <Homer>,   <[Homer]>.
  CPerson  PersonInfo   ⠪: PersonInfo 頥 ॠ  㭪権 CPerson.   ! ⠫ ,    ⭮襭 <ॠ ।⢮>, ,   , ⠪ ⭮襭  ।⠢  ᯮᮡ:   樨 (. ࠢ 38)  ⮣ ᫥ (. ࠢ 39).  ࠢ 39 ⬥祭,    饬 砥  ।⥫쭠,  ᫨ 㦭 ८। 㠫 㭪樨,  ॡ ᫥.   砥 CPerson  ८। valueDelimOpen  valueDelimClose - ,    樨  蠥.  祢 襭 - ਬ ⮥ ᫥ CPerson  PersonInfo, ,    ࠢ 39,  ॡ ᪮쪮  ࠡ.  ⠪  ॠ樨 CPerson ᯮ짮 ⠭ 樨  ᫥  楫 ८। 㠫 㭪権 PersonInfo.   ⠭   ⮬ ᫥.
 CPerson ⠪  ॠ 䥩 IPerson,   ⮣ ॡ ⮥ ᫥.    諨  ⢥ ᫥: ⠭ ⮣ ᫥ 䥩   ᫥ ॠ樨:

class IPerson { //  뢠 䥩,
public: //    ॠ
virtual ~IPerson();
virtual std::string name() const = 0;
virtual std::string birthDate() const = 0;
};
class DatabaseID {...}; // ᯮ ;
// ⠫  ⢥
class PersonInfo { //  ⮬   㭪樨,
public: // 騥  ॠ樨
explicit PersonInfo(DatabaseID pid) // 䥩 IPerson
virtual ~PersonInfo();
virtual const char *theName() const;
virtual const char *theBirthDate() const;
virtual const char *valeDelimOpen() const;
virtual const char *valeDelimClose() const;
...
};
class CPerson: public IPerson, private PersonInfo { // ᯮ
public: // ⢥
explicit CPerson(DatabaseID pid): PersonInfo(pid) {} // ᫥
virtual std::string name() const // ॠ樨
{ return PersonInfo::theName();} // 㭪権-童
//  䥩
// IPerson
virtual std::string birthDate() const
{ return PersonInfo::theBirthDate();}
private: // ८।
const char * valeDelimOpen() const { return "";} // 㭠᫥
const char * valeDelimClose() const { return "";} // 㠫
}; // 㭪権,
// 
// ப-ࠧ⥫

 樨 UML  襭 룫廊 ⠪:
ᬮ७ ਬ 뢠,  ⢥ ᫥    㤮,  .
,  ⢥ ᫥ -    㬥  ꥪ⭮-ਥ஢ 㬥ਨ.  ࠢ   ᫥  ᪮쪮 㤭    ਬ, ⮬ ᫨   ஥஢ ணࠬ     ᫥,     ⥭ ਠ  ⢥ ᫥, , ᪮॥ ᥣ, ।⥭ ᫥ ⤠ ࢮ 室. ᫨  ,  ⢥  ਠ  ॡ ਬ ⢥ ᫥,  ४  ᫥ 㬠 -  猪  ᯮᮡ  .    ६  ⢥ ᫥ -  ᠬ , ⮩  ᮯ஢  ࠧ㬭 ᯮᮡ ⨦ 楫.  ⠪    ਬ .   , ⥫쭮 㬠  ᫥⢨.

 ᫥ 
 ⢥ ᫥ ᫮ 筮.   ਢ  筮  室 ਬ 㠫쭮 ᫥.
  㠫쭮 ᫥ - ⥫  , ᭨ த⢨  ᫮ 権 樠樨  ᢠ.  ࠪ⨪  ࠧ㬭 ਬ,  㠫    ᮤঠ .
 ⢥ ᫥  .   業ਥ 砥  ⮣ ᫥ 䥩᭮   ⮣ ᫥ , 饣  ॠ樨.




 7
  饭 ணࠬ஢


砫쭮 蠡  C++   ⮣, ⮡  뫮 ॠ  ⭮⥫쭮 ⨯ ⥩: vector, list, map   .    ⥭  ࠡ  蠡 ⠫ 㦨     ᯮᮡ  ਬ. ⥩ 뫨  ᠬ  ᥡ,  饭 ணࠬ஢ -   ,  騩  ⨯ ꥪ⮢, 묨  㫨,-   . ਬࠬ ⠪ ணࠬ஢   STL, ⠪  for_each, find  merge.  筮 ⮣ ᭨,  堭 蠡 C++ ᠬ  ᥡ  設 ਭ:    ᯮ짮  ᫥  塞 祭.  ਢ  ⠯ணࠬ஢ 蠡: ᮧ ணࠬ,  ᯮ   C++   ᢮ ࠡ   砭 樨.    ⥩ -    ⮫ ⮣,   ᯮᮡ 蠡 C++. , ᬮ  ஬ ࠧࠧ ਬ,  ᭮ ணࠬ஢ 蠡  讥 ᫮  .    饭  .
  ⠢ ᥡ 楫 ᤥ   ᯥ  ணࠬ஢ 蠡, , ⠢  ,  ⠭  ࠧ  ⮬ .  ⮬    筮 ଠ樨  ⮣, ⮡ ࠧ ࠭  ।⠢  ணࠬ஢ 蠡 - ⮫쪮 ப, ᪮쪮  .



ࠢ 41:   ⮬,  ⠪  䥩  䨧  ⠯ 樨

  ꥪ⭮-ਥ஢ ணࠬ஢ ८  䥩  䨧  ⠯ ᯮ. ਬ, ᬮਬ ᫥騩 (᫥) :

class Widget {
public:
Widget();
virtual ~Widget();
virtual std::size_t size() const;
virtual void normalize();
void swap(Widget& other); // . ࠢ 25
...
};

 ⮫  ᫥ 㭪:

void doProcessing(Widget& w)
{
if(w.size() > 10 && w != someNastyWidget) {
Widget temp(w);
temp.normalize();
temp.swap(w);
}
}

    ᪠  ६ w  㭪樨 doProcessing:
 ᪮ ,  ६ w  ⨯ Widget,  w  ন 䥩 Widget.    筮 ᠭ ⮣ 䥩  室  (ਬ,  筮 䠩  Widget), ⮬  뢠   䥩ᮬ -  騬  室  ணࠬ.
 ᪮   㭪権-童 Widget  㠫묨,  맮  㭪権 ।⢮ w  ਬ஬ 䨧 ६ ᯮ: ⭠ 㭪,  㦭 맢, ।  ६ ᯮ  ᭮ ᪮ ⨯ w (. ࠢ 37).
 蠡  饭 ணࠬ஢ ਭ樯쭮 ⫨砥.  ⮬   䥩  䨧 ६ ᯮ த ⢮,    .    ।  室  䥩  䨧 ६ 樨. ⮡ ,   砥, ᬮ,  ந, ᫨  ॢ⨬ 㭪 doProcessing  蠡 㭪樨:

template<typename T>
void doProcessing(T& w)
{
if(w.size() > 10 && w != someNastyWidget) {
T temp(w);
temp.normalize();
temp.swap(w);
}
}

 ⥯  ᪠  ६ w  蠡 doProcessing?
  䥩,   ন ६ w, । ﬨ, 믮塞묨  w  蠡.   砥 ,  ⨯ ६ w (  T)  ন 㭪樨-童 size, normalize  swap;  ஢ ( ᮧ temp),  ⠪  ࠢ  ࠢ⢮ ( ࠢ  someNastyWidget).   㢨,    ᮢᥬ 筮,     筮. ,   ࠦ,    ४  ⮣, ⮡ 蠡 ஢, ।⠢ ᮡ  䥩,  ⨯ T  ন.
  ᯥ譮 맮 㭪権,    w, ⠪  operator> operator!=,  ॡ ⨧஢ 蠡.  ⨧ ந室  ६ 樨. ᪮ ⨧ 蠡 㭪権  ࠧ묨 蠡묨 ࠬࠬ ਢ  맮 ࠧ 㭪権,  뢠  䨧 ६ 樨.
 ᫨    짮 蠡, ࠧ  䨧 ६ ᯮ  䨧 ६ 樨    , ᪮   ࠧ  楤ன । ⮣,   ॣ㦥 㭪権 뢠 ( ந室  ६ 樨)  ᪨ 뢠  맮 㠫 㭪権 (஥ ந室  ६ ᯮ).  ࠧ  묨  묨 䥩ᠬ - ⨥, ࠪ୮ ⮫쪮  蠡, ⮬ ⠭    ஡.
 䥩 筮   ᨣ 㭪権,    㭪権, ⨯ ࠬ஢, 頥 祭  . . ,  䥩  Widget

class Widget {
public:
Widget();
virtual ~Widget();
virtual std::size_t size() const;
virtual void normalize();
void swap(Widget& other);
};

⮨  ,   㭪権 size, normalize  swap   ⨯  ࠬ஢, 頥 祭  ਧ ⠭⭮ (䥩 ⠪ 砥 㥬 ஬  ஢   ᢠ - . ࠢ 5).  ⠢ 䥩  室 ⠪ typedefbi.
 䥩 ᪮쪮 ⫨砥.     ᨣ 㭪権.  ⮣  ⮨  ४ ࠦ. ᬮਬ  ࠧ  ᫮  砫 蠡 doProcessing:

template<typename T>
void doProcessing(T& w)
{
if(w.size() > 10 && w != someNastyWidget) {
...

 䥩 T (⨯ ६ w)  ᫥騥 ࠭祭:
   ।⠢ 㭪-童   size,  頥 楫 祭.
   ন 㭪 operator!=,  ࠢ  ꥪ ⨯ T. (  ।,  someNastyWidget  ⨯ T.)
  ॣ㧪 ஢     ॡ   㤮⢮  易⥫쭮 浪. , T  ন 㭪-童 size,  ⮨ 㯮,   㭪   㭠᫥   .   㭪  易  楫᫥ ⨯.       ᫮ ⨯. -    易  ⨯,  ண । operator>! 㦭 , ⮡  頫 ꥪ ⠪ ⨯ X,    맢 operator>, ஬ । ࠬ ⨯ X  int (⮬  10  ⨯ int).  ⮬ 㭪 operator>    ਭ ࠬ, ⨯ ண  筮 ᮢ  X; 筮, ᫨ ⨯  ࠬ Y    ८ࠧ  ⨯ X!
筮  ॡ, ⮡ ⨯ T ন operator!=, 筮 㤥  ⮣, ⮡ 㭪 operator!= ਭ  ꥪ ⨯ X   ꥪ ⨯ Y. ᫨ T  ८ࠧ  X,  someNastyWidget  Y,  맮 operator!= 㤥 ४.
( :   ਭ    ॣ㧪 operator&&,  १ ன ᥬ⨪ ਢ ࠦ   㦥  樥,  祬- ᮢ襭 .)
 設⢠    㣮,   稭 뢠   䥩,   ᠬ  祣 譮   .  䥩 -    ४ ࠦ.   ᥡ ࠦ   ᫮묨,    ࠭祭 筮 祢.

if(w.size() > 10 && w != someNastyWidget)...

   ᪠  ࠭祭,  㭪ﬨ size, operator>, operator&&  operator!=,  ஢ ࠭祭 ᥣ ࠦ  楫 . ᫮  । if   㫥᪨ ࠦ, ⮬ ᨬ   ⨯ १ ᫥ (w.size() > 10 && w!= someNastyWidget)   ᮢ⨬  bool.     䥩,  蠡 doProcessing   ᢮ ࠬ ⨯ T. ஬ ⮣,  ࠡ doProcessing 室, ⮡ 䥩 ⨯ T ᪠ 饭   ஢,  ⠪ 㭪 normalize, size  swap.
࠭祭,  묨 䥩ᠬ  ࠬ 蠡, ⠪  ॠ,  ࠭祭,  묨 䥩ᠬ  ꥪ :  ,  㣨 ஢  ⠯ 樨.    ᯮ짮 ꥪ ᯮᮡ, ⨢騬  䥩ᠬ  ᮢ (⠪   ᪮),  筮 ⠪    ⠩ ᯮ짮  蠡 ꥪ,  ন騩  䥩, ண ॡ 蠡 ( ,   ᪮).

 ᫥ 
  ,  蠡 ন 䥩  䨧.
  ᮢ 䥩 ।     ࠧ ᨣ 㭪権. 䨧   ६ ᯮ - १ 㠫 㭪樨.
  ࠬ஢ 蠡 䥩   ᭮  ४ ࠦ. 䨧   ६ 樨 - १ ⨧  ࠧ襭 ॣ㧪 㭪権.



ࠢ 42: ᢮  祭 祢 ᫮ typename

:  ࠧ  <class>  <typename>  ᫥饬  蠡:

template <class T> class Widget; // ᯮ "class"
template <typename T> class Widget; // ᯮ "typename"

⢥: .   蠡  ࠬ ⨯, class  type-name  ᮫⭮    .  ணࠬ । ᥣ  class, ⮬   ᫮ . 㣨 ( ) । typename, ᪮    ⮬,  ࠬ஬  易⥫쭮   ⨯ .  ࠧࠡ稪 ᯮ typename,  ᪠  ⨯,  १ࢨ ᫮ class  砥,  ᪠ ⮫쪮 ⨯, ।塞 짮⥫.   窨 ७ C++, class  typename   ࠬ 蠡   筮    .
  ᥣ  C++ 祢 ᫮ class  typename .   易 ᯮ짮 typename. ⮡  -  , ਬ   ⨯ ,    뫠  蠡.
।,     蠡 㭪樨, ਭ饩  ⢥ ࠬ ᮢ⨬  STL-⥩, ᮤঠ騩 ꥪ,    ᢮ 稭 ⨯ int.  ।,   㭪  ⠥ 祭 ண .   祭 ᮤঠ⥫쭠 㭪,   ⮬   ॠ -檨.   㦥 ਫ,    㤥 ஢,    ⮬  ६ -    ⠪ 㯮,  :

template <typename C> // ⠥ ன
void print2nd(const C& container) //  ⥩
{ //  ४ C++!
if (container.size() >= 2) {
C::const_iterator iter(container.begin()); //  ,
// 㪠뢠騩  
// 
++iter; // ᬥ  ன
// 
int value = *iter; // ᪮஢   int
std::cout << value; //  int
}
}

 뤥  ⮩ 㭪樨   ६ - iter  value.  iter  C::const_iterator -    ࠬ 蠡 C.   蠡,    ࠬ 蠡, 뢠 ᨬ묨 . ᨬ      뢠  ᨬ . C::const_iterator -   ᨬ . ᪨    ᨬ  ⨯,    , ஥ ⭮  ⨯.
㣠 쭠 ६  print2nd - value -  ⨯ int,  int -  , ஥      ࠬ 蠡.   뢠 ᨬ묨.
 ᨬ    稭 㤭  ⠯ ᨭ⠪᪮  室 ⥪ ஬. ਬ, ।,   ॠ㥬 print2nd   㯮, ᠢ  砫 ⠪ :

template <typename C> // ⠥ ன  ⥩
void print2nd(const C& container) //  ४ C++!
{
C::const_iterator *x;
...
}

룫廊 ⠪,    x   ६ - 㪠⥫  C::const_iterator.   ⮫쪮 , ᪮  <>,  C::const_iterator  ⨯.  , ᫨   C  ᪨ 童    const_iterator  , ᫨ x 㤥  쭮 ६?  ⮬ 砥 ਢ   㤥   ६,   㬭 C::const_iterator  x!  ⭮,   ,   ᨭ⠪᪨ ஢ 室   C++      ਠ 室 ,  ᠬ 㬠襤.
  C 祣  ⭮,    㧭,   C::const_iterator ⨯  ,   ६ ࠧ 蠡 print2nd  祣  C  .  C++ ।ᬮ७ ࠢ, ࠧ饥  ।: ᫨ ᨭ⠪᪨  砥  ᨬ   蠡,  ।,     ⨯, ᫨ ⮫쪮   㪠  .  㬮砭  ᨬ    ⨯.  ᪫祭  ⮣ ࠢ,  ஬  ᪠  .
   , ᬮ   砫 print2nd:

template <typename C>
void print2nd(const C& container)
{
if (container.size() >= 2) {
C::const_iterator iter(container.begin()); // ।, 
... //    ⨯

   ᭮, 祬  ४ C++.  iter   ⮫쪮  砥, ᫨ C::const_iterator  ⨯,    ᮮ騫 C++  ⮬, ⮬ C++ ।,    ⠪. ⮡ ࠢ ,   ᮮ C++,  C::const_iterator -  ⨯.  ⮣  頥 祢 ᫮ typename ।⢥ । :

template <typename C> //  ४ ++
void print2nd(const C& container)
{
if (container.size() >= 2) {
typename C::const_iterator iter(container.begin());
...
}
}

饥 ࠢ : 直 ࠧ,   頥   ᨬ   蠡,   ।  ᫮ typename (᪮   ᪫祭).
 typename ᫥ ᯮ짮  䨪樨 ⮫쪮  ᨬ  ⨯;  㣨    ਬ.  ਬ 蠡 㭪樨,  ਭ  ⥩,    ⮣ ⥩:

template <typename C> // ᪠ typename (  "class")
void f(const C& container, // typename  ᪠
typename C::iterator iter); // typename ॡ

C    ᨬ  ⨯ (    祣-, ᨬ  ࠬ 蠡), ⮬   㦭 । ᫮ typename   ⥩,  C::iterator -   ᨬ  ⨯, ⮬ ।  ᫥ ⠢ typename.
 ࠢ <typename  ।  ᨬ  ⨯>  ᪫祭: typename   ।  ᨬ  ⨯  ᯨ᪥  ᮢ   䨪    ᯨ᪥ 樠樨 童. ਬ:

template <typename T>
class Derived: public Base<T>::Nested { // ᯨ᮪  ᮢ:
public: // typename  ᪠
explicit Derived(int x)
:Base<T>::Nested(x) // 䨪  
{ //  ᯨ᪥ 樠樨 童:
// typename  ᪠
typename Base<T>::Nested temp; // ᯮ짮 
... // ᨬ  ⨯  
} // 䨪 
... //   ᯨ᪥ 樠樨
}; // 童: typename 室

 ᮣᮢ ᪮쪮 ࠧࠦ,    ਮ⥭   ⠭  .
ᬮਬ   ਬ ᯮ짮 typename, ⮬      ॠ쭮 . ।,   襬 蠡 㭪樨,  ਭ ,  ⨬ ᤥ   - temp - ꥪ,     㪠뢠.   ᤥ ਬ୮ ⠪:

template <typename IterT>
void workWithIterator(IterT iter)
{
typename std::iterator_traits<IterT>::value_type temp(*iter);
...
}

 㣠   ࠦ std::iterator_traits<IterT>::value_type.   ᯮ ⠭ -ࠪ⨪ (traits) (. ࠢ 47). ,  C++  <⨯ ⮣,   㪠뢠 ꥪ ⨯ *IterT>.  ⮬ ।  쭠 ६ (temp) ⮣  ⨯,   ꥪ,   㪠뢠 IterT,  ⥬  樠஢ 祭,  ஥ 㪠뢠 iter. ᫨ IterT 㤥 ⨯ vector<int>::iterator,  temp 㤥  ⨯ int. ᫨  IterT 㤥 ⨯ vector<string>::iterator,  temp 㤥  ⨯ string. ᪮ std::iterator_traits<IterT>::value_type -   ᨬ  ⨯ (value_type   iterator_traits<IterT>,  IterT - ࠬ 蠡),   ।  ᫮ typename.
᫨  ⭮   ࠦ std::iterator_traits<IterT>::value_type, ।⠢,     . ᫨ ,   設⢮ ணࠬ⮢, ⠥,   ⠪   ࠧ ᫨, । ᥢ  ⮣ ⨯ ।⢮ typedef.   童 ᮢ-ࠪ⨪,   ⭮ value_type, (. ࠢ 47 ଠ  -ࠪ⨪), ਭ ᮣ襭, ᮣ᭮ ஬  typedef  ᮢ   童.  ࠧ, । 쭮 typedef 筮 룫廊 ⠪:

template <typename IterT>
void workWithIterator(IterT iter)
{
typedef typename std::iterator_traits<IterT>::value_type value_type;
value_type temp(*iter);
...
}

 ணࠬ⮢ ᥤ⢮ typedef  typename 砫 ࠧࠦ,   ᪮ ᫥⢨  ࠢ 饭   ᨬ  ⨯.  ᪮ ਢ몭.  ⮬       ᪨ 稭. 쪮 ࠧ  ⮢  std::iterator_traits<IterT>::value_type?
 ⢥ ⥫쭮 砭   㯮,     ⠨  ண 믮 ࠢ,  祢 ᫮ typename.  ਭ ,  ஬ typename ॡ,  ய饭;  ਭ ,  typename ,   ᪠;  (筮  ᠥ  ஢) ⢥࣠ typename  ⠬,   室.  ,  裡  typename  묨 ᨬ묨  ⨯   稭   祭 쥧 訡  ७ ணࠬ   .

 ᫥ 
   ࠬ஢ 蠡 祢 ᫮ class  typename 塞.
 ᯮ typename  䨪樨  ᨬ  ⨯, ᫨     ᯨ᪥  ᮢ   ⢥ 䨪    ᯨ᪠ 樠樨 童.



ࠢ 43: 室 ,      蠡  

।,   㦭  ணࠬ,  㤥 뫠 ᮮ饭 ᪮쪨 . 饭  ࠢ   ஢ ଥ, ⠪   ଥ ⮣ ⥪. ᫨  ६ 樨   筮 ଠ樨  । ⮣,  ᮮ饭   ࠢ  ,    ਡ  襭, ᭮  蠡:

class CompanyA {
public:
...
void sendClearText(const std::string& msg);
void sendEncryptedText(const std::string& msg);
...
};
class CompanyB{
public:
...
void sendClearText(const std::string& msg);
void sendEncryptedText(const std::string& msg);
...
};
... //   㣨 
class MsgInfo {...}; // , ᮤঠ騩 ଠ,
// ᯮ㥬  ᮧ
// ᮮ饭
template<typename Company>
class MsgSender {
public:
... // ,   . .
void sendClear(const MsgInfo& info)
{
std::string msg;
ᮧ msg  info
Company c;
c.sendClearText(msg);
}
void sendSecret(const MsgInfo& info) // 筮 sendClear,  뢠
{...} // c.sendEncrypted
};

 ணࠬ 㤥 ࠡ.  ।,    ⨬ ⮪஢  ଠ  ࠢ ᮮ饭.    , ᠢ ந , ,   , ࠧ㬭  ᤥ ᫥騬 ࠧ:

template <typename Company>
class LoggingMsgSender: public MsgSender<Company> {
public:
...
void sendClearMsg(const MsgInfo& info)
{
  ⮪ । ࠢ;
sendClear(info); // 맢 㭪   
//    㤥 ஢!
  ⮪ ᫥ ࠢ;
}
...
};

⬥⨬,  㭪, ࠢ ᮮ饭,  ந  뢠  (sendClearMsg), 祬   (sendClear).  襥 襭, ⮬  ⠪ ࠧ  室 ஡ ᮪ 㭠᫥  (. ࠢ 33),  ࠢ ᫮, 騥  ८। ᫥㥬 㠫 㭪権 (. ࠢ 36).     㤥 ஢,  ࠩ , ஬, ᮢ⨬  ⠭⮬.   ,  㭪樨 sendClear  .  ,   㭪 ।   ,    ⠭ ᪠  ⠬. ⠥  - 祬.
஡  ⮬,    砥 । 蠡  LoggingMsgSender,   ,    ᫥. ⭮,   MsgSender<Company>,  Company - ࠬ 蠡,   ⥭   ⨧樨 LoggingMsgSender.  ,  ⠪ Company,  ,  룫廊  MsgSender<Company>.  ⭮,   ᯮᮡ 㧭,     㭪 sendClear.
⮡ ᭥ ⢮,  祬 ᫮, ।,      CompanyZ, 뢠騩 ,  ⠨  ⮬, ⮡  ᮮ饭 ஢:

class CompanyZ { //    ।⠢
public: // 㭪樨 sendCleartext
...
void sendEncrypted(const std::string& msg);
...
};

騩 蠡 MsgSender  室  CompanyZ, ⮬    । 㭪 sendClear,   ꥪ⮢  CompanyZ   ᫠. ⮡   ஡,   ᮧ ᯥ樠஢  MsgSender  CompanyZ:

template <> //  ᯥ樠 MsgSender;
class MsgSender <CompanyZ> { // ⫨砥  饣 蠡
public: // ⮫쪮 ⢨ 㭪樨
... // sendCleartext
void sendSecret(const MsgInfo& info)
{...}
};

   ᨭ⠪  <template<>>  砫 । .  砥,     蠡,   ⮭ .  ᯥ樠஢  蠡 MsgSender,   ᯮ짮, ᫨ ࠬ஬ 蠡  CompanyZ. 뢠   ᯥ樠樥 蠡 : 蠡 MsgSender ᯥ樠஢  ⨯ CompanyZ,   ᯥ樠 ਬ,  ᪮  ⢥ ࠬ 㪠 ⨯ CompanyZ,  㣨 ᮡ ࠬ஢ 蠡    ਭ.
 ᯥ樠 蠡 MsgSender  CompanyZ, ᭮ ᬮਬ ந  LoggingMsgSender:

template <typename Company>
class LoggingMsgSender: public MsgSender<Company> {
public:
...
void sendClearMsg(const MsgInfo& info)
{
  ⮪ । ࠢ;
sendClear(info); // ᫨ Company == CompanyZ,
//  ⮩ 㭪樨  
  ⮪ ᫥ ࠢ;
}
...
};

 ᫥  ,      ᫠, ᫨  ᮬ  MsgSender<CompanyZ>, ⠪     㭪樨 sendClear. ⮬ C++ ⢥࣭ ⠪ 맮;  ,  蠡    ᯥ樠஢,  䥩, ।⠢塞 ⮩ ᯥ樠樥,    ⠪,   饬 蠡.  १  筮   㭠᫥   蠡  .  ஬ ᫥,   室  <ꥪ⭮-ਥ஢ C++>  <C++  蠡> (. ࠢ 1), ᫥ ⠥ ࠡ.
⮡ ࠢ , 㦭 - ⠢ C++ ⪠   < 뢠  蠡  >.  ⮣  ६ ᯮᮡ. -,  । 饭  㭪    㪠⥫ this:

template <typename Company>
class LoggingMsgSender: public MsgSender<Company> {
public:
...
void sendClearMsg(const MsgInfo& info)
{
  ⮪ । ࠢ
;
this->sendClear(info); // 冷! ।, 
// sendClear 㤥 㭠᫥
  ⮪ ᫥ ࠢ
;
}
...
};

-,  ᯮ짮 using-.  㦥 㦤  ⥬  ࠢ 33,  뫮 ᪠,  using-       묨  ந . ⮬   ९ sendClearMsg ᫥騬 ࠧ:

template <typename Company>
class LoggingMsgSender: public MsgSender<Company> {
public:
using MsgSender<Company>::sendClear; // ᮮ頥   ⮬, 
... // sendClear    
void sendClearMsg(const MsgInfo& info)
{
...
sendClear(info); // ଠ쭮, ।, 
... // sendClear 㤥 㭠᫥
}
...
};

 using- 㤥 ࠡ  , ⠪   ࠢ 33,  ᯮ   襭 ࠧ .  ஡   ⮬,        , 묨  ந ,   ⮬,     ⠭ ந      , ᫨ ⮫쪮       ⮬.
 ᫥ ᯮᮡ ⠢   ஢ -  㪠,  뢠 㭪 室   :

template <typename Company>
class LoggingMsgSender: public MsgSender<Company> {
pubilc:
...
void sendClearMsg(const MsgInfo& info)
{
...
MsgSender<Company>::sendClear(info); // ଠ쭮, ।, 
... // sendClear 㤥 㭠᫥
}
...
};

  ᯮᮡ 㦥 , ᪮쪮 ᫨ 뢠 㭪 㠫쭠,   䨪 ⪫砥 ᪮ 뢠.
 窨 ७  ,   室 :   ,   ᯥ樠 蠡   㤥 ন 䥩, ।⠢ 騬 蠡.  頭 -  ,  室   ६ ᨭ⠪᪮  ந 蠡 ,  LoggingMsgSender,  ᫨  頭  㤥 믮, ⨭ ᯫ뢥 . ਬ, ᫨  ணࠬ  ⠪ :

LoggingMsgSender<CompanyZ> zMsgSender;
MsgInfo msgData;
... //  info  msgData
zMsgSender.sendClearMsg(msgData); // 訡!  ᪮

 맮 sendClearMsg  ᪮, ⮬   ⮩ 窥  ,    -  ᯥ樠 蠡 MsgSender<CompanyZ>    㭪樨 sendClear,  sendClearMsg ⠥ 맢.
 ࠧ,    ⮬,    ࠢ 饭  童   - ࠭ (  । 蠡 ந )   (  蠡 ⨧ ।묨  蠡 㬥⠬). C++ ।⠥ ࠭ ⨪,  ⮬ ।,   ᮤন  ᮢ, ⨧㥬  蠡,  ⭮ 祣.

 ᫥ 
  蠡 ந ᮢ 뫪    蠡  ᮢ ⢫   䨪 <this->>, using-  ।⢮  㪠  .



ࠢ 44: 頩 ᨬ  ࠬ஢   蠡

 - 㤥 ᯮᮡ  ६   㡫஢ .  ⮣ ⮡   20 宦 ᮢ,      15 㭪権-童,  ࠥ ⥪  蠡  砥  ᣥ஢ 20  ᮢ   300 室  㭪権. (㭪樨-童 蠡 ᮢ  , ⮫쪮  ணࠬ   頥, ⮬  300 㭪権-童  ,  ᫨ 㤥   ᯮ짮.)  㭪権   ਢ⥫.  ᠭ ⢠ ⨯ 㭪権    蠡   ࠬ த  ⠫쭮.  ࠧ  ⥫쭠 孮?
: . ᫨   㤥 ⥫,  ᯮ짮 蠡  ਢ  ࠧ堭 .  뢠 㡫஢  筮 ணࠬ ,   ⮣  㣮.  १    室   ꥪ⭮  ⠭ ஬  殮. 襣  ⮬ , ⮬ 㦭 ,   ⠪ ⭮.
᭮ 㬥  ⮣ -  魮  稢 ,   ᠬ ⮩   祣 筮.  ᫨      ᠫ 蠡, ⠪   室  ﭭ.
   㭪  㦨,     ॠ樨  祬 ⫨砥  ॠ樨 㣮 㭪樨, ࠧ  㡫 ? 筮, .  ᪫砥     㭪権, 頥   ,    뢠   㭪. 묨 ᫮,     㭪樨  ।    ⫨ ⥩, ६頥 騥    㭪,  ⫨騥  ⠢  . 筮, ᫨     ,    ⮣   筮 ᮢ  ﬨ 㣮 ,   ⠭  㡫஢,   뭥 騥    ,  ⥬ ᯮ ᫥  樥 (. ࠢ 32, 38  39), ।⠢ 室    騬 ।⢠. ⫨騥  室 ᮢ   .
 ᠭ 蠡 믮 ⠪  ,  ᯮᮡ   㡫஢ .    ᮡ.  蠡  㡫஢  ࠧ: 㤭   ७    㭪  .  蠡  㡫஢  ᠥ  :  ⮫쪮   室  蠡, ⮬  㦭 ७஢, ⮡  室 ,   १ ⨧樨 蠡   㡫஢.
।, ਬ,     蠡    䨪஢ ࠧ, ,  ᥣ 祣, ন  饭 .

template<typename T, std::size_t n> // 蠡  ࠧ୮ n x n,
class SquareMatrix { // 饩  ꥪ⮢ ⨯ T;
public: // .  ଠ  ࠬ size_t
...
void invert(); // 饭   
};

 蠡 ਭ ࠬ ⨯ T,  ⠪ ࠬ ⨯ size_t,  騩 ⨯. ࠬ,  騥 ⨯, ᯮ ०, 祬 ࠬ-⨯,   ᮢ襭  ,    ਬ,    ⢥묨.
 ᬮਬ ⠪ :

SquareMatrix<double, 5> sm1;
...
sm1.invert(); // 맮 SquareMatrix<double, 5>::invert()
SquareMatrix<double, 10> sm2;
...
sm2.invert(); // 맮 SquareMatrix<double, 10>::invert()

  ⨧஢   㭪樨 invert.   , ⮬     ࠡ⠥  栬 5x5,  㣠 -  栬 10x10,   ᥬ ⠫쭮, ஬ ⠭ 5  10,  㭪樨 祬  ⫨.  - ᪨ ਬ ࠧ堭   १ ਬ 蠡.
  ,    㭪樨, ᮫⭮ ,  ᪫祭 ⮣,    ᯮ ⠭ 5,   㣮 - 10? ⢥,  ᮧ 㭪,  ਭ ࠬ,  ⥬ 뢠 ,  ࠧ ।  ⢥ ࠬ 5,  㣮 ࠧ - 10.  ࢠ ⪠ த     ॠ樨 蠡 SquareMatrix:

template<typename T> //  ,  騩
class SquareMatrixBase { //  ࠧ୮ 
protected:
...
void invert(std::size_t matrixSize); //   
... // ࠧ୮
};
template<typename T, std::size_t n>
class SquareMatrix: private SquareMatrixBase<T> {
private:
using SquareMatrixBase<T>::invert; // ⮡  ᮪ 
// ᨨ invert; . ࠢ 33
public:
...
void invert() {this->invert(n);} // ஥ 맮 ᨨ invert
}; //   
// .  - 祬
// ਬ "this->"

 , ࠬਧ  㭪樨 invert 室    - SquareMatrixBase.   SquareMatrix, SquareMatrixBase - 蠡,   ⫨稥  SquareMatrix,   ⮫쪮  ࠬ - ⨯ ꥪ⮢  ,    ࠬ size. ⮬  , ᮤঠ騥 ꥪ  ⨯,  ࠧ 騩  SquareMatrixBase. , ,   ࠧ ⢥  㭪樨 invert   .
祭 SquareMatrixBase::invert -   㡫஢   ந , ⮬ using- 饭  ᥪ protected,   public. ⥫ 室  맮 ⮩ 㭪樨 㫥, ᪮  ந   맮 invert ஥ (ࠨ  - . ࠢ 30).  ஥ 㭪 ਬ  <this->>, ⮬   ⨢ 砥,  ᫥  ࠢ 43,  㭪権  蠡   (SquareMatrixBase<T>)    ᮢ. ⬥⨬ ⠪,  ᫥ SquareMatrix  SquareMatrixBase - ⮥.  ࠦ  䠪,     ⮫쪮   楫 -  ॠ ந,   砥  楯㠫쭮 ⭮襭 <>  SquareMatrixBase  SquareMatrix ( ⮬ ᫥ . ࠢ 39).
    諮 ,    ஡,    ।⮨ . 㤠  SquareMatrixBase 㧭,   묨   ࠡ? ୮   ⭠  ࠬ,   㧭,  室 ᠬ  ⭮ ? -,  ⭮ ⮫쪮 ந .   ந   ।  ଠ , ⮡   믮 饭 ?
   ᯮᮡ -  ⥫ ࠬ  㭪 SquareMatrixBase::invert, ᪠, 㪠⥫  砫 ⪠ ,  ࠧ  .  㤥 ࠡ, , ᪮॥ ᥣ, invert -  ⢥ 㭪   SquareMatrix,    ᠭ ⠪,   㤥   ࠧ୮,  ७ᥭ   SquareMatrixBase. ᫨ ⠪ 㭪権 㤥 ᪮쪮, ᥬ   ,  室  .  ਤ     ࠬ,  ,   ⭮ । SquareMatrixBase     ଠ. - ࠢ쭮 .
 ୠ⨢ - ࠭ 㪠⥫     SquareMatrixBase.  ⠬   ࠭ ࠧ୮ . 砥 ⠪ :

template<typename T>
class SquareMatrixBase {
protected:
SquareMatrixBase(std::size_t n, T pMem) // ࠭ ࠧ୮
:size(n), pData(pMem){} //  㪠⥫   
void setData(T *ptr) { pData = ptr;} // ᢮ 祭 pData
...
private:
std::size_t size; // ࠧ୮ 
T *pData; // 㪠⥫   
};

  ந ᠬ ,  뤥 . ,  ⭮, ॠ,  ன   ࠭ אַ  ꥪ SquareMatrix:

template<typename T, size_t size>
class SquareMatrix: private SquareMatrixBase {
public:
SquareMatrix() // ।   ࠧ୮
:SquareMatrixBase<t>(n, data) {} //   㪠⥫  
...
private:
T data(n*n);
};

ꥪ ⠪ ⨯  㦤  ᪮ 뤥 ,     祭 訬.  ⮣  뤥      :

template<typename T, size_t size>
class SquareMatrix: private SquareMatrixBase {
public:
SquareMatrix() // ᢮ 㪠⥫  
:SquareMatrixBase<t>(n, 0), //    祭 null
pData(new T(n*n)) // 뤥    ,
{this->setDataPtr(pData.get();} // ࠭ 㪠⥫    ।
... //    
private:
boost::scoped_array<T> pData; //   boost::scoped_array
}; // . ࠢ 13

ᨬ  ⮣,  ࠭ ,  窨 ७ <ࠧ堭>   ,  ⥯  ( , ) 㭪樨-童 SquareMatrix 뢠  ஥묨 맮  ᨩ   ,  ⥯  ࠧ ᥬ 栬, ᮤঠ騬    ⮣  ⨯, ᨬ   ࠧ.    ६ ꥪ SquareMatrix ࠧ ࠧ஢ ⭮  ࠧ ⨯. ⮬, ᬮ     SquareMatrix<double, 5> SquareMatrix<double, 10>    ⥬  㭪ﬨ, ।묨  SquareMatrixBase<double>,   । 㭪樨, 饩 ࠬ ⨯ SquareMatrix<double, 10>, ꥪ ⨯ SquareMatrix<double, 5>. ,  ࠢ ?
, ,   ᯫ⭮.  㭪樨 invert  ⪮ <⮩>  室 ⥪ ࠧ୮ , ᪮॥ ᥣ,   ᣥ஢  䥪⨢ , 祬 ࠧ塞 㭪樨, ன ࠧ୮ ।  ⢥ ࠬ  ࠭  ᠬ ꥪ. ਬ, <> ࠧ୮   ⠭⮩ ६ 樨, ⠪     ਬ ࠧ  ⨬樨,  ⭮ ࠨ ⠭ ।⢥  設    ।⢥ ࠭.  㭪樨,  饩  ࠧ୮, ⠪   ன.
 㣮 ஭, 稥 ⮫쪮  ᨨ invert  ࠧ ࠧ୮⥩ 㬥蠥 ꥬ ᯮ塞 ,  ,  ᢮ ।, 㬥 ࠧ ࠡ祣 ⢠ ணࠬ   쭮 뫮   .   ᪮ ᯮ ணࠬ ⮫쪮,    䥪⨢  ࠢ  饩  ࠧ୮ ᨥ   墮 ஢.  䥪  騬? ⢥ ᯮᮡ  ⢥ - ஡  ਠ  ᫥   襩 ⭮ ଥ  ९१⨢묨 ࠬ .
㣮 䠪, 騩  䥪⨢,-  ࠧ ꥪ⮢. ᫨   㤥 ⥫,  ७ ᨬ  ࠧ୮ 㭪権     ਢ  㢥祭 ࠧ  ꥪ. ਬ,  ⮫쪮  ਢ    ꥪ SquareMatrix  㪠⥫      SquareMatrixBase, ᬮ   ,  ந   ⠪    .  㢥稢 ࠧ  ꥪ SquareMatrix,  ࠩ ,  ࠧ 㪠⥫.  ஢  ⠪, ⮡ 室   㪠⥫ ⯠,   ஬. ਬ, ᫨     饭 童  ࠭ 㪠⥫   ,   㥬 樥 (. ࠢ 22).  ⠪  ਢ  ᫮ ⬮ ࠢ ᠬ. ᫨    ࠭ 㪠⥫   ,         뤥 ᪨,  䨧᪨ 室  ꥪ ந  (  ).      ।, ᫥  㤠 㪠⥫? ⢥  ⠪  ,  祬 ७  , ⥬ ᫮  砥.    㬥७ 㡫஢     ᯠᥭ.
 ⮬ ࠢ  㦤 ⮫쪮 ࠧ堭  - ࠬ஢ 蠡,   ⨯,   ࠬ-⨯  ਢ  ⮬ . ਬ,   ଠ int  long      筮 ।⠢, ⮬ 㭪樨-童, ᪠,  vector<int> vector<long>,   묨 - ࠧ堭  ⮬ .  騪 ꥤ  ॠ樨 㭪権,   - , , ,  蠡, ⨧஢  int   long,   ଠ ਢ  ࠧ堭,   㣨 - . 筮  設⢥   ⨯ 㪠⥫   筮 ।⠢, ⮬ 蠡  ࠬࠬ 㪠⥫ ⨯ (ਬ, list<int*>, list<const int*>, list<SquareMatrix<long,3>*> . .)    ᯮ짮 騥 ॠ樨  㭪権-童.  ࠢ,  砥,  㭪樨-童,  ࠡ  ண ⨯묨 㪠⥫ﬨ (ਬ, T*)   ᥡ 뢠 㭪樨, ࠡ騥  ⨯஢묨 㪠⥫ﬨ (  void*).   ॠ ⠭⭮ ⥪ C++ ⠪ 室 ਬ  蠡 vector, deque  list   . ᫨  ᯮ ᭮ ࠧ堭  - ᯮ짮 蠡, , ⮨ 㯨 筮.

 ᫥ 
   ⢮ ᮢ  㭪権, ⮬  騩  蠡 ,     ࠬ஢ 蠡, ਢ  ࠧ堭 .
 堭 - ࠬ஢ 蠡,   ⨯,   ,  ࠬ 蠡 ࠬࠬ 㭪権  묨-童 .
 堭 - ࠬ஢-⨯  ࠭, ᯥ稢 騥 ॠ樨  砥,  蠡 ⨧ ⨯    ।⠢.



ࠢ 45: ࠡ뢠 蠡 㭪権-童 ⠪, ⮡  ਭ < ᮢ⨬ ⨯>

⥫㠫 㪠⥫ -  ꥪ,   ᥡ     㪠⥫,   㭪樮쭮,  ᫥  ।⠢. ਬ,  ࠢ 13 ,   ᯮ짮 ⠭  auto_ptr  tr1::shared_ptr  ⮬᪮ 㤠 ᪨ 뤥 ᮢ  㦭 ६.  STL-⥩஢  ᥣ  ⥫㠫묨 㪠⥫ﬨ. ⭮,   筮 㪠⥫  ,   㤥 ᤢ  ᫥騩 㧥 易 ᯨ᪠  १ 믮 樨 <++>,   ᯨ᪠ list::iterator ࠡ⠥  ⠪.
 祣  㪠⥫  - ⠪   প  ८ࠧ ⨯. ⥫  ꥪ ந ᮢ  ८ࠧ  㪠⥫  ꥪ  ᮢ, 㪠⥫  ⠭ ꥪ -  㪠⥫  ⠭  . . ਬ, ᬮਬ  ८ࠧ,   ந室  ஢ 娨:

class Top {...};
class Middle: public Top {...};
class Bottom: public Middle {...};
Top *pt1 = new Middle; // ८ࠧ Middle*  Top*
Top *pt2 = new Bottom; // ८ࠧ Middle*  Bottom*
Const Top *pct2 = pt1; // ८ࠧ Top*  const Top*

㫨஢ ⠪ ८ࠧ   ।塞 짮⥫ <⥫㠫> 㪠⥫  .  ⮣ 㦭, ⮡ ஢ ⠪ :

Template<typename T>
class SmartPtr {
public:
explicit SmartPtr(T *realPtr); // ⥫㠫 㪠⥫ 筮
... // 樠 ஥묨
}; // 㪠⥫ﬨ
SmartPtr<Top> pt1 = // ८ࠧ SmartPtr<Middle>
SmartPtr<Middle>(new Middle); //  SmartPtr<Top>
SmartPtr<Top> pt2 = // ८ࠧ SmartPtr<Bottom>
SmartPtr<Bottom>(new Bottom); // SmartPtr<Top>
SmartPtr<const Top> pct2 = pt1;

 ⨧樨  蠡  易 - ⭮襭, ⮬  ⠥,  SmartPtr<Middle> SmartPtr<Top>- ᮢ襭 ࠧ ,   易   㣮, 祬, ਬ, vector<float> Widget. ⮡  뫮 ⢫ ८ࠧ  ࠧ묨 ᠬ SmartPtr, 室   ᮮ⢥騩 .  ਢ  ਬ  । ᮧ  ꥪ ⥫㠫쭮 㪠⥫, ⮬  砫 ।稬  ᠭ ஢,    ᥡ ⠪,   㦭. 祢  ⮨  ⮬,    ࠧ  室 .  ਢ 娨   ᪮஢ SmartPtr<Top>  SmartPtr<Middle>  SmartPtr<Bottom>,  ᫨  饬  㤥 ७,  ਤ   ஢ ꥪ⮢ SmartPtr<Top>  㣨 ⨯ ⥫㠫 㪠⥫. ਬ, ᫨    ⠪ :

class BelowBottom: public Bottom {...};

 㦭 㤥 ঠ ᮧ ꥪ⮢ SmartPtr<Top>  SmartPtr<Below-Bottom>, , 祢,  ⥫  ࠤ ⮣ ஢ 蠡 SmartPtr.
 ਭ樯,    ࠭祭 ᫮ ஢. ᪮ 蠡   ⨧஢  樨 ࠭祭 ᫠ 㭪権, 宦,   㦥  -㭪  SmartPtr,  -蠡.  ਬ 蠡 㭪樨-童 ( 뢠 蠡 童),   蠡, 饣 㭪樨-童 :

template<typename T>
class SmartPtr {
public:
template<typename U> // 蠡 童
SmartPtr(const SmartPtr<U>& other); //  <饭
... //  ஢>
};

 ,     ⨯ T  U  SmartPtr<T>   ᮧ  SmartPtr<U>, ⮬  SmartPtr<T>  , ਭ騩 ࠬ ⨯ SmartPtr<U>.  , ᮧ騥  ꥪ  㣮, ⨯ ண  㣮 ⨧樥 ⮣  蠡 (ਬ, SmartPtr<T>  SmartPtr<U>),  뢠 饭묨 ࠬ ஢.
饭  ஢  ਢ  ਬ    䨪஬ explicit.   ᤥ ७. ८ࠧ ⨯  ஥묨 ⨯ 㪠⥫ (ਬ,  㪠⥫  ந   㪠⥫   ) ந室    ॡ ਢ, ⮬ ࠧ㬭   ⥫㠫 㪠⥫ 㫨஢ ⠪ .  ⮬   㪠 ᫮ explicit   饭  蠡.
  ᠭ  ࠧ, 饭  ஢  SmartPtr ।⠢ , 祬  㦭. ,  ⨬   ᮧ SmartPtr<Top>  SmartPtr<Bottom>,    ᨫ ᮧ SmartPtr<Bottom>  SmartPtr<Top>, ⮬   ⨢  ⮣ ᫥ (. ࠢ 32).  ⠪  ⨬ ᮧ SmartPtr<int>  SmartPtr<double>, ⮬     ८ࠧ int*  double*. - ࠧ   㧨 ࠧ 㭪権-童,  ᯮᮡ ஢  蠡.
।,  SmartPtr ᠭ  ࠧ auto_ptr  tr1::shared_ptr,   ।⠢ 㭪-童 get,  頥  ஥ 㪠⥫, ࠭饣  ꥪ <⥫㠫쭮> 㪠⥫ (. ࠢ 15),   ᯮ짮 ॠ樥 蠡 , ⮡ ࠭  ८ࠧ:

template<typename T>
class SmartPtr {
public:
template<typename U>
SmartPtr(const SmartPtr<U>& other) // 樨஢  ࠭
:heldPtr(other.get()) {...} // 㪠⥫ 㪠⥫, ࠭騬
//  㣮 ꥪ
T *get() const { return heldPtr;}
...
private: // ஥ 㪠⥫,
T *heldPtr; // ࠭騩  <⥫㠫쭮>
};

 ᯮ㥬 ᯨ᮪ 樠樨 童, ⮡ 樠஢ 童  SmartPtr<T> ⨯ T* 㪠⥫ ⨯ U*,  ࠭  Smart-Ptr<U>.   ⪮ ⮫쪮 ⮣,    ८ࠧ 㪠⥫ U*  T*,    ࠧ ,   㦭. ⠪, SmartPtr<T> ⥯  饭 騩 ,   ⮫쪮 ⮣,   । ࠬ ᮢ⨬ ⨯.
ᯮ짮 蠡 㭪権-童  ࠭稢 ࠬ.    ਬ ⠪ 㭪権 - প ᢠ. ਬ,  shared_ptr  TR1 (. ࠢ 13) ন ஢   ᮢ⨬ ஥ 㪠⥫, tr1::shared_ptr, auto_ptr  tr1::weak_ptr (. ࠢ 54),  ⠪ 稥  ࠢ   ᢠ ꥪ    ⨯, ஬ tr1::weak_ptr.  ਢ ࠣ ᯥ䨪樨 TR1  tr1::shared_ptr;  ,    ࠬ஢ 蠡 ᯮ 祢 ᫮ class,   typename.    ࠢ 42,   ⥪      .

template<class T> class shared_ptr {
public:
template<class Y> //  
explicit shared_ptr(Y *p); //  ᮢ⨬
template<class Y> // ஥ 㪠⥫,
shared_ptr(shared_ptr<Y> const& r); // shared_ptr,
template<class Y> // weak_ptr 
explicit shared_ptr(weak_ptr<Y> const& r); // auto_ptr
template<class Y>
explicit shared_ptr(auto_ptr<Y>& r);
template<class Y> // ᢠ
explicit shared_ptr& operator=(shared_ptr<Y> const& r); // 
template<class Y> // ᮢ⨬
explicit shared_ptr& operator=(auto_ptr<Y> const& r); // shared_ptr 
... // auto_ptr
};

     explicit,  ᪫祭 饭  ஢.  ,   ८ࠧ   ⨯ shared_ptr  㣮 ᪠,   ८ࠧ  ஥ 㪠⥫  㣮 <⥫㠫쭮> 㪠⥫  ᪠. ( ८ࠧ - ਬ,   ਢ ⨯ - ࠧ襭).  ᭮ ⬥,   । ꥪ auto_ptr  tr1::shared_ptr   ᢠ ࠬ 㪠뢠  䨪 const, ⮣  । ࠬ ⨯ tr1::shared_ptr  tr1::weak_ptr ⠭.  ᫥⢨ ⮣ 䠪,   ⫨稥  㣨 ᮢ ꥪ auto_ptr   ஢ (. ࠢ 13).
 㭪樨-童 - 㤥᭠ ,    ⬥ ᭮ ࠢ 몠.  ࠢ 5 ,     㭪権-童,    ஢ ⮬᪨,-   ஢   ᢠ.   tr1::shared_ptr  饭  ஢,  ᭮,   砥 ᮢ ⨯ T  Y ⨧ 饭  ஢   ᢥ  ᮧ <筮>  ஢. ⮬  ,  㤥    砥,   ꥪ tr1::shared_ptr   㣮 ꥪ ⮣  ⨯: ஢   ஢  tr1::shared_ptr  ⨧஢ 饭  ஢  蠡?
  ᪠, 蠡 童  ⬥ ᭮ ࠢ 몠,    ࠢ ᫥,  ᫨  ஢ 㦥,     ,   㤥 ᣥ஢ ⮬᪨.    饭  ஢ (蠡 童)  ।頥  ஬ 筮  ஢. ⮬ ᫨    ஫஢  ᯥ ஢ ⥬ ஢,     饭  ஢, ⠪  .   ᠥ ᢠ. ਢ ࠣ ।  tr1::shared_ptr,    :

template<class T> class shared_ptr {
public:
shared_ptr(shared_ptr const& r); //  ஢
template<class Y> // 饭
shared_ptr(shared_ptr<Y> const& r); //  ஢
shared_ptr& operator=(shared_ptr const& r); //  ᢠ
template<class Y> // 饭 
shared_ptr& operator=(shared_ptr<Y> const& r); // ᢠ
...
};


 ᫥ 
 ᯮ 蠡 㭪樨-童  樨 㭪権, ਭ  ᮢ⨬ ⨯.
 ᫨   蠡 饭 ஢ ஢  饭  ᢠ,  -०     ஢   ᢠ.



ࠢ 46: ।  蠡 㭪樨,  騥 童,  ⥫ ८ࠧ ⨯

 ࠢ 24 , 祬 ⮫쪮  ᢮ 㭪 ਬ  ८ࠧ ⨯  㬥⮢.  ⢥ ਬ 뫠 ਢ 㭪 operator*   Rational. ० 祬 த ⥭, ४  ᢥ  ਬ  , ⮬  ᥩ  ୥  ⮩ ⥬, ᬮॢ ,   , 䨪樨 ਬ  ࠢ 24. ⫨稥 ⮫쪮  ⮬,    Rational,  operator*   ᤥ 蠡:

template <typename T>
class Rational {
public:
Rational(const T& numerator = 0, // . ࠢ 20 - 祬
const T& denominator = 1); // ࠬ ।  뫪
const T numerator() const; // . ࠢ 28 - 祬
const T denominator() const; // १ 頥 
... // 祭,   ࠢ 3 -
// 祬  ⠭
};
template <typename T>
const Rational<T> operator*(const Rational<T>& lhs,
const Rational<T>& rhs)
{...}

   ࠢ 24,  ᮡࠥ ন ᬥ蠭 䬥⨪, ⮬ ⥫ , ⮡ ਢ   ஢.    客, ⮬     ࠢ 24 ࠡ⠫. ⢥ ⫨稥  ⮬,   Rational  㭪-童 operator* ⥯ 蠡:

Raional<int> oneHalf(1, 2); //  ਬ  ࠢ 24,
//  Rational - ⥯ 蠡
Ratinal<int> result = oneHalf * 2; // 訡!  

 䠪,     ,   ,   蠡 Rational  , ⫨饥   蠡 ᨨ.    ᠬ  ⠪.  ࠢ 24  ,  㭪  ⠥ 맢 (operator*, ਭ  ࠬ ⨯ Rational),     ⮬ 祣  ⭮. ⮬  ⠥ ,  㭪 㦭 ⨧஢ (  ᮧ)  蠡 operator*.  ,   ⮩ 㭪樨 operator*   ਭ  ࠬ ⨯ Rational<T>,   ⮣ ⮡ ந ⨧, 㦭 ᭨,  ⠪ T. ஡  ⮬,     ⮣ ᤥ.
 뢥 T,  ᬮ  ⨯ 㬥⮢, ।  맮 operator*.   砥  Rational<int> (⨯ ६ oneHalf)  int (⨯ ࠫ 2).  ࠬ ᬠਢ ⤥쭮.
뢮  ᭮ ⨯ oneHalf ᤥ .  ࠬ operator*   Rational<T>,   㬥, । operator* (oneHalf),  ⨯ Rational<int>, ⮬ T   int.  ᮦ, 뢥 ⨯ 㣮 ࠬ  ⠪ .   ⭮,  ⨯ ண ࠬ operator* ࠢ Rational<T>,  ன 㬥, । 㭪樨 operator* (᫮ 2),  ⨯ int.   ।,   T   砥?  ,   ᯮ -explicit ஬, ⮡ ८ࠧ 2  Rational<int> ⠪ ࠧ ᤥ 뢮,  T  int,    ⮣  ந室.   㯠 ⠪ ⮬,  㭪樨  ८ࠧ ⨯   ᬠਢ  뢮 㬥⮢ 蠡. . , ⠪ ८ࠧ ᯮ  맮 㭪権,  । ⥬,  뢠 㭪, 㦭 㡥,   . ⮡ 㡥  ⮬, 室 뢥 ⨯ ࠬ஢   ⥭樠쭮 室 蠡 㭪権 (⮡  뫮 ⨧஢ ࠢ 㭪).   ८ࠧ ⨯ ।⢮ 맮 ஢  뢮 㬥⮢ 蠡  ᬠਢ.  ࠢ 24  蠡  뫮, ⮬  ஡ 뢮 㬥⮢ 蠡  .       蠡  C++ (. ࠢ 1),   室   .
     뢮 㬥⮢ 蠡, ᯮ짮  㦥⢥ 㭪樨  蠡 .  砥,   Rational<T>   operator*  Rational<T>  㭪-㣠.  蠡 ᮢ 楤 뢮 㬥⮢   ⭮襭 ( ਬ ⮫쪮  蠡 㭪権), ⮬ ⨯ T ᥣ ⥭   ⨧樨 Rational<T>.  頥  ᮮ⢥饩 㭪樨 operator*  㣠  Rational<T>:

template <typename T>
class Rational {
public:
...
friend //  㭪樨
const Rational operator*(const Rational& lhs, // operator*
const Rational& rhs); // (஡ . )
};
template <typename T> // । 㭪樨
const Rational<T> operator*(const Rational<T>& lhs, // operator*
const Rational<T>& rhs)
{...}

 맮 operator*  㬥⠬ ࠧ ⨯ ᪮, ⮬    ꥪ oneHalf ⨯ Rational<int> ⨧  Rational<int>    㭪- operator*,  ਭ ࠬ Rational<int>. ᪮  㭪 (  蠡 㭪樨),    뢮 ⨯ ࠬ஢ 짮 㭪ﬨ  ८ࠧ (ਬ, -explicit ࠬ Rational) , ⠫ , 㬥 ࠧ  맮 operator*  ࠬࠬ ࠧ ⨯.
 ᮦ, ࠧ <㬥 ࠧ>   ⥪  ஭᪨ ⥭, ᪮    ,   . ᪮   ⮩ ஡,  ᭠砫   ᤥ  砭  ᨭ⠪, ᯮ㥬   㭪樨 operator*   Rational.
 蠡   蠡  ᯮ짮  ᮪饭 祭 蠡   ࠬࠬ, ⮬  Ratonal<T> ࠧ蠥   Rational  Ratonal<T>.   ਬ    ᪮쪮 ᨬ,    ᪮쪮 ࠬ஢  묨 ,   㬥 ࠧ 室   ६ ᤥ  ᭥.  ᯮ  ⮬, ⮬  operator*   ਭ騩  騩 Rational  Rational<T>.  ४⭮ 뫮   operator* ᫥騬 ࠧ:

template <typename T>
class Rational {
public:
...
friend
const Rational<T> operator*(const Rational<T>& lhs,
const Rational<T>& rhs);
...
};

  (  ⠪  ) ᯮ짮 ᮪饭 .
 ୥  ஡ . , ᮤঠ騩 맮  ࠬࠬ ࠧ ⨯, , ⮬   ,   ⨬ 맢  । 㭪 (operator*, ਭ ࠬ ⨯ Rational<int> Rational<int>),   㭪 ⮫쪮   Rational,   । ⠬.  楫 - ⠢ 蠡 㭪樨 operator*,  饩 童 , ।⠢  ।,  ⠪ ࠧ   . ᫨  塞 㭪 ᠬ⥫쭮 ( ⠪  ந室,   室  蠡 Rational),       ।.   砥    ਢ ।, ⮬ 騪    室.
⥩訩 ᯮᮡ ࠢ  - ꥤ ⥫ operator*   :

template <typename T>
class Rational {
public:
...
friend Rational operator*(const Rational& lhs, const Rational& rhs)
{
return Rational(lhs.numerator() * rhs.numerator(), //  
lhs.denominator () * rhs.denominator()); // ॠ,
} //  
//  ࠢ 24
};

-  ࠡ⠥  㦭: 맮 operator*  ࠬࠬ ᬥ蠭 ⨯ ,   ᪠. !
᭮ , 饥 ⮩ 孨: ᯮ짮 ⭮襭 㦥⢥   易      ⮩  . ⮡ ᤥ 묨 ८ࠧ ⨯   㬥⮢,  㦭 㭪,   童 (. ࠢ 24);  ⮣ ⮡  ⮬ ⨧ ࠢ쭮 㭪樨,  㦭    . ⢥ ᯮᮡ  ᢮ 㭪   - ᤥ  㣮 (friend).    . 筮? . 䥪⨢?  直 ᮬ.
   ࠢ 30, 㭪樨, ।  ,   ஥묨;  ᠥ  㭪権-㧥,  襩 operator*.   ஢ 䥪   ࠨ, ᤥ ⠪, ⮡ operator*   祣,  맮 ᯮ⥫쭮 㭪樨, ।  .   砥  ⮬  ᮡ 室, ⮬  㭪 operator*  ⠪ ⮨ ᥣ   ப,    ᫮ 㭪権  ⥫    ⥫. ⮬ ⮨     < 㣠, 뢠饣 ᯮ⥫ 㭪>.
 䠪,  Rational -  蠡 , 砥,  ᯮ⥫쭠 㭪 筮 ⠪ 㤥 蠡, ⮬   筮 䠩, ।饬 Rational, 筮 룫廊 ਬ୮ ⠪:

template <typename T> class Ratonal; // 
// 蠡 Rational
template <typename T> // 
const Rational<T> doMultiply(const Rational<T>& lhs, // 蠡
const Rational<T>& rhs); // ᯮ⥫쭮
// 㭪樨
template <typename T>
class Rational {
public:
...
friend
const Rational operator*( const Rational& lhs,
const Rational& rhs) //  
{ return doMultiply(lhs, rhs};} // ᯮ⥫
... // 㭪
};

  ॡ, ⮡  । 蠡 室   䠩, ⮬   ।     㭪 doMultiply.    ࠢ 30, ⠪ 蠡  易  ஥묨.     룫拉:

template <typename T> // । 蠡
const Rational<T> doMultiply( const Rational<T>& lhs, // ᯮ⥫쭮
const Rational<T>& rhs) // 㭪樨
{ //  筮 䠩
return Rational(lhs.numerator() * rhs.numerator(), //  室
lhs.denominator () * rhs.denominator());
}

筮,  蠡, doMultiply  ন 㬭 祭 ࠧ ⨯,      㦭.  뢠 ⮫쪮  operator*,  ᯥ稢 প ࠬ஢ ᬥ蠭 ⨯!  , 㭪 operator* ন  ८ࠧ ⨯, 室  ६ ꥪ⮢  Rational,  ⥬ ।   ꥪ ᮮ⢥饩 ⨧樨 蠡 doMultiply,   믮 ᮡ⢥  㬭.   ⢨,  ⠪ ?

 ᫥ 
    蠡 ,  ஬  㭪樨, 㦤騥   ८ࠧ ⨯   ࠬ஢, । ⠪ 㭪樨  㧥  蠡 .



ࠢ 47: ᯮ -ࠪ⨪  ।⠢ ଠ樨  ⨯

 ᭮ ⥪ STL ᮤন 蠡 ⥩஢, ஢  ⬮,       㦥 蠡.    뢠 advance.  advance ६頥 㪠    ﭨ:

template <typename T, typename DistT> // ६頥  iter
void advance(Iter T& iter, DistT d); //  d ⮢ ।
// ᫨ d < 0,  ६頥 iter
// 

楯㠫쭮 advance    ᠬ,  । iter+=d,  ⠪ ᯮᮡ advance    ॠ, ⮬  ⮫쪮   ந 㯮 ন  +=.    ஢ advance ॠ ⥬ ७ 樨 ++  - ஢ d ࠧ.
   ,   ⥣ਨ ஢  STL?  譮,  ⪨ .   ⥣਩ ஢, ᮮ⢥ ,   ন.   ( input iterators)  ६ ⮫쪮 ।,   蠣  ࠧ,    ⮫쪮 ,    㪠뢠   , 祬  祭    ࠧ.   㪠⥫ ⥭  室 䠩.  ⮩ ⥣ਨ ⭮   C++ iostream_iterator.  뢮 (output iterators) ஥ 筮,  㦠  뢮: ६ ⮫쪮 ।,   蠣  ࠧ,  뢠    ,  ஥ 㪠뢠, 祬   ⮫쪮  ࠧ.   㪠⥫   室 䠩.  ⮩ ⥣ਨ ⭮  ostream_iterator.  ᠬ <᫠> ⥣ਨ ஢. ᪮    뢮  ६ ⮫쪮  אַ ࠢ       㤠, 㤠 㪠뢠, 祬  ,  室 ⮫쪮  室 ⬮.
 魠 ⥣ ஢ ⮨  ࠢ ஢ (forward iterators).     ,      뢮,  ࠧ      ,  ஥ 㪠뢠,   ࠧ.    㤮묨  室 ⬮. STL  ।⠢ ॠ ࠢ 吝 ᯨ᪮,    ⥪   ( 筮 뢠 slist);  ⠪ ⥩஢  ࠢ묨.  ஢ ⥩஢  ⥪ TR1 (. ࠢ 54) ⠪   ࠢ묨.
㭠ࠢ  (bidirectional iterators)   㭪樮쭮 ࠢ ஢  ६饭 .   STL-⥩ list ⭮  ⮩ ⥣ਨ, ࠢ     set, multiset, map  multimap.
 魠 ⥣ ஢ -    ந 㯮 (random access iterators).  ⮣ ⨯   㭪 㭠ࠢ ஢ < 䬥⨪>,    ६饭 ।     ﭨ, 稢   ﭭ ६.  䬥⨪ 筠 䬥⨪ 㪠⥫,  㤨⥫쭮, ᪮   ந 㯮  ஥ 㪠⥫,  ஥ 㪠⥫   ᥡ    ந 㯮.   vector, deque  string  ࠬ  ந 㯮.
    ⥣਩ ஢ C++  ⠭⭮ ⥪  ᮮ⢥ <-> (tag struct):

struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag: public input_iterator_tag {};
struct bidirectional_iterator_tag: public forward_iterator_tag {};
struct random_access_iterator_teg: public bidirectional_iterator_tag {};

⭮襭 ᫥  ⨬ ࠬ ४⭮ ࠦ  ⨯ <> (. ࠢ 32): ୮,   ࠢ   ⠪ ࠬ   . . ᪮  㢨 ਬ ᯮ짮 ⠪ ᫥.
 ୥  樨 advance. ᪮  ࠧ ஢  ࠧ,   뫮  ॠ樨 advance ᯮ짮 <訬 騬 ⥫>,   ࣠ 横,  ஬  㢥稢  㬥蠥  .  ⠪ 室 ॡ   ६.   ந 㯮 ᯥ稢     ⥩  ﭭ ६, , 筮,   ⥫ ᯮ짮 ⨬ २⢮,  ᪮  .
 ⢨⥫쭮 ⥫  ॠ advance - ⠪:

template<typename IterT, typename DistT>
void advance(IterT& iter, DistT d)
{
if (iter  ஬  ந 㯮) {
iter += d; // ᯮ짮  䬥⨪
} //  ஢  ந 㯮
else {
if(d>=0) {while (d-) ++iter;} // 뢠 ++  -  横
else {while(d++) -iter;} //  ஢ 㣨 ⥣਩
}
}

  ⮣ 㦭 㬥 ।,   iter ஬  ந 㯮, ,  ᢮ ।, ॡ   ⮬,   ⨯ - IterT - ⭮  ⥣ਨ ஢  ந 㯮. 㣨 ᫮,  㦭   ଠ  ⨯.   ⮣  㦠 ࠪ⨪ (traits):  ଠ  ⨯  ६ 樨.
Traits -   祢 ᫮   ।।   C++;  孨  ᮣ襭, ஬ ᫥ ணࠬ.   ॡ, ।塞  ,  ,      ࠡ   ஥ ⨯,   ⨯, ।塞 짮⥫. ਬ,  맮  筮 㪠⥫ (⨯ const char*)  祭 ⨯ int  advance  ࠡ,   ,  孨 ࠪ⨪   ਬ   ஥ ⨯.
 䠪,  ࠪ⨪  ࠡ  ஥묨 ⨯, 砥,   뢠  ࠧ饭 ᯥ樠쭮 ଠ樨  ⨯, ⮬   㪠⥫  ଠ  ࠧ. ⮬ ࠪ᪠ ଠ  ⨯   譥  ⭮襭  ⨯. ⠭⭠ 孨 砥  饭   蠡,  ண    ᪮쪮 ᯥ樠権.  ஢  ⠭⭮ ⥪  蠡 iterator_traits:

template<typename IterT> // 蠡  ଠ樨
struct iterator_traits; //  ⨯ ஢

 , iterator_traits -  .  ᮣ襭 ࠪ⨪ ᥣ ॠ   . 㣮 ᮣ襭 砥  ⮬,  , ᯮ㥬   ॠ樨, 祬- 뢠 ᠬ- ࠪ⨪.
 iterator_traits ⮨  ⮬,    ⨯ IterT । ᥢ typedef iterator_category   iterator_traits<IterT>.  typedef  ⥣,  ன ⭮  IterT.
 ⮩   iterator_traits ⮨   ⥩. ࢠ -  ॡ, ⮡  ।塞 짮⥫ ⨯ ஢   ᥡ  typedef   iterator_category,   ᮮ⢥ -. ਬ,  deque  ࠬ  ந 㯮, ⮬  ஢ deque  룫拉 ਬ୮ ⠪:

template <:>
class deque {
public:
class iterator {
public:
typedef random_access_iterator_tag iterator_category;
...
};
...
};

  ⥩஢ list  㭠ࠢ묨, ⮬    룫廊 ⠪:

template <:>
class list {
public:
class iterator {
public:
typedef bidirectional_iterator_tag iterator_category;
};
...
};

 蠡 iterator_traits  ७ 室騩    typedef:

// iterator_category  ⨯ IterT -  ,  ᮮ頥   ᠬ IterT
// . ࠢ 42 ଠ  ᯮ짮 "typedef typename"
template <typename IterT>
struct iterator_traits {
typedef typename IterT::iterator_category iterator_category;
...
};

 ࠡ⠥  짮⥫᪨ ⨯,   室  ஢,   㪠⥫ﬨ, ⮬    㪠⥫  묨 typedef. ⮬  ன  蠡 iterator_traits ॠ প ஢,  㪠⥫ﬨ.
 ⮩ 楫 iterator_traits ।⠢  ᯥ樠 蠡  ⨯ 㪠⥫. ⥫  ᥡ    ந 㯮, ⮬  iterator_traits   㪠   ⥣:

template <typename IterT> // 筠 ᯥ樠 蠡
struct iterator_traits<IterT*> //  ஥ ⨯ 㪠⥫
{
typedef random_access_iterator_tar iterator_category;
...
};

   ,  ஥  ॠ -ࠪ⨪:
 ஢ ଠ  ⨯,   ⥫  ᤥ 㯭 (ਬ,  ஢ -   ⥣).
    祭 ⮩ ଠ樨 (ਬ, iterator_category).
 ।⠢ 蠡    ᯥ樠権 (ਬ, iterator_traits),  ᮤঠ ଠ  ⨯,    ন.
 蠡 iterator_traits,-  ᠬ  std::iterator_traits, ⮬     ⠭⭮ ⥪ C++,-   筨  ᥢ  advance:

template<typename IterT, typename DistT>
void advance(IterT& iter, DistT d)
{
if (typeid(typename std::iterator_traits<IterT>::iterator_category)==
typeid(std::random_access_iterator_tag))
...
}

룫廊 ,    ᮢᥬ ,  㦭. -,  ஡  樨,    ᬮਬ  ࠢ 48;  ⠭   㭤⠫쭮 ⥫⢥.  IterT ⥭  ⠯ 樨, ⮬ iterator_traits<IterT>::iterator_category ⠪   ।  ६ 樨.  । if   ६ ᯮ. 祬   ६ ᯮ  ⠪,   ᤥ  ६ 樨?    ६  ࠧ㢠 ᯮ塞 .
  㦭 - ⠪  ᫮  (ਬ, । if..else)  ⨯,  﫠   ६ 樨.  ,  C++  室  ।⢠.    ,  ॣ㧪.
  ॣ㦠  㭪 f,  㪠뢠 ࠬ ࠧ ⨯  ࠧ ᨩ.  뢠 f,  롨ࠥ  室  ॣ㦥 ᨩ, ᭮뢠  । 㬥. ,  , : <᫨    ᥣ ᮮ⢥ । ࠬࠬ, 맮 ; ᫨  室 㣠  - ⠭  ,  ⠪ >. ? ᫮   ⨯  ६ 樨. ⮡ ⠢ advance ࠡ 㦭  ࠧ, ᫥ ᥣ  ᮧ  ᨨ ॣ㦥 㭪樨,   ⢥ ࠬ     ꥪ iterator_category ࠧ ⨯.    㭪樨 doAdvance:

template<typename IterT, typename DistT> // ᯮ짮 
void doAdvance(IterT& iter, DistT d, // ॠ 
std::random_access_iterator_tag) // ஢
{ //  ந 㯮
iter += d;
}
template<typename IterT, typename DistT> // ᯮ짮 
void doAdvance(IterT& iter, DistT d, // ॠ 
std::bidirectional_iterator_tag) // 㭠ࠢ
{ // ஢
if(d >= 0) {while(d-) ++iter;}
else {while (d++) -iter;}
}
template<typename IterT, typename DistT> // ᯮ짮
void doAdvance(IterT& iter, DistT d, //  ॠ
std::input_iterator_tag) //  ஢
{ // 
if(d < 0) {
throw std::out_of_range("⥫쭮 ࠢ"); // . 
}
while (d-) ++iter;
}

᪮ forward_iterator_tag ᫥ input_iterator_tag,   do-Advance  input_iterator_tag 㤥 ࠡ   ࠢ묨 ࠬ.  ⥫ 㬥   ᫥  ࠧ묨 ࠬ iterator_tag. ᪨  㬥    ⮣ ᫥:       ,  㤥 ࠡ ⠪   ந   ᮢ.
䨪 advance ᪠  ⥫, ⠪  ⥫ 祭 ᤢ  ஢  ந 㯮  㭠ࠢ ஢,    ।, ᫨  ⠥ ᤢ  ⥫쭮 ﭨ    ࠢ . 樨,   ஢,  ।,  d -  ⥫쭮, ⮬ 室  祭  横,   <>  , ᫨  । ⥫쭮 祭.  , ਢ ,   ਠ,  ஬  ⮣ 㦤 ᪫祭.  ॠ樨 ४.  ப⨥ । :    ।᪠,  ந.
 ࠧ ॣ㦥 ᨨ doAdvance, 㭪樨 advance ⠥ ⮫쪮 맢 , ।  ⢥ ⥫쭮 ࠬ ꥪ, ᮮ⢥騩 ⨯ ⥣ਨ , ⮡   ਬ 堭 ࠧ襭 ॣ㧪  맮 ࠢ쭮 ॠ樨:

template <typename IterT, typename DistT>
void advance(IterT& iter, DistT d)
{
doAdvance( // 맢 
iter, d, // doAdvance
typename // ᮮ⢥
std::iterator_traits<IterT>::iterator_category() // ⥣ਨ
); //  iter
}

 ⮣ -  㦭 ᯮ짮 -ࠪ⨪:
   ॣ㦥 <ࠡ> 㭪権  蠡 㭪権 (ਬ, doAdvance),  ⫨ ࠬ஬-ࠪ⨪.   㭪  ᮮ⢥⢨  । ࠪ⨪.
  <> 㭪  蠡 㭪樨 (ਬ, advance),  뢠 ࠡ稥 㭪樨, । ଠ, ।⠢ ᮬ-ࠪ⨪.
-ࠪ⨪ ப ᯮ  ⠭⭮ ⥪. ,  iterator_traits,  iterator_category, ।⠢    ଠ樨   (  ᯮ value_type; ࠢ 42  ਬ  ਬ).   char_traits,  ᮤন ଠ  ᨬ ⨯,  numeric_limits,  ࠭ ଠ  ᫮ ⨯, ਬ   ᨬ 祭  . .  numeric_limits  ᡨ  ⮫, ᪮ 蠥 ᮣ襭,  ᮮ⢥⢨  묨  ᮢ-ࠪ⨪  稢  <traits>,    祣  , ਤ ᬨ.
 ⥪ TR1 (. ࠢ 54)  楫   ᮢ-ࠪ⨪,  ।⠢ ଠ  ⨯,  is_fundamental<T> ( T - ஥ ⨯), is_array<T> ( T - ⨯ ᨢ)  is_base_of<T1,T2> (    T1 ⥬ ,   T2,    ᮬ). ᥣ TR1   ⠭⭮ C++  50 ᮢ-ࠪ⨪.

 ᫥ 
 -ࠪ⨪  㯭 ଠ  ⨯  ६ 樨.  ॠ  ਬ 蠡   ᯥ樠権.
  ⠭  ॣ㧪 -ࠪ⨪  ஢ ⨯  ६ 樨.



ࠢ 48:  ⠯ணࠬ஢ 蠡

⠯ணࠬ஢ 蠡 (template metaprogramming - TMP) -   ᠭ ᭮  蠡 ணࠬ  C++, ᯮ塞  ६ 樨.   㬠  ⮬: 蠡 ⠯ணࠬ -  ணࠬ, ᠭ  C++,  ᯮ   C+ +.  TMP-ணࠬ 蠥 ᯮ,  १ - ࠣ   C++, ⨧஢  蠡,-   筮.
᫨    ࠧ   㡨 , ,  筮 殮 㬠  .
C++  ।砫  ⠯ணࠬ஢ 蠡,    ,  孮 TMP 뫠   砫 90- ,   ⮫쪮 , , ⭮,   ᠬ ,   ⠭ ⥪  祭 ७, 騥 ࠡ  TMP. , TMP 뫮  ,   ਤ㬠. ।⢠, 騥  ᭮ TMP,   C++   蠡. 㦭 뫮 ⮫쪮, ⮡ - ⨫,     ᯮ짮 ⥫   ࠧ.
孮 TMP   २⢠. -,    ⠪ ,  묨 ᯮᮡ ᤥ 뫮  㤭   . -, ᪮ 蠡 ⠯ணࠬ ᯮ  ६ 樨 C++,   ६  ࠡ  ⠤ ᯮ  ⠤ 樨.  ⭮,  訡,  筮 ᯫ뢠  ६ ᯮ,  뫮  㦨  樨. 㣮 २⢮ -  ,  ணࠬ C++, ᠭ  ᯮ짮 TMP,  ᤥ 䥪⨢묨    ᫠: ⭮ ᯮ塞,  த⢨, ॡ .   ᪮  ࠡ ७  ⠤ 樨, , 祢,    ६.  樨 ணࠬ,   ਬ 孮 TMP,  ॡ   ६, 祬  樨  ணࠬ, ᠭ  ਬ TMP.
ᬮਬ ᥢ 蠡 㭪樨 advance, ।⠢  . 227 (. ࠢ 47; ,     ࠢ ᥩ, ᪮   ।,        ਠ).  뤥  ⮬ ࠣ , ᠭ  ᥢ:

template<typename IterT, typename DistT>
void advance(IterT& iter, DistT d)
{
if (iter  ஬  ந 㯮) {
iter += d; // ᯮ짮  䬥⨪
} //  ஢  ந 㯮
else {
if(d>=0) {while (d-) ++iter;} // 뢠 ++  -  横
else {while(d++) -iter;} //  ஢ 㣨 ⥣਩
}
}

  ᯮ짮 typeid, ⮡  ᥢ ॠ .   㤥 襭 <ଠ>  C++ ᯮᮡ -  ࠡ 믮  ६ ᯮ:

template<typename IterT, typename DistT>
void advance(IterT& iter, DistT d)
{
if (typeid(typename std::iterator_traits<IterT>::iterator_category)==
typeid(std::random_access_iterator_tag))
iter += d; // ᯮ짮 ⨢ 䬥⨪
} //  ஢  ந 㯮
else {
if(d>=0) {while (d-) ++iter;} // 뢠 ++  -  横
else {while(d++) -iter;} //  ஢ 㣨 ⥣਩
}
}

 ࠢ 47 ⬥祭,  室, ᭮  typeid,  䥪⨢, 祬  ᯮ짮 ᮢ-ࠪ⨪, ᪮  ⮬ 砥: (1) ஢ઠ ⨯ ந室  ६ ᯮ,    ६ 樨,  (2) , 믮騩 ஢ ⨯,   祭  ᯮ塞 ணࠬ. ᪨  ਬ 뢠,  孮 TMP  ஦  䥪⨢ ணࠬ  C++, ⮬  ࠪ⨪ -     砩 TMP. ,  ࠪ⨪   ᫥ ࠦ if:else  ६ 樨.
 㦥 ⬥砫 ,    孮 TMP  ᤥ , 祬 <ଠ> C++,  advance   権 ⮣ ⢥ত.  ࠢ 47 㯮  ⮬,  ᭮  typeid ॠ advance  ਢ  ஡  ६ 樨,    ਬ ⠪ 樨:

std::list<int>::iterator iter;
...
advance(iter, 10); // ᤢ iter  10 ⮢ ।
//  ᪮  ਢ
//  ॠ樨

ᬮਬ  advance,  㤥 ᣥ஢  ⮣ 맮. ᫥ ⠭ ⨯ iter  10  ⢥ ࠬ஢ 蠡 IterT  DistT  稬 ᫥饥:

void advance(std::list<int>::iterator& iter, int d)
{
if (typeid(std::iterator_traits<std::list<int>::iterator>::iterator_category)==
typeid(std::random_access_iterator_tag))
iter += d; // 訡!
}
else {
if(d>=0) {while (d-) ++iter;}
else {while(d++) -iter;}
}
}

஡  뤥 ப,  砥  +=.   砥  ⠥ ᯮ짮 +=  ⨯ list<int>::iterator,  list<int>::iterator -  㭠ࠢ  (. ࠢ 47), ⮬   ন +=.  += ন ⮫쪮   ந 㯮.  ,    ⠥ ᯮ ।, ᮤঠ饥 +=, ⮬   list<int>::iterator ஢ઠ  ਢ祭 typeid   믮 ᯥ譮,  - 易 ࠭஢,   室  ४⥭,  ᫨    ᯮ,  <iter += d> - ४   砥,  iter   ஬  ந 㯮. 襭   ᭮ 孮 TMP ।,    ࠧ ⨯ 뭥ᥭ  ࠧ 㭪樨,    ᯮ ⮫쪮 樨, ਬ  ⨯,    ᠭ.
뫮 ,  孮 TMP ।⠢ ᮡ  設 ਭ,    筮    ᫥. ᯮ TMP,    ६, 믮 横,   뢠 㭪樨  . .  ⠪ 樨 룫 ᮢ襭 , 祬    <ଠ쭮> C++. ਬ,  ࠢ 47 ,   TMP ᫮ । if:else ࠦ   蠡   ᯥ樠権.  ⠪ 樨   <TMP ஢ ᥬ>.  ⥪  ࠡ  TMP (ਬ, MPL  Boost - . ࠢ 55) ।  ᮪஢ ᨭ⠪,   ⠪  ਭ  <ଠ> ++.
⮡   TMP  㣮 , ᬮਬ,  ⠬ 룫 横. 孮 TMP  ।⠢  横᪨ 権, ⮬ 横    ४ᨨ. (᫨   祭 㢥७  ४ᨥ, ਤ ᢮   ०, 祬 㯠  ᯮ짮 TMP.  TMP -   㭪樮 ,   ⠪ 몮 ४ -  ,  ⥫  ਪ᪮ - - ꥬ ਭ.)   ४-  ᮢᥬ 筠, ᪮  ॠ樨 横 TMP  ४ᨢ 맮 㭪権,   ४ᨢ ⨧樨 蠡.
 ணࠬ <Hello World>  TMP  ᫥ 䠪ਠ  ६ 樨. 筮, ,   <Hello World>,  ࠧ ࠦ,     樨  ⥩ 몠. ᫥ 䠪ਠ   TMP ᢮  ᫥⥫쭮 ४ᨢ ⨧権 蠡. ஬ ⮣,    ᯮᮡ ᮧ  ᯮ짮 ६  TMP. :

template<unsigned n> // 騩 砩: 祭 Factorial<n>- 
struct Factorial { // ந n  Factorial<n-1>
enum { value = n*Factorial<n-1>::value };
};
template<> //  砩: 祭 Factorial<0>-
struct Factorial<0> { //  1
enum { value = 1 };
};

 ⠪ 蠡 ⠯ணࠬ ( ᠬ   ⢥ 蠡 㭪 Factorial),  砥 祭 䠪ਠ n,   Factorial<n>::value.
᪠    ⠬,  ⨧ 蠡 Factorial<n> 뫠  ⨧ 蠡 Factorial<n-1>.   类 ४ᨢ ணࠬ,   ᮡ 砩, ४騩 ४.   砥  ᯥ樠 蠡 Factorial<0>.
 ⨧ 蠡 Factorial  ன struct,     ᯮ <  ᫥> (. ࠢ 2)   ६ TMP   value.  ६ value ࠭ ⥪饥 祭 䠪ਠ. ᫨   TMP 뫨 騥 横,  祭 value 﫮    樨 横.  ᪮  TMP  横  ४ᨢ ⨧ 蠡,   ⨧ 砥 ᢮ ᮡ⢥  value,  祭  ᮮ⢥ <樨 横>.
ᯮ짮 Factorial  ᫥騬 ࠧ:

int main()
{
std::cout << Factorial<5>::value; // ⠥ 120
std::cout << Factorial<10>::value; // ⠥ 3628800
}

᫨  室 ᠭ ਥ , ,  ⠫   ॢ饭  ⠯ணࠬ 蠡. ᫨    蠡, ᯥ樠樨, ४ᨢ ⨧樨,   ᫥  室   த Factorial<n-1>::value  뢠   ࣠, ⠫ ,   ଠ ணࠬ C++.
筮, 蠡 Factorial  ⠪     TMP,  <Hello World> -   筮 몠 ணࠬ஢. ⮡ , 祬  TMP ⮨ ,  ।⠢ ᥡ, 祣     ⮩ 孮.   ਬ:
 ᯥ祭 ४⭮  ७.     ਫ , ⮡  ७ (ਬ, , ﭨ, ६  . .) ࠢ쭮 ⠫. ᢠ ६, ।⠢饩 , 祭 ६, ।⠢饩 ᪮,-  訡,   ६ ﭨ  ६ ६  ᢠ १ ६ ᪮ ࠢ쭮. ᯮ TMP,  ᯥ ( ६ 樨),   樨  ७  ணࠬ  ४, ᨬ  ⮣, ᪮쪮 ᫮ ᫥. ( ਬ ⮣,   ᯮ짮 TMP  ࠭ ⨪ 訡.)   ᯥ⮬ ⠪ ᯮ짮 TMP   প ᫥ ஡ ⥯.   ⮬, ⮡ ஡ ᮪頫  ६ 樨,   ⮡   ⢥न, ਬ,   ६  ⥯ 1/2 -    ᠬ,   ६  ⥯ 4/8.
 ⨬ 権  栬.  ࠢ 21 ,   㭪樨,  operator*,    ꥪ,   ࠢ 44 ।⠢  SquareMatrix, ⮬ ᬮਬ ⠪ :

typedef SquareMatrix<double, 10000> BigMatrix;
BigMatrix m1, m2, m3, m4, m5; // ᮧ 
... //  ᢮  祭
BigMatrix result = m1 * m2 * m3 * m4 * m5; // ᫨ ந

᫥ result <ଠ> ᯮᮡ ਢ  ᮧ  ६ ,     맮 operator*.  ⮣, ᨬ 樨 㬭 ஦ ᫥⥫쭮   横  ⠬ .  ਬ । 蠡 孮, ᭮ 易  TMP  稢襩  蠡 ࠦ (expression templates),   ᮧ ६ ꥪ⮢  ꥤ 横, 祬     ਢ  짮⥫᪮ .  १ ணࠬ ॡ    믮 ⥫쭮 ॥.
  ᯥ樠஢ ॠ権 ୮ ஥஢.  ஥஢,  Strategy (. ࠢ 35), Observer, Visitor  . .,   ॠ  ᯮᮡ. ᯮ ᭮  TMP 孮, 뢠 ஥஢  ᭮ ⨪ (policy-based design),  ᮧ 蠡, ।⠢騥 ᨬ ஥ 襭 (<⨪>),    ᮮ⢥騬 ࠧ ᪮஢  ஦ ॠ権 ୮   . ਬ,  孨 ਬ﫠  ⮣, ⮡  ᪮쪨 蠡, ॠ ࠧ筮  <⥫㠫> 㪠⥫, த ( ६ 樨)   ⥭ ࠧ ⨯ <⥫㠫> 㪠⥫.  १ 饭, 室饣  ࠬ ਢ ணࠬ 権,  ਬ ୮ ஥஢  <⥫㠫> 㪠⥫,  孮   ᭮ ⠪ 뢠 ஦饣 ணࠬ஢ (generative programming).
孮 TMP ।祭   . ਬ塞   ᨭ⠪ ⨢  祢,  ন騩 㬥਩  ࠧ. (⫠稪  蠡 ⠯ணࠬ?  ᬥ訫, ࠢ!) ᪮  ᯮ⥫ ,  ࠢ⥫쭮 ,  ਬ塞   ᮣ襭   ᯥਬ⠫ ࠪ.    襭 䥪⨢   ७   ⠤ ᯮ  ⠤ 樨   ⥫,   ࠧ , ஥ 㤭   ॠ  ६ ᯮ, ⠪ 쬠 ਢ⥫쭮.
প TMP . ⭮,  ᫥饩 ᨨ C++ 㤥 ॠ  প ⮩ 孮,  TR1  㦥 ஢ (. ࠢ 54). 砫  , 饭 ⮩ ⥬,  ଠ  TMP  Internet ⠭  . , TMP   ⠭  ࠢ ࠧ,    ணࠬ⮢ (ᮡ ࠧࠡ稪 ⥪)   猪   .

 ᫥ 
 ⠯ணࠬ஢ 蠡  ७  ࠡ  ⠤ ᯮ  ⠤ 樨.   ⮣  ࠭ 㦨 訡   ந⥫쭮 ணࠬ.
 孮 TMP   ᯮ짮  樨   ᭮ 樨 ⨪,  ⠪ ⮡ ।  , ४⭮  । ⨯ .




 8
ன new  delete


  ,  ᫨⥫ । ᭠ ஥ প <ᡮન > (, ਬ, Java . NET), 筮 室 C++  ࠢ    ᪮쪮 ॢ訬.   ࠧࠡ稪, ᮧ騥 ॡ⥫  ᠬ ਪ ணࠬ, 롨 C++  ⮬,    ࠢ  .  ࠧࠡ稪 ,  ᯮ    ணࠬ,  ࠧࠡ뢠 ᮡ⢥ 楤 ।  ᢮   楫  ᨬ쭮  ந⥫쭮 ( 窨 ७  ६, ⠪  ॡ ) ᢮ ⥬.
 ⮣ 㦭 ,  ࣠ 楤 ࠢ   C++.  ⮩ ⥬  饭  .  ᭮   -  楤 뤥  ᢮  ( new  delete),  ᯮ⥫쭠 ஫ ⢮ ࠡ稪 new - 㭪樨,  뢠,  new   㤮⢮   뤥 .
 ࠢ   筮 । 易 ⥫ ᫮,  騥   ⥬, ᪮ <> -  㥬  ,   ஬   ᨭ஭஢.   ࠢ  饩     ᯮ짮 㥬 ᪨ .  ⥬ ᥣ ࠦ ணࠬ⮢, ࠧࠡ뢠  ணࠬ.  ࠢ쭮 ᨭ஭樨, ⢨  ஢  ⬠  ⥫쭮 ஥஢  楫 ।饭 ६ 㯠, 饭  楤ࠬ ࠡ     ਢ  ०  , ࠢ 祩.  ⮣ ⮡ ﭭ    ⮩ ᭮,    ⮬ ⮫쪮   ।,   㤥   ⮬  ⥭ ⠫쭮  .
    ⮬,   new  delete ਬ ⮫쪮  뤥  ᢮  ꥪ⮢.   ᨢ 뤥 operator new[]  ᢮ operator delete[] (   <[]>    㭪権). ᫨   ७ ⨢,   ᪠   new  delete ᠥ ⠪ new[]  delete[].
 , ⬥,   砥 STL-⥩஢ 뤥    ࠢ ꥪ-।⥫, 樨஢  ᠬ ⥩ࠬ,    new  delete.  ⮩  祣    ।⥫   STL.



ࠢ 49:    ࠡ稪 new

  new   㤮⢮   뤥 ,  㦤 ᪫祭. -  頫 㫥 㪠⥫,       ⠪  㯠.   ⮫  ⠪ ॢ訬 ,   ⫮  㦤   ࠢ.
० 祬 㤨 ᪫祭  ⢥   㤮⢮   뤥 ,  new 뢠 । 짮⥫ 㭪, 뢠 ࠡ稪 new (new-handler). ( ᠬ    ᮢᥬ ⠪. 쭮  new ᪮쪮 ᫮. ஡ ᠭ  ࠢ 51.) ⮡  㭪, ࠡ뢠 墠 ,  뢠 set_new_handler - ⠭  㭪,   筮 䠩 <new>:

namespace std {
typedef void (*new_handler)();
new_handler set_new_handler(new_handler p) throw();
}

 , new_handler -  typedef  㪠⥫  㭪,  祣  ਭ   頥,  set_new_handler - 㭪,  ਭ  頥 new_handler ( throw()    set_new_handler -  ᯥ䨪 ᪫祭;  ,  ᮮ頥,  㭪  㦤  ᪫祭,   ᠬ   ᪮쪮 ᭥; ஡ . ࠢ 29).
ࠬ set_new_handler -  㪠⥫  㭪,  operator new 뢠   뤥 襭 . 頥 set_new_handler 祭 - 㪠⥫  㭪,  ᯮ짮  ⮩ 楫 । 맮 set_new_handler.
ᯮ set_new_handler ᫥騬 ࠧ:

// 㭪,    맢, ᫨ operator new
//   뤥 
void outOfMem()
{
std::cerr << " 㤮⢮   뤥 \n";
std::abort();
}
int main()
{
std::set_new_handler(outOfMem);
int *pBigDataArray = new int[100000000L];
...
}

᫨ operator new   뤥   ࠧ饭 100 000 000 楫 ᥫ, 㤥 맢 㭪 outOfMem,  ணࠬ , 뤠 ᮮ饭  訡. (, 㬠,  , ᫨    ᪨ 뤥  ६ 뢮 ᮮ饭  訡  cerr:)
 operator new   㤮⢮   ,  㤥 뢠 ࠡ稪 new   ,    㬥  筮 . , ਢ騩   맮,   ࠢ 51,   ⠪ ᮪஢ ᠭ 筮, ⮡ ᤥ 뢮  ⮬,  ࠢ쭮 ஥஢ 㭪-ࠡ稪 new   -   ᫥饣 ᯨ᪠:
  ⥫ .  १ ᫥ ⪠ 뤥   operator new   ᯥ譮.   ᯮᮡ ॠ ⠪ ⥣ - 뤥 让    砫 ࠡ ணࠬ,  ⥬ ᢮   ࢮ 맮 ࠡ稪 new.
 ⠭ 㣮 ࠡ稪 new. ᫨ ⥪騩 ࠡ稪   뤥 , , ,  ⥭ - 㣮,  ᬮ  ᤥ. ᫨ ⠪,  ⥪騩 ࠡ稪  ⠭  ᥡ 㣮 (맢 set_new_handler).  ᫥騩 ࠧ,  operator new   ࠡ稪, 㤥 맢 ᫥ ⠭. ( ⢥ ୠ⨢ ࠡ稪   ᮡ⢥ , ⮡  ᫥饬 맮 ᤥ - 㣮.  ⮣ , ਬ, ⥬ 䨪樨  ᪨, ।  ࠭⢥    ,    .)
  ࠡ稪 new,   । 㫥 㪠⥫ set_new_handler. ᫨ ࠡ稪  ⠭,  operator new ࠧ 㤨 ᪫祭  㤠筮 ⪥ 뤥 .
 㤨 ᪫祭 ⨯ bad_alloc  ண ⨯, 㭠᫥  bad_alloc.  ᪫祭  墠뢠 operator new, ⮬  ࠭  ⮣ ,  뫠 襭 .
   ࠢ - 筮 맢 abort  exit.
 ਠ 롮 ᯥ稢     ॠ樨 㭪権-ࠡ稪 new.
 ࠡ 訡  뤥    㣨 ᯮᮡ, 騬   ।塞 ꥪ:

class X {
public:
static void outOfMemory();
...
};
class Y {
public:
static void outOfMemory();
...
};
X *p1 = new X; // ᫨ 뤥   㤠,
// 맢 X::outOfMemory
Y *p2 = new Y; // ᫨ 뤥   㤠,
// 맢 Y::outOfMemory

++  ন ᯥ   ࠡ稪 new,     㦤  .   ॠ ⠪  ᠬ⥫쭮.  ⮣     । ᮡ⢥  set_new_handler  operator new. ।   㭪 set_new_handler   짮⥫  ࠡ稪 new   (筮 ⠪ ,   set_new_handler ⠭  ࠡ稪 new). ਭ騩  operator new ࠭,   뤥   ꥪ⮢ ⮣   쭮 ࠡ稪 new 㤥 ᯮ짮 ,  ।   .
।,   ࠡ 訡 뤥    Widget.  㭪,  㤥 뢠,  operator new   뤥 筮   ꥪ Widget, ⮬   ᪨ 童 ⨯ new_handler  ࠭ 㪠⥫  ࠡ稪 new  .  Widget 㤥 룫拉 ਬ୮ ⠪:

class Widget {
public:
static std::new_handler set_new_handler(std::new_handler p) throw();
static void *operator new(std::size_t size) throw(std::bad_alloc);
private:
static std::new_handler currentHandler;
};

᪨ 童    ।  ᠬ  (᫨ ⮫쪮   ⠭ 楫 - . ࠢ 2), ⮬:

std::new_handler Widget::currentHandler = 0; // 樠஢ 㫥
//  䠩 ॠ樨 

㭪 set_new_handler   Widget ࠭ ।  㪠⥫  ୥  㪠⥫  㭪, ⢮ ࠭.   㯠  ⠭⭠  set_new_handler:

static std::new_handler set_new_handler(std::new_handler p) throw()
{
std::new_handler oldHandler = currentHandler;
currentHandler = p;
return oldHandler;
}

     operator new   Widget.
1. 맢 ⠭ set_new_handler, 㪠  ⢥ ࠬ 㭪-ࠡ稪 訡   Widget.  १ ࠡ稪 new   Widget 㤥 ⠭  ⢥ 쭮.
2. 맢  operator new  ॠ쭮 뤥 . ᫨ ந 訡,  operator new 맮 ࠡ稪 new, ਭ騩 Widget, ᪮  㭪 뫠 ⠭  ⢥ 쭮 ࠡ稪. ᫨    祬  ਢ,   operator new 㤨 ᪫祭 bad_alloc.  ⮬ 砥 operator new   Widget  ⠭ 室 ࠡ稪 new,  ⥬ ࠭ ᪫祭. ⮡ ࠭஢,  室 ࠡ稪 ᥣ ⠭,  Widget ࠪ     ᫥ ᮢ ࠢ 13  ᯮ짮 ࠢ ᠬ ꥪ⮢  ।饭 祪.
3. ᫨  operator new  ﭨ 뤥 筮   ꥪ Widget,  operator new  Widget 頥 㪠⥫  뤥 .  ꥪ, ᠬ⥫쭮 ࠢ饣  ࠡ稪 new, ⮬᪨ ⠭   ࠡ稪,   ⠭ । 맮 operator new  Widget.
 ᬮਬ,    ࠧ  C++. 筥  , ࠢ饣 ᠬ,   ᮤন 祣, ஬ ᭮ 権, 㥬  RAII: 墠   ६ ஢ ꥪ  ᢮   㭨⮦ (. ࠢ 13):

class NewHandlerHolder {
public:
explicit NewHandlerHolder(std::new_handler nh) //  ⥪騩
:handler(nh) {} // ࠡ稪 new
~NewHandlerHolder() // ᢮ 
{ std::set_new_handler(handler);}
private:
std::new_handler handler; //  
NewHandlerHolder(const NewHandlerHolder&); // ।
NewHandlerHolder& // ஢
operator=(const NewHandlerHolder&); // (. ࠢ 14)
};

  ॠ  new  Widget ᮢᥬ ⮩:

void Widget::orerator new(std::size_td size) throw(std::bad_aloc)
{
NewHandlerHolder // ⠭ ࠡ稪
h(std::set_new_handler(currentHandler)); // new   Widget
return ::operator new(size); // 뤥  
// 㤨 ᪫祭
} // ⠭ 
// ࠡ稪 new

짮⥫  Widget ਬ  ।⢠ ᫥騬 ࠧ:

void outOfMem(); //  㭪樨,  㦭
// 맢, ᫨ 뤥 
//  Widget  㤠
Widget::set_new_handler(outOfmem); // ⠭ outOfMem  ⢥
// ࠡ稪 new  Widget
Widget *pw1 = new Widget; // ᫨ 뤥   㤠,
// 뢠 outOfMem
std::string *ps = new std::string; // ᫨ 뤥   㤠,
// 뢠  ࠡ稪 new
// (᫨ )
Widget::set_new_handler(0); // ⬥ ࠡ稪 new
Widget *pw1 = new Widget; // ᫨ 뤥   㤠,
// ࠧ  㦤 ᪫祭 (
// ࠡ稪 new ᥩ )

, ॠ騩  奬,     (ᨬ  ), ⮬ ࠧ㬭 뫮  ୮ ᯮ짮   㣨 . ⮩ ᯮᮡ ᤥ  - ᮧ <ᮥ塞>  ,    ,  ।祭  ⮣, ⮡   㭠᫥ -⢥ ।⢮,   砥 ᯮᮡ ⠭ ᯥ   ࠡ稪 new. ⥬ ॢ    蠡, ⮡  ந    ࠧ  .
 ⠪ 室 ਭ     ᠬ ᫥ 室  㭪樨 set_new_handler  operator new,  蠡  ࠭,     㤥 ᮡ⢥ 童  currentHandler.  ᫮,   룫廊  . ᪨ ⢥ ⫨稥  ,  ⥯  㯥  :

template<typename T> // <ᮥ塞>   
class NewHandlerSupport { // প ᯥ筮  
public: // 㭪樨 set_new_handler
static std::new_handler set_new_handler(std::new_handler p) throw();
static void *operator new(std::size_t size) throw(std::bad_alloc);
... // 㣨 ᨨ  new - . ࠢ 52
private:
static std::new_handler currentHandler;
};
template<typename T>
std::new_handler
NewHandlerSupport<T>::set_new_handler(std::new_handler p) throw()
{
std::new_handler oldHandler = currentHandler;
currentHandler = p;
return oldHandler;
}
template<typename T>
void *NewHandlerSupport<T>::operator(std::size_t size)
throw(std::bad_alloc)
{
NewHandlerHolder h(std::set_new_handler(currentHandler);
return ::operator new(size);
}
// currentHandler    樠 祭 null
template<typename T>
std::new_handler NewHandlerSupport<T>::currentHandler = 0;

 ⨬ 蠡   প set_new_handler  Widget 祭 : Widget  ᫥  NewHandlerSupport<Widget>. (   ࠢ,    ஡ ,   ந室.)

class Widget: public NewHandlerSupport<Widget> {
... //  ࠭,   樨
}; // set_new_handler  operator new

 ,  㦭 ᤥ   Widget, ⮡ ।⠢ ᯥ   ࠡ稪 set_new_handler.
  ,  ᯮ  䠪,  Widget ᫥  New-HandlerSupport<Widget>? ᫨ ⠪,   ᯮ⢮ ᨫ,   ,  NewHandlerSupport   ᯮ ᢮ ࠬ ⨯ T.   㦤  . ⢥,   㦭,-  ⤥쭠  NewHandlerSupport,  筥  ᪨ 童 currentHandler   , ந  NewHandlerSupport. ࠬ 蠡 T  ⫨砥    㣮. 堭 蠡 ⮬᪨   currentHandler   ⨯ T,  ண  ⨧.
  ᠥ ⮣,  Widget ᫥ 蠡  ,  ਭ Widget  ࠬ ⨯,  㣠,  ⮫쪮 砫  ਢ.  ࠪ⨪  祭 㤮 孨,  ᮡ⢥ , ஥ ࠦ  䠪,    ,      ࠧ,    ⢥.  뢠  쥧 ४ᨢ 蠡  (curious recurring template pattern - CRTP). ⭮ ᫮.
  㡫 ,  ன ᠫ,   뫮     <   >, ⮬  Widget ᫥ NewHandler-Support<Widget>   : < - Widget,    ᫥  NewHandlerSupport  Widget>.   ⠭ 짮 ।   (  ᠬ),  ᫨ 㬠  CRTP   ᯮᮡ ᪠ <ᤥ   >,   㤥    ᫥ 蠡.
,  NewHandlerSupport,   ᯥ   ࠡ稪 new   , ஬  㦭.  ᫥ ᮥ塞  ਢ  ⢥ ᫥,  ० 祬 ⠢   , , , ⮨  ࠢ 40.
 1993  C++ ॡ, ⮡  new 頫 㫥 㪠⥫, ᫨   뤥 㦭 .   operator new  ⮬ 砥  㦤 ᪫祭 bad_alloc.  ஬ ꥬ   C++  ᠭ  ⮣,   ⠫ ন  ᯥ䨪.   ⠭⨧樨 C++  ⥫ ⬥  , ᭮  ࠢ  null, ⮬ 뫮 襭 ।⠢ ୠ⨢  operator new, ᯥ稢騥 ࠤ樮   饭 null  㤠.      <nothrow> ( 㦤騥 ᪫祭), ⮬    ᯮ ꥪ nothrow (।   <new>)  窥,  ᯮ new:

class Widget {...};
Widget *pw1 = new Widget; // 㦤 bad_alloc, ᫨
// 뤥   㤠
if(pw1 == 0) ... //  ஢ઠ 
//  㤠筮
Widget *pw2 = new (std::nothrow)Widget; // 頥 0, ᫨ 뤥
//   㤠
if(pw2 == 0) ... //  ஢ઠ 
//  ᯥ譮

 㦤騩 ᪫祭  new ।⠢   ࠭⨨ ⭮⥫쭮 ᪫祭, 祬    .  ࠦ <new (std::nothrow)Widget> ந室  . -, nothrow-  new 뢠  뤥 , 筮  ࠧ饭 ꥪ Widget. ᫨    㤠,   new 頥 㫥 㪠⥫,    ⥫. ᫨   뤥 㤠,  뢠  Widget,   ⮩ 窥  ࠭⨨ 稢.  Widget   ,  㣮.   ᠬ  ᥡ    new - ,  ᫨   ,   ⠢  ᯮ짮 nothrow.  맮  new  <new (std::nothrow)Widget>  㦤 ᪫祭,   Widget   ⭮.  ᫨  㤨 ᪫祭,   㤥 ࠭  筮. 뢮? ਬ nothrow new ࠭ ⮫쪮 ,   operator new  㤨 ᪫祭,     ࠭⨩ ⭮⥫쭮 ࠦ,  <new (std::nothrow)Widget>.  ⮬   ⮨  ਡ  nothrow new.
ᨬ  ⮣, ᯮ  <ଠ> (㦤騩 ᪫祭) new   ਠ nothrow, , ⮡    ࠡ稪 new, ᪮  뢠  ଠ.

 ᫥ 
 set_new_handler  㪠 㭪,    맢, ᫨   뤥     㤮⢮७.
  nothrow new ࠭祭, ᪮  ଠ ਬ ⮫쪮  뤥 ; ᫥騥 맮   -० 㦤 ᪫祭.



ࠢ 50:     new  delete

୥  ᭮. ० ᥣ 祬 -    । ஬ ᨨ operator new  operator delete? ,  ࠩ ,  ࠭ 稭.
 ⮡ 㦨 訡 ਬ. ᫨  ᢮ (   delete) , 뤥 ஬ new,  ਢ  窠 . 맮 delete   ࠧ    ⮣  ⪠ , 뤥 new, ਢ  ।  ணࠬ. ᫨  new 㤥  ᯨ᮪ 뤥 ᮢ,   delete 㤠  ᢮ ⥩  ᯨ᪠,  ⠪ 訡  㦨. 筮 ࠧ 訡  ணࠬ  ਢ    殬 뤥  (९ )   , ।饣 砫 뤥 . 樠஢   new  訢  襣 ࠧ   ᯮ㥬    ᫥ , 㯭 짮⥫, 뢠   ⮢ (<ᨣ>).  ⮬  delete  ஢ 稥 ⠪ ᨣ. ᫨  , ,  뫠 ,   delete  ⮪஢  䠪   祭 㪠⥫,  ண  㦨.
 ⮡  䥪⨢. ᨨ ஢ new  delete, ⠢塞   ஬, 㭨ᠫ.    ਥ    ࠡ ணࠬ (ਬ, Web-ࢥ஢), ⠪   ணࠬ, ࠡ   ᥪ㭤.   㬥 ࠡ뢠 ਨ ᮢ  뤥   ,  ,  ⠪ ᬥ   㣨.   ஢  ப  ਠ⮢ ᯮ짮 -  ᪮ 뤥 ᪮쪨  讣 ࠧ,    殮 ᥣ ६ ࠡ ணࠬ,  뤥  ᢮   讣 ⢠  ꥪ⮢   ६ .   । ࠣ <>,  ᫨ ⮣  ,    殢 㤥  㤮⢮   뤥 讣  ,  ᫨ 㬬୮ ⠪ ꥬ ,  ࠧᥭ    ⪮.
뢠  ॡ, ।塞  ࠬ , 㤨⥫쭮,  ⠢塞  ࠬ  new  delete ਤন । ⥣.  ࠡ 筮   ,  ⨬쭮 -   . ᫨   ।⠢,  ᪠  ᯮ  襩 ணࠬ,  ᬮ  ᮡ⢥ ᨨ ஢ new  delete, ॢ室騥  䥪⨢ ⠭.  <ॢ室⢮>  ࠧ㬥,   ࠡ ॥ (   浪)  ॡ   ( 50 %).  ,     , ਫ  ⠢塞 new  delete ᮡ⢥묨 ﬨ - ⮩ ᯮᮡ ⨬ 襭 ந⥫쭮.
 ⮡ ᮡ ⨪ ᯮ짮. ० 祬 ३  ᠭ ᮡ⢥ new  delete, ࠧ㬭 ᮡ ଠ  ⮬,   ணࠬ ᯮ  .  । 뤥塞   ࠧࠬ?  ।  ६ ? 冷 뤥  ᢮  ᭮ ᫥ ਭ樯 FIFO (< 襫 -  襫>)   LIFO (<᫥ 襫 -  襫>)?   ୮  ?   ࠪ ᯮ짮   ६,     ࠧ  浪 뤥-᢮   ࠧ묨 ⠤ﬨ ᯮ?  ᨬ ꥬ ᪨ 뤥  ᯮ    ६?
 , ᠭ 짮⥫᪨ ᨩ new  delete - 쭮  . ਬ, ᬮਬ ,   ॠ   new  ஫   ࠭栬 뤥 . ࠢ,    ⢮ 䥪⮢,    㤥    .

static const int signature = 0xDEADBEEF;
typedef unsigned char Byte;
//  ⮬   ᪮쪮 䥪⮢ - . 
void *operator new(std:size_t size) throw(std::bad_alloc)
{
using namespace std;
size_t realSize=size+2*sizeof(int); // 㢥 ࠧ 襭
// , ⮡  뫮 ࠧ
// ᨣ
void *pMem = malloc(realSize); // 맢 malloc  祭 
if(!pMem) throw(bad_alloc);
//  ᨣ  ࢮ  ᫥ ᫮ 뤥 
*(static_cast<int>pMem)) = signature;
*(reinterpret_cast<int*>(static_cast<Byte*>(pMem)+realSize-sizeof(int))) =
signature;
//  㪠⥫   ࠧ  砫쭮 ᨣன
return static_cast<Byte*>(pMem)+sizeof(int);
}

設⢮ ⪮ ⮩ ᨨ  new 易  ⥬,     ᮮ⢥ ᮣ襭 C++ ⭮⥫쭮 㭪権  ⠪ . ਬ,  ࠢ 51 ,    new   横 맮 㭪樨-ࠡ稪 new, 祣  ਠ  . ⮬ ᮣ襭 饭 ࠢ 51, ⮬ ᥩ   ।   ⮭ : ࠢ.
  ⥪ ॡ, ⮡   ⨯ 뫨 ࠧ饭     । ᠬ. ਬ, ⥪  ॡ, ⮡ 㪠⥫ ࠧ頫  ᠬ,  ६ (  뫨 ஢  ࠭ 塠⮢ ᫮),   ⨯ double 稭  , ⭮ 쬨. ᫨  ਤন ⮣ ᮣ襭,    訡  ६ ᯮ. 㣨 ⥪  ௨,    ஢  ᮪ ந⥫쭮, ᫨ 㤮⢮७ ॡ ࠢ. ਬ,  ⥪ Intel x86 祭 ⨯ double   ஢  ࠭  ,     㤥 ⥫쭮 ॥, ᫨  ஢  쬨⮢ ࠭栬.
ࠢ , ⮬  C++ ॡ, ⮡  㪠⥫, 頥 ஬ new, 뫨 ஢   ⨯ . 㭪 malloc 稭 ⨬  ॡ, ⮬ ᯮ짮 㪠⥫, 饭 malloc, ᭮.   ਢ   new   頥 㪠⥫, 祭  malloc,  頥 㪠⥫, ᬥ饭  饭 malloc  ࠧ int.   ࠭⨩,   ᭮! ᫨  맮  new, ⮡  ,   ࠧ饭 double ( ᫨  襬  new[]  뤥   ᨢ 祭 ⨯ double),  ⮬ ⨬ ணࠬ  設,  int  4 ,  祭 double   ஢  ࠭栬 쬨⮢ , , ᪮॥ ᥣ, ୥ ࠢ쭮 ஢ 㪠⥫.   맢 ਩ ⠭ ணࠬ.      ࠡ.   砥,  ᮢᥬ  ,   ⥫.
   ⠫ ⫨砥   ᨮ쭮 ⢠  ,    ᪮  ணࠬ, 㦤 ⢫  㣨 .  ᮡ⢥  ,   ࠡ⠥, 筮 .  ⠪,  ࠡ⠥ ,  ᫮.  ,   ४  ⨬ , ᫨ ⮫쪮  ⥫쭮 ॡ.
    .    ४⥫, 騥 ⫠  ⮪஢ ࠡ 㭪権 ࠢ . 孮⭮ ⢮  㬥樥  襬   ᪫ 室  ᠭ ᮡ⢥ ᨩ new  delete.   ଠ 㯭 ᪨ த, 騥  㭪樨 ࠢ , ⠢塞  ࠬ. ⮡ ᯮ짮  ७ 㭪樮쭮  (।⥫쭮) 襭 ந⥫쭮, ਤ    ணࠬ ( , ᠬ ᮡ, ).
㣮 ਠ -     .     , ⮬  ᪠  ஡.   ⠪ ।⥫     - ⥪ Pool  ஥ Boost (. ࠢ 55). ⥪ Pool । ।⥫ , ⨬஢  ᯮ짮       権,      ⠭  : ।   讣 ⢠  ꥪ⮢.     C++,   ࠭ ।樨 ⮩, ਢ  ᮪ந⥫쭮 ।⥫    ꥪ⮢,   ᪠ ⠪ <> ⠫,  ७ᨬ, ᮣ襭  ࠢ, ᭮ ⭮⥫쭮 ⮪  . .  ॠ ⥪ 祭 ࠧ  ⮩稢 .  ᫨    ᮡ⢥ new  delete, ⢮ ᨩ   , ⭮,     ⠫,  ⫨ < ࠡ騥> ⥬  ⢨⥫쭮 ࠡ. ࠢ -   ⠪ ⠫. ⮨  ⬥,    TR1 (. ࠢ 54) 祭 প   ॡ ࠢ, ᯥ  ⭮ ⨯.
 饣 ࠢ -   ⮬,     ᨨ new  delete  㬮砭 -  쭮 ஢   ஢ .    ⢥     ஡.
 ⮡ 㦨 訡 ᯮ짮 ( 뫮 ᪠ ).
 ⮡ ᮡ ⨪  ᯮ짮 ᪨ ।  (⠪ 뫮 ᪠ ).
  ᪮७  ।  ᢮ . ।⥫ 饣 祭  (   ᥣ) ࠡ  , 祬 ⨬஢ ᨨ, ᮡ ᫨ ᫥ ᯥ樠쭮 ࠧࠡ⠭  ꥪ⮢ । ⨯.    ।⥫  ਬࠬ 뤥  䨪஢ ࠧ, த ,  ।⠢ ⥪ Pool  ஥ Boost. ᫨  ਫ 筮,   , ⠢塞  ஬,  㬮砭 ⮪,       ந⥫쭮, ᠢ     ਫ. 筮, ० 祬 ,  㦭 ९뢠  new  delete  襭 ᪮, 㡥   䨫஢,   㭪樨 ⢨⥫쭮  㧪 ⮬.
 ⮡ 㬥  室, ࠪ  ⠭⭮  .   饣 祭  (  ᥣ)  ⮫쪮  ⨬஢ ᨩ,   ॡ  .  ந室 - ⮣,    뤥  易   室. ।⥫, ⨬஢   ꥪ⮢ (, ਬ, Pool),      室.
 ⮡ ஢ 㡮⨬쭮 ࠢ  ।⥫  㬮砭.   㦥 㯮, ᠬ    祭 double  ⥪ x86 砥 ⮣,   ஢  쬨 ࠭栬.  ᮦ,  new, ⠢塞  묨 ࠬ,  ࠭ 쬨⮢ ࠢ  ᪮ 뤥 double.      new  㬮砭  ᯥ樠,  ࠭ ⠪ ࠢ,     ந⥫쭮 ணࠬ.
 ⮡ 㯯஢ 易 ꥪ   㣮. ᫨  ,  ।   筮 ᯮ ,   ஢  訡 - ⢨ ࠭  䨧᪮   ࠡ  ⠪ 묨, , ,   ᮧ ⤥    , ⮡  뫨 ᮡ࠭ ,   ⮫ 讬 ᫥ ࠭, ᪮쪮 . ᨨ ஢ new  delete  ࠧ饭 (. ࠢ 52)  ᯥ ⠪ 㯯஢.
 ⮡  ⠭⭮ .   , ⮡  new  delete  , 祣 ⠢塞  ஬ ᨨ   㬥. ਬ,  㦭 ।  ᢮    ࠧ塞 ,   権  ⠪    ⮫쪮 ணࠬ 䥩 C. ᠭ ᯥ樠 ᨩ new  delete (,  ࠧ饭 - . ࠢ 52)    C API   C++.  ⠪   ᯥ樠  delete,   ᢮  ﬨ, ⮡  ⥯    ਫ.

 ᫥ 
   稭  ᠭ ᯥ樠 ᨩ new  delete,  襭 ந⥫쭮, ⫠ 訡  ࠡ  祩,  ⠪ ᡮ ଠ樨  ᯮ짮 .



ࠢ 51: ਤন ਭ ᮣ襭  ᠭ new  delete

 ࠢ 50 , 祬   ᮡ⢥ ᨨ ஢ new  delete,  祣    ᮣ襭,  ᫥ ਤন   ᠭ.  ⨬ ᮣ襭  ⠪  ᫮,     ⨢ 樨, ⮬    室.
筥   new.  ⢥饣 ⠭  new ॡ, ⮡  頫 ࠢ쭮 祭, 뢠 ࠡ稪 new,  襭   㤠 뤥 (. ࠢ 49),  ࠢ쭮 ࠡ뢠   뤥  ⮢. ஬ ⮣,  ਭ   ⮬, ⮡ ﭭ   <ଠ>  new,    襩  ᠥ 䥩 , 祬 ॡ ॠ樨 (. ࠢ 52).
ᯥ ࠢ쭮 頥 ஬ new 祭 . ᫨   뤥 襭 ,  頥 㪠⥫  . ᫨  ,  ᫥ ४  ࠢ 49  㦤 ᪫祭 ⨯ bad_alloc.
   ⠪ , ⮬   new ⠥ 뤥    ࠧ,  ᫥  㤠 뢠 㭪-ࠡ稪 new. ।,   ᬮ - ᤥ  ᢮ ண ꥬ . 쪮 ⮣,  㪠⥫  ࠡ稪 new ࠢ ,  new 㦤 ᪫祭.
,  C++ ॡ, ⮡  new 頫 ४ 㪠⥫  ⮣,  襭 0 ⮢ . ( ࠭  頥 ॠ  饩  㣨  몠.)  ⮬ ⮣  ᥢ   new (童 ) 룫廊 ⠪:

void *operator new(std::size_t size) throw(std::bad_alloc)
{ //   new  ਭ
using namespace std; // ⥫ ࠬ
if (size == 0) { // ࠡ   0 ⮢,
size = 1; // ,  㦭 뤥 1 
}
while(true) {
⪠ 뤥 size ⮢;
if(뤥 㤠)
return (㪠⥫  );
// 뤥   㤠; ஢, ⠭ 
// 㭪-ࠡ稪 new (. )
new_handler globalHandler = set_new_handler(0);
set_new_handler(globalHandler);
if(globalHandler) (*globalHandler)();
else throw std::bad_alloc();
}
}

ࠪ⮢   0 ⮢ ⠪,  ᫨  訢 1 , 룫廊 ᮬ⥫쭮,   ,  ४⭮,  ࠡ⠥,  ⮬   ᪮쪮   ᮡࠥ 訢 0 ⮢?
 ⠪   ࠢ    ᥢ,  㪠⥫  㭪-ࠡ稪 ⠭  ,  ⥬ ⠭  ० 祭.  ᮦ,  ᯮᮡ ।⢥  㪠⥫  ⥪騩 ࠡ稪 new, ⮬ 室 뢠 set_new_handler, ⮡   ⥪饥 祭. 㡮,  ⮦ 䥪⨢,  ࠩ ,  筮 ணࠬ.  筮 ।, ,  - 堭 ᨭ஭樨  ᭮ 㫨஢ (묨) ࠬ , 易묨  㭪樥-ࠡ稪 new.
 ࠢ 49 ⬥祭,   new ᮤন ᪮ 横,   ਢ    横 : <while(true)>. ⢥ ᯮᮡ   横 - ᯥ譮 뤥   믮  㭪樨-ࠡ稪   ᠭ  ࠢ 49 ⢨: ᤥ 㯭  , ⠭ 㣮 ࠡ稪,  ⥪騩 ࠡ稪, 㤨 ᪫祭 ⨯, ந  bad_alloc,    ࠢ .     ᭮, 祬 ࠡ稪 new   ᥡ  ࠧ. ᫨    ᮣ襭,  横   new   .
  ,  㭪-童 operator new ᫥ ந묨 ᠬ.   ਢ    ᫮. ,   ਢ ᥢ operator new ந ⪠ 뤥 size ⮢ (᫨ ⮫쪮 size  ࠢ ). ⢥,  size -  㬥, । 㭪樨. ,    ࠢ 50,   稭 ᠭ ᯥ樠쭮    ⨬ ࠧ饭 ꥪ⮢ । ,     ᮢ. 묨 ᫮, ᫨   X ।  new,  ।,   ⠭  ꥪ ࠧ sizeof(X) -  ,  . - ᫥, ,   맢  new  , ⮡ 뤥   ꥪ ந :

class Base {
public:
static void *operator new(std::size_t size) throw(std::bad_alloc);
...
};
class Derived: public Base //     operator new
{...};
Derived *p = new Derived; // 뢠 Base::operator new!

᫨ ।   Base  new   ஥஢  ⮬ ⮩ ஡ ( ⠪ ⭮ ),   ४⭮ ࠡ  砥,  㯨   뤥  <ࠢ쭮> ࠧ,  ᥣ   ⠭⭮  new:

void *Base::operator new(std::size_t) throw(std::bad_alloc)
{
if(size != sizeof(Base)) // ᫨ size <ࠢ>
return ::operator new(size); // 맢 ⠭  new
//  ࠡ⪨ 
... //  ⨢ 砥 ࠡ 
// 
}

  : <!  뫨 ஢ ⮫᪨ 砩  㫥 ࠧ஬!>  ᠬ  . ஢ઠ ,     ࠢ  sizeof(Base). C++  । ࠭ ॡ, ਬ  ⮭ ꥪ   㫥 ࠧ (. ࠢ 39).  ।, sizeof(Base)     , ⮬ ᫨ size ࠢ ,   㤥 ॠᮢ::operator new,  易 ࠢ쭮 ࠡ    .
᫨   ࠢ ।   ᨢ  ஢ ,  㦭 㤥 ॠ  new[] - ᯥ樠쭮  ᨢ. ( 㭪 筮 뢠 <new  ᨢ>, ⮬  㤭 ।⠢,   ந <operator new[]>). ᫨    operator new[],  ,     뤥  ଠ஢ .    祣     騬 ꥪ⠬  ⮬ ᨢ. ᪨     ।, ᪮쪮 ꥪ⮢ 㤥  ⮬ ᨢ. -,    ࠧ ꥪ.   - ᫥   맢  new[]    뤥   ᨢ ꥪ⮢ ந ,  筮  ꥪ⮢  . ⮬    ।  Base::operator new[],  ࠧ  ꥪ ࠢ sizeof(Base),  ,  ।,  饥 ⢮ ꥪ⮢  ᨢ 㤥 ࠢ (襭 ᫮ m)/sizeof(Base). -, ࠬ size_t, ।  new[],  ᮮ⢥⢮ 襬 ꥬ , 祬  ᠬ ꥪ, ⮬ ,    ࠢ 16,  ᪨ 뤥 ᨢ  १ࢨ஢   ࠭ ᫠ ⮢ ᨢ.
  ᮣ襭,    ᫥  ᠭ  new[].  ᠥ  delete,     .  ,   㦭 ,-  ,  C++ ࠭ ᭮ ᢮   㫥 , ⮬    ।⠢ ⠪ ࠭.  ᥢ   delete,  饣 童 :

void operator delete(void *rawMemory) throw()
{
if(rawMemory == 0) return; // 祣  , ᫨ । 㫥
// 㪠⥫
᢮ ,   㪠뢠 rawMemory
;
}

 ⮩ 㭪樨,  童 , ⠪ ,  ᪫祭 ⮣,  㦭 ஢ ࠧ ⮣,   ᮡࠥ ᢮. ।,   new, ।  , ।   뤥 <ࠢ쭮> ⢠ ⮢ 쭮::operator new,  ⠪  । ଠ  <୮> ࠧ 㭪樨::operator delete:

class Base { //  ,   ࠭,  
public: //  operator delete
static void *operator new(std::size_t size) throw(std::bad_alloc);
static void operator delete(void *rawMemory, std::size_t size) throw();
...
};
void Base::operator delete(void *rawMemory, std::size_t size) throw()
{
if(rawMemory == 0) return; // ஢ઠ  㫥 㪠⥫
if(size != sizeof(Base)) { // ᫨ ࠧ <>,
::operator delete(rawMemory); // 맢 ⠭ 
return; // delete  ࠡ⪨ 
}
᢮ ,   㪠뢠 rawMemory
;
return;
}

᭮,  祭 ⨯ size_t, ஥ C++ ।  delete,   ࠢ, ᫨ 㤠 ꥪ, ந  ,  ஬  㠫쭮 .  ⮣ 㦥 筮, ⮡ ॡ     㠫쭮 ,   ࠢ 7 ᠭ  㣠,  ⢥ 稭.   ⬥,  ᫨  ⨫ 㠫    ,  㭪 operator delete  ࠡ ࠢ쭮.

 ᫥ 
  new  ᮤঠ ᪮ 横,  ⠥ 뤥 ,  뢠 㭪-ࠡ稪 new, ᫨  㤠 㤮⢮   뤥 ,   ࠡ뢠   뤥  ⮢. ᨨ  new ஢   ࠡ뢠   뤥  襣 ࠧ, 祬 .
  delete   祣   ।  㫥 㪠⥫. ᨨ  delete ஢   ࠡ뢠   ᢮ ,  , 祬 .



ࠢ 52: ᫨  ᠫ  new  ࠧ饭,   ᮮ⢥騩  delete

 new  delete  ࠧ饭   C++  ᫨誮 , ⮬  ⮬,      ,  祣 譮. ᯮ (ࠢ 16  17),     ⠪ ࠦ new:

Widget *pw = new Widget;

 뢠  㭪樨:  new, ⮡ 뤥 ,   Widget  㬮砭.
।,   맮 訫 ᯥ譮,  ன 㤨 ᪫祭.  ⮬ 砥 室 ⬥ 뤥 , 믮  蠣 1.  ⨢ 砥  稬  . 짮⥫᪨    ᢮ , ⮬   Widget 㤨 ᪫祭  pw 祣 ⠪   뫮 ᢮. ⥫쭮, 짮⥫ ⠪   稫 㪠⥫  ,    ᢮. ⮬ ⢥⢥  ⬥ 蠣 1   ⥬ ६ ᯮ C++.
ᯮ ⥬ ࠤ  맢  delete, ᮮ⢥騩 ᯮ짮  蠣 1  new,  ᤥ    ⮣,  ,   ਠ  delete -    - 㦭 맢.   ஡, ᫨   ଠ new  delete  묨 ᨣࠬ, ⮬    new:

void *operator new(std::size_t size) throw(std::bad_alloc);

ᮮ⢥ 筮  delete:

void operator delete(void *rawMemory) throw(); // 筠 ᨣ
//  쭮 
// 
void operator delete(void *rawMemory, //  ࠭
std::size_t size) throw(); // ᨣ  
//  

᫨   ⮫쪮 묨 ଠ new  delete,  ᯮ ⥬    ਠ delete,  ,  ⬥ ⢨, 믮 ஬ new. ஡ ᪠ ࠢ쭮 ਠ delete  ⮣,       new - ⠪,  ਭ ⥫ ࠬ.
ਬ, ।,   ᠫ  new ஢ ,  ॡ  ⮪ ofstream, 㤠  뢮 ⫠筠 ଠ  뤥 ,     ᠫ ⠪   delete ஢ :

class Widget {
public:
...
static void *operator new(std:size_t size, // 筠
std::ostream& logStream) // ଠ new
throw(std::bad_alloc);
static void operator delete(void *pMemory, // 筠
std:size_t size) throw(); // ଠ delete
// ஢ 
...
};

 襭 猪 ਢ  訡,  ⮡ , 祬  ⠪, ਤ   묨 ନ.
㭪 operator new, ਭ ⥫ ࠬ ( 易⥫쭮 㬥 size_t), 뢠 ஬ new  ࠧ饭  ࠧ騬 ஬ new (placement new). ਢ   new  ࠧ   ⠪. ᮡ  뢠 ࠧ騩  new,  ண  㬥⮬ 㦨 㪠⥫   ,  ꥪ   ᪮஢.   new 룫廊 ⠪:

void *operator new(std::size_t, void *pMemory) throw(); // "ࠧ騩 new"

  new   ⠭⭮ ⥪ C++,   砥   ,   室 ⥪ ४⨢ #include <new>.  , ⠪  new ᯮ  ॠ樨  vector  ᮧ ꥪ⮢  뤥   .  ⠪ ࢮ砫쭠   new  ࠧ饭;    稫  <placement new>.  ࠧ, ᠬ ନ <ࠧ騩 new> ॣ㦥. 筮,    ࠧ饬 new,      㭪:  new, ਭ騩 ⥫ 㬥 ⨯ void*.  ⠪    㣮 ᨨ new, ਭ饩 ⥫ 㬥. 筮 ⥪ ᪫砥 ⨢稢 ⮫,   ,  騩 ନ <ࠧ騩 new> 砥   new, ਭ ⥫ 㬥, ᪮ ࠦ <ࠧ騩 delete>  <delete  ࠧ饭> (஥  ᥩ 㤨) ந室  .
 ୥    Widget, ஥   ਫ. ஡  ⮬,    뢠  窨 . ᬮਬ ᫥騩 짮⥫᪨ ,  ⮪ ଠ  뤥   ⮪ cerr  ᪮ ᮧ ꥪ⮢ Widget:

Widget *pw = new (std::cerr) Widget; // 맢  new, । cerr
//  ⢥ ࠬ ⨯ ofstream;
//    窥   砥,
//   Widget 㦤
// ᪫祭

᫨ 뤥  諮 ᯥ譮,   Widget 㦤 ᪫祭,  ᯮ ⥬ ⢥砥  ᢮ ⮩ ,  ᯥ 뤥  new. ᯮ ⥬   ,  ࠡ⠥ 맢   new, ⮬   ⬥ १ 樨 ᠬ⥫쭮.  ⮣ ᯮ ⥬    delete,  ਭ   ⢮ 㬥⮢ ⮣  ⨯,   new,  ᫨ 室 ,  뢠.   砥  new ਭ ⥫ 㬥 ⨯ ostream&, ⮬ ᮮ⢥騩  delete   ᫥ ᨣ:

void operator delete(void *, std::ostream&) throw();

   ࠧ騬 ﬨ new ᨨ  delete,  ਭ ⥫ ࠬ, 뢠 ࠧ騬 delete.    Widget   ࠧ   delete, ⮬ ᯮ ⥬  ,  ⬥ ,  ᤥ ࠧ騩 new.  १    祣.  ⮬ ਬ   delete  뢠, ᫨  Widget 㦤 ᪫祭!
ࠢ ⮥: ᫨  new  ⥫묨 㬥⠬  ᮮ⢥  delete  ⠪  㬥⠬,   delete  뢠  砥 室 ⬥ 뤥 , 믮 new. ⮡  祪   ਢ  , Widget   ࠧ騩  delete,  ᮮ⢥ ࠧ饬  new,  믮 ⮪஢:

class Widget {
public:
...
static void *operator new(std:size_t size, std::ostream& logStream)
throw(std::bad_alloc);
static void operator delete(void *pMemory) throw();
static void operator delete(void *pMemory, std::ostream& logStream)
throw();
...
};

 ⨬ , ᫨  Widget 㤨 ᪫祭  ।

Widget *pw = new (std::cerr) Widget; //  ࠭,  ⥯ 
// 祪

 ⮬᪨ 㤥 맢 ᮮ⢥⢥ ࠧ騩  delete, ⠪  Widget ࠭,   祪   ⮩ 稭  㤥.
ᬮਬ,  ந, ᫨  ᪫祭  ( 筮  뢠),   짮⥫᪮    맮 delete:

delete pw; // 맮 筮  delete

 ᪠  ਨ,  뢠   delete,   ࠧ . 騩 delete 뢠, ⮫쪮 ᫨ 㦤 ᪫祭 , ᫥騩  맮 ࠧ饣 new. ᫨ delete ਬ  㪠⥫ ( ਬ  - pw),   delete  ࠧ饭   㤥 맢.
 ,   ।饭  祪 , 樨஢  ࠧ饩 ᨥ new,   ⠪ ।⠢    delete ( 砩, ᫨     ᪫祭),  ࠧ   ⥬  ⥫묨 㬥⠬,    ࠧ饣 new (᫨ ⠪ ). 㯠 ⠪,      ᮭ - 㫮 祪 . ,  ࠩ , - 祪   ⮩ 稭.
, ᪮  㭪権-童 뢠  㭪樨  ꥬ ⥪ (. ࠢ 33),    ஦, ⮡  ⮣,   new ஢   㣨 ᨨ new ( ⮬ ᫥ ),   뢠 짮⥫. ਬ, ᫨     ,  ஬  ⮫쪮 ࠧ   new, 짮⥫ 㦠,  筠 ଠ new ⠫ 㯭:

class Base {
public:
...
static void *operator new(std::size_t size, // 뢠 
std::ostream& logStream) //  
throw(std::bad_alloc);
...
};
Base *pb = new Base; // 訡! 筠 ଠ
//  new 
Base *pb = new (std::cerr)Base; // ࠢ쭮, 뢠
// ࠧ騩 new  Base

筮  new  ந  뢠  ,  㭠᫥ ᨨ  new:

class Derived: public Base {
public:
...
static void *operator new(std::size_t size) // ८।
throw(std::bad_alloc); //   new
...
};
Derived *pd = new (std::cerr)Derived; // 訡! 
// ଠ ⥯ 
Derived *pd = new Derived; // ࠢ쭮, 뢠
//  new  Derived

 ࠢ 33 筮 ஡ ᬮ७   ᮪   ,   ᠭ 㭪権 ।  㦭 ,   㬮砭 C++ ।⠢ ᫥騥   new  쭮  :

void operator new(std::size_t) throw(bad_alloc); //  new
void operator new(std::size_t, void*) throw(bad_alloc); // ࠧ騩 new
void operator new(std::size_t, // new,  㦤騩
const std::nothrow_t&) throw(); // ᪫祭 -
// . ࠢ 49

᫨     new  ,  ⥬ ᠬ 뢠   ⠭ . ,   ᤥ  㯭묨     ᯥ樠 ଠ new,    , ᫨ ⮫쪮   ७  室  ᯮ짮   짮⥫ .     new,  ஬   ,   ⠪ ।⠢ ᮮ⢥騩  delete. ᫨  , ⮡  㭪樨  ᥡ  ࠧ,  뢠 ᮮ⢥騥   ᨨ  ᢮ 㭪権.
 ⮩ ᯮᮡ - ᮧ  , ᮤঠ騩  ଠ  new  delete:

class StandardNewDeleteForms {
public:
// ଠ new/delete
static void *operator new(std::size_t size) throw(bad_alloc)
{ return ::operator new(size);}
static void operator delete(void *pMemory) throw()
{ ::operator delete(pMemory);}
// ࠧ騥 new/delete
static void *operator new(std::size_t size, void *ptr) throw(bad_alloc)
{ return ::operator new(size, ptr);}
static void operator delete(void *pMemory, void *ptr) throw()
{ ::operator delete(pMemory, ptr);}
//  㦤騥 ᪫祭 new/delete
static void *operator new(std::size_t, const std::nothrow_t& nt) throw()
{ return ::operator new(size, nt)}
static void operator delete(void *pMemory, const std::nothrow_t&) throw()
{ ::operator delete(pMemory, nt);}
};

짮⥫,    ᢮ ᥭ ᯥ樠묨 ଠ new, ਬ ᫥  using- (. ࠢ 33), ⮡    ⠭ ଠ:

class Widget: public StandardNewDeleteForms { // ᫥
public: // ⠭ 
using StandardNewDeleteForms::operator new; // ᤥ  
using StandardNewDeleteForms::operator delete; // 묨
static void *operator new(std::size_t size, // 
std::ostream& logStream) // ᯥ樠
throw(bad_alloc); // ࠧ騩 new
static void operator delete(void *pMemory, // 
std::ostream& logStream) // ᮮ⢥騩
throw(); // ࠧ騩 delete
...
};


 ᫥ 
    ࠧ   new, 㡥,   뫨  ᮮ⢥⢥ ࠧ饬  delete. ᫨   㤥,   襩 ணࠬ   ⮭, 㤭㫮 窨 .
  ࠧ騥 ᨨ new  delete,   ⮬, ⮡ ﭭ   ଠ ᨩ  㭪権.




 9



ᬮ      ⮨ ᥣ   ࠢ,   祭 .
 ࢮ ࠢ ન,  ।०  -  ,      .  ࠩ , ᫨  , ⮡  ணࠬ  ᥡ ࠢ쭮.  ஬ ।⠢  ⠭⭮ ⥪ C++,    㭪樮쭮, ।   TR1.  ,  ᫥ ࠢ ।⠢  ஥ Boost - ,   Web-ᠩ, 饭 騬 ᠬ ਬ C++.  ᮢ,    ࠢ,  䥪⨢ ணࠬ  C++   .



ࠢ 53: 頩   ।० 

 ணࠬ   ।० .   殢, ᫨  ஡ 뫠 -饬 쥧,  뤠  訡!  㦤   ࠢ⥫쭮 ।묨  ࠡ  - 㣨 몠,   ⭮襭 C++  ,  ᮧ⥫ ஢ 筥  業 ⨭  . ਬ,  ਢ 訡,  ࠭   ᪠   :

class B {
public:
virtual void f() const;
};
class D: public B {
public:
virtual void f();
};

।,  㭪 D::f 㤥 ८। 㠫 㭪 B::f,  訡 ⮨  ᫥饬:   B 㭪-童 f - ⠭⭠,   D     const.     ஢ ᮮ頥 ᫥饥:

warning: D::f() hides virtual B::f()
(।०: D::f() 뢠 virtual B::f())

  ணࠬ, 稢  ᮮ饭,  ᥡ: <筮, D::f 뢠 B::f - ⠪   !>  ࠢ.   ⠥ ᪠ : f,   B,  뫠  ୮  D,   ⠭ (᭥ 稭 ⮣  . ࠢ 33). ᫨ ⠢    ।०,   猪 ਢ  訡筮  ணࠬ, , ⮡  稭, ॡ   ⫠ -  ⮬,    㦥  㦨.
  ⮣   ਮ  ࠡ  ।०ﬨ ⭮ , 㦥 㤭 㤥 ,   ࠧ ᮮ饭 ( ᮦ, । ॠ쭮 祭 ᮮ饭 न쭮 ⫨砥  ।). ७஢,  ᫥⢨ ᬮ ᯮ ஢ 楫  ।०,  筮   ,  樨 ண   뤠  ।०,   롮 襣 ஢ ⨪.     뫮, ० 祬 ⪫ ।०,  㡥,   筮    .
   ஭㫨 ⥬ ।०, ⮨ ,    ᢮ த ᨬ  ॠ樨, ⮬  ᫥ ᫨誮 ᫠  ४뢠   㦥  訡. ਬ,   ᮪⨥ 㭪樨, ਢ , 室 १ 㣮 ( ᮦ, ப ࠭)   - ।०.

 ᫥ 
 ਭ 쥧 ।०   ࠩ  ⮣, ⮡     뢠 ।०,    ᨬ쭮 ஢ ⨪.
    ᨬ  ।० , ⮬  ࠧ  ।०  ࠧ .  室     ய  ।०,    ਢ몫 .



ࠢ 54:   ⠭⭮ ⥪,  TR1

⠭ C++ (㬥, 뢠騩    ⥪)  ஢  1998 .  2003  뫨 ᥭ 訥 , ࠢ騥 訡.   ⠭⨧樨, , த ࠡ,   <ᨨ 2.0> ⠭ C++  ਬ୮  2008 . । ⭮⥫쭮 筮  , 祬 筮  뫪  ᫥  C++  <++0> ( C++ 200- ).
।⥫쭮, C++0x 㤥 뢠     ᠬ ,     㭪樮쭮 C++ 㤥     ⠭⭮ ⥪.  㦥  -  ⮣,    ⥪, ⮬   ᯥ஢  㬥, ⭮   TR1 (<Technical Report 1>), ᮧ ࠡ祩 㯯  ⥪ C++.   ⠭⨧樨 ࠭  ᮡ ࠢ ஢ ᠭ  TR1 㭪樮쭮, ० 祬  㤥 祭  樠 ⠭ C++0x,  ⢥  .  ࠪ᪮ 窨 ७, TR1 頥 砫  ।樨 C++,   뫮   ⠭⮬ C++ 1.1.   䥪⨢ ࠡ騬 ணࠬ⮬ C++,     㭪樮쭮, ᠭ  TR1, ⮬     ⥪  ਫ   ⨯.
० 祬  ⪨  ⮣,  祭  TR1, ⮨ ᯮ ᭮  ⠭⭮ ⥪ C++, ᯥ஢  C++98:
 ⠭⭠ ⥪ 蠡 (STL),  ⥩ (vector, string, map  . .); ;  (find, sort, transform  . .); 㭪樮 ꥪ (less, greater  . .)  ࠧ  ⥩஢  㭪樮 ꥪ⮢ (stack, priority_queue, mem_fun, not1  . .).
 ⮪ -뢮 (iostreams),  প । 짮⥫ ਧ樨, ୠ樮 -뢮  ।। ꥪ - cin, cout, cerr  clog.
 প ୠ樮樨,    ᪮쪮 ⨢ .   wchar_t (筮 16- char)  wstring (ப, 騥  wchar_t), 騥 ࠡ  ஢ Unicode.
 প ᫥ ⮤,  蠡   ᥫ (complex)  ᨢ  祭 (valarray).
  ᪫祭,    exception, ந   - logic_error  runtime_error,  ⠪ ࠧࠧ , ᫥騥 ⨬.
 ⠭⭠ ⥪ C89. ,    ⠭⭮ ⥪ C 1989 ,    C++.
᫨ -  ᫥  ,  ᮢ  ६  ࠢ , ⨢  襬  㪮  C++.
TR1 ᯥ 14  ⮢ ⥪.   室  ࠭⢥  std, 筥,   ࠭⢥ tr1.  ࠧ,    TR1 shared_ptr (. ) - std::tr1::shared_ptr.  ⮩    ய᪠ std::,     ⠭⭮ ⥪,  ᥣ 㪠뢠 䨪 tr1::.
 饩  뫨 ਢ ਬ ᫥ ⮢ TR1:
 <⥫㠫> 㪠⥫ tr1::shared_ptr  tr1::weak_ptr. tr1::shared_ptr ࠡ⠥  ஥ 㪠⥫,  ᫥, ᪮쪮 ஢ tr1::shared_ptr 㪠뢠  ꥪ.  ਥ 뢠  뫮 (reference counting).  㭨⮦ ᫥ ⠪ 㪠⥫ (  稪 뫮  ꥪ ⠭ ࠢ 0), ꥪ ⮬᪨ 㤠.  㤮  ।饭 祪   横᪨  ,  ᫨    ꥪ⮢ ᮤঠ 뫠騥   㣠 㪠⥫ tr1::shared_ptr,  ࠧ 横,  稪 뫮  ⠢ ⥫묨,  ᫨  譨 㪠⥫  ꥪ, ࠧ騥 横,  㭨⮦ (  㯯 ꥪ⮢  楫 ⨦).  ⠪ 樨  㯠 । <᫠ 㪠⥫> tr1::weak_ptr.    ⮬, ⮡ 㯠  ஫ 㪠⥫, ᮧ 横   , ᭮  ਬ tr1::shared_ptr,   ⨢ 砥 뫨  横. ⥫ tr1::weak_ptr     뫮.  ࠧ蠥 ᫥ 㪠⥫ tr1::shared_ptr  ꥪ,  ꥪ 㤠,  ᫨   த 㪠뢠 - tr1::weak_ptr.  ⠪ 㪠⥫ tr1::weak_ptr ⮬᪨   ⢨⥫.
tr1::shared_ptr,  ,    TR1.  ⭮ ਡ    ⮩ ,  ⮬ ᫥  ࠢ 13,  , 祬  ⠪ . ( ᮦ,    諮   tr1::weak_ptr.)
 tr1::function   ।⠢  뢠 魮 (   㭪  㭪樮 ꥪ),  ᨣ ᮢ⨬  楫 ᨣன. ᫨  ⨬ ᯥ  ॣ樨 㭪権 ⭮ 맮,  ਭ ࠬ int   string,   ᤥ ᫥饥:

void registerCallback(std::string func(int)); // ⨯ ࠬ
//  㭪
// ਭ int 
//  string

 ࠬ - func - 易⥫쭮, ⮬ registerCallback     ⠪:

void registerCallback(std::string (int)); //  ,  ; 
// ࠬ 饭

⬥⨬,  <std::string (int)> -  ᨣ 㭪樨. tr1::function  ᤥ 㭪 registerCallback      ⮣,   㬥⮬    뢠 魮,  ਭ ࠬ int   ८ࠧ㥬  int  頥 string   ८ࠧ㥬  string. tr1::function ਭ  ⢥ 蠡 ࠬ ᨣ 楫 㭪樨:

void registerCallback(std::tr1::function<std::string (int)> func);
// ࠬ func -   뢠
// 魮  ᨣன, ᮢ⨬
//  "std::string (int)"

 ⠪ த 㤨⥫쭮 㤮.  ࠫ த஢   ࠢ 35.
 tr1::bind  ,   ᯮᮡ -뢠⥫ STL bind1st  bind2nd,   㣮.  ⫨稥  뢠⥫, ⢮  TR1, tr1::bind  ࠡ   ⠭묨, ⠪   ⠭묨 㭪ﬨ-童. ᪠ ⠪ ࠬ, ।  뫪. ஬ ⮣,  ⫨稥   뢠⥫, tr1::bind  㦤    ஭  ࠡ  㪠⥫ﬨ  㭪樨, ⮬   ptr_fun, mem_fun  mem_fun_ref । 맮 tr1::bind   㦤.  , tr1::bind -  뢠⥫ ண , ஥ ⢥  ᢮ ।⢥. ਬ ᯮ짮  ਢ  ࠢ 35.
稥  TR1  ࠧ   㯯. ࢠ 㯯 ।⠢ 쭮 , ᠬ⥫ 㭪樮쭮:
 -⠡ ᯮ  ॠ樨 ⥩஢,  set, multiset, map  multimap. 䥩  ⥩஢ ᬮ஢  ᭮ ᮮ⢥饣   ।饩 ᨨ ⥪.  㤨⥫  -⠡ TR1 : tr1::unordered_set, tr1::unordered_multiset, tr1::unordered_map, tr1::unordered_multimap.  ࠦ  䠪,   ⫨稥  set, multiset, map  multimap,  ஢ ⥩஢ TR1   㯮冷祭.
  ࠦ,   ᪠    ப, ॡ ᮮ⢥⢨  . .
 ⥦ (tuples) -  饭 蠡 pair, 㦥 饣  ⠭⭮ ⥪. ᫨ ꥪ ⨯ pair  ᮤঠ ⮫쪮  ꥪ,  ꥪ tr1::tuple  㦨 ⨫饬  ந쭮 ᫠ 㣨 ꥪ⮢. ࠭  ࠭ Python  Eiffel, ࠤ!   C++    襩 த .
 tr1::array -  , <STL-஢> ᨢ,   ᨢ, ন騩 ⠪ 㭪樨-童,  begin  end.  tr1::array 䨪  樨;  ꥪ  ᯮ ᪮ .
 tr1::mem_fn - ᨭ⠪᪨ 㭨஢ ᯮᮡ 樨 㪠⥫  㭪樨-童.  tr1::bind 頥 뢠⥫ bind1st  bind2nd  ⥪ C++98, ⠪  tr1::mem_fn   mem_fn  mem_fn_ref.
 tr1::reference_wrapper - ।⢮, ।祭  ⮣, ⮡ ਤ 뫪 襥 室⢮  ꥪ⠬.  ⭮,    ᮧ ⥩,   ᥡ ⠪,  ᮤঠ 뫪 ( ⢨⥫쭮 ⥩  ᮤঠ ⮫쪮 ꥪ  㪠⥫).
  砩 ᥫ - ।⢮,  ॢ室饥 㭪 rand,  C++ 㭠᫥  ⠭⭮ ⥪ C.
 樠 ⥬᪨ 㭪樨,   , 㭪樨 ᥫ,  ᪨ ⥣ࠫ   㣮.
 ७, ᮢ⨬  C99, -  㭪権  蠡, ।祭  祭  C++   ।  ⥪ C99.
ன  ⮢ TR1 ᯥ稢 প  ७ 孨 ணࠬ஢  ਬ 蠡,   ⠯ணࠬ஢ 蠡 (. ࠢ 48):
 ࠪ⨪ ⨯ (type traits) -  ᮢ  ।⠢ ଠ樨  ⨯  ६ 樨 (. ࠢ 47).   ⨯ T -ࠪ⨪ TR1  㧭,    ஥,   㠫 ஬, ।⠢  ⮩  (. ࠢ 39),     ८ࠧ   㣮 ⨯ U   㣮. -ࠪ⨪ TR1 ⠪  ⠪ । ࠢ쭮 ࠢ   ⨯,  祭   ᠭ ᯥ樠஢ 㭪権 ।  (. ࠢ 50).
 tr1::result_of - 蠡, 騩 뢥 ⨯ 祭, 頥 㭪樥.  ᠭ 蠡     뫠  ⨯ ꥪ, 頥  맮 㭪樨 (蠡),   ⨯  ᫮ ࠧ   ⨯ ࠬ஢. tr1::result_of 頥 । 頥 ⨯ 祭, 頥 㭪樥: tr1::result_of ᯮ      ᠬ ⥪ TR1.
ᬮ      TR1 ( ⭮, tr1::bind  tr1::mem_fn)  ࠭ ⢮訥 ,   TR1 ᮤন   ᮢᥬ  ⥩.    ⮢ TR1   , ⮬ 㭠᫥  㤥 த ࠡ.
 TR1 ᠬ  ᥡ - ᥣ  㬥[4]. ⮡ ᯮ짮 २⢠ ᠭ   㭪樮쭮, 室    ॠ樨.     㤥 ⠢   ࠬ,   2005 ,  ᠫ  , ⭮,   祭  TR1 諮  ⠢ 饩   ॠ樨 ⠭⭮ ⥪.  , 㦭      㣨 : 10  14 ⮢ TR1 ᭮  ⥪, 㯭  ᠩ Boost (. ࠢ 55), ⮬  ⫨ 筨 TR1- 㭪樮쭮.   <TR1->, ⮬   ⥫쭠  ⮣,  ᠭ  TR1,    ⥪ Boost,   ,   뭥譨 ᨨ Boost   ᮮ⢥ ᯥ䨪樨 TR1. ,   㤥   , Boost  ⮫쪮 㤥 ।⠢  ᮮ⢥ TR1 ॠ,  ⠪    ,  諨  TR1 ᨬ.
᫨  ।⠥ ਬ TR1- ⥪ Boost  ⢥ ६ ,   ,    ࠬ   ⠢ ᮡ⢥ ॠ樨 TR1, ,  ਤ ਬ   ࠭⢠ .   Boost 室  ࠭⢥  boost, ⮣   TR1  室  ࠭⢥ std::tr1.   㪠 , ⮡  ਭ 뫪  ࠭⢮ std::tr1   boost.    :

namespace std {
namespace tr1 = ::boost; // std::tr1 - ᥢ  ࠭⢠ boost
}

孨᪨ ⠪  ⠥ ।, ⮬ ,  ᭥  ࠢ 25, 頥  -  ࠭⢮  std.  ࠪ⨪, ,  ஡ ⭮.    ।⠢ ᮡ⢥ ॠ TR1,  㦭 㤥 ⮫쪮 㤠   ᥢ ࠭⢠ . , 뫠騩  std::tr1, ⠭ ࠢ.
,    TR1,     ⥪ Boost,-  -⠡.  -⠡ 㯭 㦥    ᪮쪨 筨   hash_set, hash_multiset, hash_map  hash_ multimap.  宩 蠭,  ⥪, ⠢塞  訬 ஬, 㦥 ᮤঠ  蠡. ᫨ ,    ᪮ 設    (     TR1). 猪   ᪮쪮 筨 -  ᪨, ⠪  .

 ᫥ 
 ᭮ 㭪樮쭮 ⠭⭮ ⥪ C++ ⮨  STL, ⮪ iostream  .  祭 ⠭⭠ ⥪ C99.
 TR1  প <⥫㠫> 㪠⥫ (ਬ, tr1::shared_ptr), 饭 㪠⥫  㭪樨 (tr1::function), ஢ ⥩஢, ॣ ࠦ   10 ⮢.
  TR1 ᠬ  ᥡ - ᥣ  ᯥ䨪. ⮡ ᯮ짮 २⢠ TR1,  ॠ.   筨 ॠ権 ⮢ TR1  ஥ Boost.



ࠢ 55:   Boost

  ᮪⢥ ⥪   , ᨬ    ஢? Boost  訬 㣠.  ⥫  ᮥ  ᮮ 樮, ⠫⫨ ணࠬ⮢  C++, ࠡ  ᫥ ᮢ६ ।⠢  ஥஢  ॠ樨 ⥪? Boost  訬 㣠.      ,  㤥 룫拉 C++  饬? Boost  訬 㣠.
஥ Boost -  ६ ᮮ⢮ ࠧࠡ稪   ᢮ ࠭塞 ⥪  C++.  Web-ᠩ 室   http://boost.org.   !
 ⢮ ࣠権  Web-ᠩ⮢, 饭 C++,  Boost   㭨묨 ᮡﬨ. -,    裡  ⮬  ⠭⨧樨 C++  ᯮᮡ    襭. Boost  ᭮ 童 ⮣ ,  ⭨    ⠪ 童 㣮.   ⮬ Boost ᥣ ஢蠫   ᢮ 楫 㦨 ମ  ஢ ।,      ⠭ C++.   १⮢ ⠪ ⭮襭 ⠫ ,   14  ⥪, ।  祭  C++   TR1 (. ࠢ 54),   ⥩ ᭮  ࠡ, த  Boost.
 ᮡ Boost - 楤 ਥ ⥪.   ᭮  㡫筮 㦤 室 ⥪ ᥬ ᮢ묨 栬. ᫨   । ⥪  Boost, 稭  ࠢ 쬠  ᯨ᮪ 뫪  ࠧࠡ稪 Boost, ⮡ 業, ᪮쪮    襩 ࠡ,  樨஢   ।⥫쭮 㦤.  ⮣ 稭 横,   Web-ᠩ 뢠 <㤨, ,   ᬮ७ ᭮. ,   㤥 ⨣ 㤮⢮⥫ १>.
 筮 ⮣  蠥,   ⥪ ⮢  ଠ쭮 ᥭ  ᬮ७.   ਥ ⢥ত,   㤮⢮  ॡ Boost. ਬ,   ஢    ࠬ (⮡ த஢ ७ᨬ).  ⠪  ⢥न,  ⥪   㯭  ਥ ᫮ 業஢ (ਬ,  ᯫ⭠  ᪮  ᪮ ᯮ짮). ⥬  । ।⠢  樠쭮 ᬮ७ ᮮ⢠ Boost.  ६ ਮ ᬮ७ ஢  ।⠢  ਠ (室 , ஥ 㬥, 짮⥫ 㬥  . .)   ᫥騬 ᠬ:
 ᪮쪮  ஥  ॠ?
    ७ᨬ  ࠬ  樮묨 ⥬?
 㤥  ⥪ ᯮ짮 ⥬,   ।祭,   쬨, ࠡ騬  ᮮ⢥饩 ।⭮ ?
   㬥 ᭮,   筮?
砭 ࠢ  ᯨ᮪ 뫪 Boost, ⮡       ப஢.   ਮ 㦤   ਥ 蠥,    ⥪ ਭ⮩, ᫮ ਭ⮩  ⢥࣭⮩.
⮥ 㦤  ࠤ Boost   ᠭ ⥪,  ⠪  ࠬ ᭨  ᥡ,  室  ⨥ ஥஢, ॠ樨  㬥஢ -ଥ ⥪ ஬諥 ஢.   ⥪ ॡ   樠쭮 ᬮ७, ० 祬   ⮩묨 ७.
Boost ᮤন ⪨ ⥪,    ﭭ  . ६  ६    ⥪ ᪫砥,  ࠢ, ⮬,   㭪樮쭮 ४뢠   ⥪, ।⠢饩  ப  ⥩   ஥஢ (     䥪⨢).
⥪ ᨫ쭮 ⫨  ࠧࠬ   ਬ.    室 ⥪, 楯㠫쭮 ॡ騥  ᪮쪨 ப  ( 筮 ᫥  ࠡ⪨ 訡  ᯥ祭 ७ᨬ  ⠭  ).   ⠪ ⥪  Conversion,  ।⠢    㤮  ਢ. ਬ, 室   㭪 numeric_cast 㦤 ᪫祭, ᫨ ८ࠧ  ᫮ ⨯  㣮 ਢ  ९,  稬  㣨  ஡,  㭪 lexical_cast  ਢ  ⨯, ন騩 operator<<,  ப,  祭 㤮  ⨪, ⮪஢  . .  ࠩ ⠢ ⥪, ।⠢騥 ⮫쪮 ப ,     楫 .  ⭮  ⥪ Boost Graph Library ( ணࠬ஢ ந  䮢),  Boost MPL Library (<⥪ ⠯ணࠬ஢>).
⥪ Boost 饭 ᠬ ࠧ ⥬, 㯨஢  ᪮쪮 ᭮ ⥣਩:
 ࠡ⪠ ப  ⥪⮢.  室 ⥪  ᭮  ⭮襭  ⨯ ଠ஢ (   printf), ࠡ  ॣ묨 ࠦﬨ (  ᭮ ᮮ⢥饩 㭪樮쭮 TR1 - . ࠢ 54),  ⠪ ᪮  ࠬ᪮ .
 ⥩.  室 ⥪  ࠡ  ᨢ 䨪஢   STL- 䥩ᮬ (. ࠢ 54), ⮢묨 ࠬ ந쭮 ,  ⠪ 묨 ᨢ.
 㭪樮 ꥪ  ᮪஢ ணࠬ஢.  ⥣ ꥤ ᪮쪮 ⥪,    ᭮ 㭪樮쭮 TR1.      ⥪ Lambda,  ⮫쪮 頥 ᮧ 㭪樮 ꥪ⮢  ,      ᮧ,  ந室:

using namespace boost::lambada; //  ।⢠
//  ⥪ Lambda
std::vector <int> v;
...
std::for_each(v.begin(), v_end(), //    x
std::cout <<_1*2+10<<"\n"); //  v  x*2+10;
// "_1" -  
// ⠭ ⥪饣
// 

 饭 ணࠬ஢.  室 ப  ᮢ-ࠪ⨪ (. ࠢ 47).
 ⠯ணࠬ஢ 蠡 (TMP - . ࠢ 48). 砥 ⥪ ⢥ত (assertions) ६ 樨,  ⠪ ⥪ Boost MPL Library. । 祣  ন STL-  , 뢠騥 魮 ६ 樨,  ਬ ⨯:

// ᮧ ⥩ ६ 樨,  ᯨ, ᮤঠ騩
//  ⨯ (float, double  long double),    "floats"
typedef boost::mpl::list<float, double, long double> floats;
// ᮧ  ⥩ ६ 樨, ᮤঠ騩 ⨯
//  "floats",  "int", ⠢  砫;  
// ⥩ "types"
typedef boost::mpl::push_front<floats, int>::type types;

 ⥩ ⨯ (  뢠 ᯨ᪠ ⨯ - typelists,     ᭮  ⮫쪮   mpl::list,    mpl::vector) 뢠  ᠭ ப     TMP-ਫ.
 ⥬⨪  ᫥ ⮤.  室 ⥪  ࠡ  樮묨 ᫠, ᪠ 襣 饣 ⥫  襣 饣 ⭮,  ⠪  権  砩묨 ᫠ (  ⥪,  ﭨ  祭 ᮮ⢥饩 㭪樮쭮   TR1).
 ४⭮  ஢.  室 ⥪  ଠ樨  蠡 䥩ᮢ (. ࠢ 41)  প ணࠬ஢  ᭮ ⮤ <஢  ᠬ 砫>.
  .  ⭥ᥭ ⥪  প   ⭮襭  ⨯ ꥤ (  <> த ⨯)  ⥪ ⥦,  諠 ਬ  TR1.
 몮 প. ন ⥪, ᯥ稢 <订> ⢨  ணࠬ, ᠭ묨  몠 C++  Python.
 .  室 ⥪ Pool  ᮪ந⥫ ।⥫   䨪஢ ࠧ (. ࠢ 50),  ⠪ 楫  <⥫㠫> 㪠⥫ (. ࠢ 13),  ,  諨  TR1 (  ⮫쪮).   ⠪ <⥫㠫> 㪠⥫,  祭  TR1,  scoped_array - 宦  auto_ptr   ᪨ 뤥 ᨢ; ࠢ 44 ਢ ਬ  ᯮ짮.
 .  ⮩ ⥣ਨ ⭥ᥭ ⥪  ᫥ CRC, 権  ⠬  ६,  ⠪ 室  䠩 ⥬.
 ᥣ    ⥪,    ᠩ ஥ Boost. ᮪   .
Boost । ⥪  襭 ᠬ ࠧ ,  , 筮,  뢠  ⥬, 묨  ணࠬ. , ਬ,  ⥪  ࠧࠡ⪨ ᪨ 䥩ᮢ,    ⥪  㯠   .  ࠩ ,   ᥩ (    ப).   ⮬ ६,   㤥   ,   . ⢥ ᯮᮡ 㧭 筮 -   ᠩ  ஢. ,  ᤥ  אַ ᥩ: http://boost.org.  ᫨    ⠬  筮 ⮣,  ,  ࠢ 易⥫쭮 㦨 - ᭮  ᥡ.

 ᫥ 
 Boost -  ᮮ⢮  Web-ᠩ  ࠧࠡ⪨ ᯫ ⥪  C++  묨 室묨 ⥪⠬, ࣠ 㡫筮 㦤. Boost 뢠  ﭨ  楤 ⠭⨧樨 C++.
 Boost ।⠢ ॠ  ⮢ TR1,  - ஬ ⮣ -  ⢮ 㣨 ⥪.




ਫ 
 । <䥪⨢ ᯮ짮 C++>

  <䥪⨢ ᯮ짮 C++> 諮 ,     묨 ४ﬨ  ࠪ⨪ ணࠬ⮢  C++. ᫨   ⥫묨 ﬨ  䥪⨢ ᢮ ࠡ,  ४   㣨  : < 䥪⨢ ᯮ짮 C++>  <䥪⨢ ᯮ짮 STL>.
  < 䥪⨢ ᯮ짮 C++> 祭 ⥫ ४樨  ஡ ᬮ७ ⠪ ⥬,  䥪⨢  ணࠬ஢  ⮬ ᪫祭. ஬ ⮣,   뢠 ⠪  ਥ ணࠬ஢  C++,  <⥫㠫> 㪠⥫,  뫮  ப-ꥪ.
<䥪⨢ ᯮ짮 STL> -  ⮦  ४権, ࣠  <䥪⨢ ᯮ짮 C++>,  ᭮    㤥 ਬ ⠭⭮ ⥪ 蠡.
ঠ   ਢ .

 䥪⨢ ᯮ짮 C++
᭮
ࠣ 1: 砩 㪠⥫  뫪
ࠣ 2: ।⠩ ਢ ⨯  ⨫ C++
ࠣ 3:   ᯮ 䨧  ᨢ
ࠣ 4:  ࠢ ஢  㬮砭

ࠣ 5: ᠩ ।塞 짮⥫ 㭪権 ८ࠧ ⨯
ࠣ 6: 砩 䨪  䨪  ஢ ६  ६
ࠣ 7:   ॣ㦠 <&&>, <||>  <,>
ࠣ 8: 砩 祭 ஢ new  delete
᪫祭
ࠣ 9: ⮡  窨 ᮢ, ᯮ 
ࠣ 10:  ᪠ 窨 ᮢ  
ࠣ 11:  ࠭ ࠡ ᪫祭  । 
ࠣ 12: ⫨砩  ᪫祭  । ࠬ  맮 㠫쭮 㭪樨
ࠣ 13: 墠뢠 ᪫祭, ।  뫪
ࠣ 14: 㬭 ᯮ ᯥ䨪樨 ᪫祭
ࠣ 15: 業   ࠡ ᪫祭
䥪⨢
ࠣ 16:  뢠  ࠢ <80-20>
ࠣ 17: ᯮ ⫮ ᫥
ࠣ 18:     ᫥
ࠣ 19:  稭  ६ ꥪ⮢
ࠣ 20: 砩 ⨬ 頥 祭
ࠣ 21: ᯮ ॣ㧪, ⮡   ८ࠧ ⨯
ࠣ 22:   ਬ  ᢠ  ⤥쭮 
ࠣ 23: ᯮ ࠧ ⥪
ࠣ 24: 뢠 , 易  㠫묨 㭪ﬨ, ⢥ ᫥, 㠫묨 묨 ᠬ  RTTI
ਥ
ࠣ 25:  㠫묨   㭪樨,  騥 童 
ࠣ 26: ࠭稢 ᫠ ꥪ⮢  
ࠣ 27:  ᨬ  樨 ॡ  頩 ࠧ ꥪ  
ࠣ 28: ᯮ ⥫㠫 㪠⥫
ࠣ 29: ᯮ  뫮
ࠣ 30: ਬ ப-
ࠣ 31:  㭪樨, 㠫  ⭮襭  祬   ꥪ

ࠣ 32: ணࠬ, 뢠  饥
ࠣ 33:  ନ  ࠪ묨
ࠣ 34:  ᯮ짮 C++  C   ணࠬ
ࠣ 35:   ⠭⮬ 몠

䥪⨢ ᯮ짮 STL
 1: ⥩
ࠣ 1:  ࠢ   롮 ⥩
ࠣ 2: ॣ  ⭮⥫쭮 ⥩୮-ᨬ 
ࠣ 3:  ஢ ꥪ⮢  ⥩ 襢  ४
ࠣ 4: 뢠 㭪 empty  ࠢ size()  㫥
ࠣ 5: ।⠩ 㭪樨, ࠡ騥  ,   
ࠣ 6: 頩    ᮡ ᨭ⠪᪮   C++
ࠣ 7:  ᯮ짮 ⥩஢, ࠭ 㪠⥫, 뤥 new,  뢠 맢 delete । 㭨⮦ ⥩
ࠣ 8:   頩 ꥪ ⨯ auto_ptr  ⥩
ࠣ 9: ⥫쭮 롨ࠩ ᯮᮡ ⪨
ࠣ 10:   ᮣ襭  ࠭祭 ।⥫ 
ࠣ 11:  ࠢ쭮 ਬ ᯥ樠஢ ।⥫ 
ࠣ 12:  ॠ᪨  ⭮⥫쭮 ⮪ ᭮ STL-⥩஢
 2: vector  string
ࠣ 13: ।⠩ vector  string ᪨ 뤥 ᨢ
ࠣ 14: ᯮ reserve   㦭 権 । 
ࠣ 15: 뢠 ࠧ  ॠ string
ࠣ 16:  । vector  string 㭠᫥ ணࠬ 䥩ᠬ
ࠣ 17: ᯮ <swap->  ᮪饭 筮 
ࠣ 18:  ਬ vector<bool>
 3: 樠⨢ ⥩
ࠣ 19: , 祬 ࠢ⢮ ⫨砥  ⭮
ࠣ 20:  ᯮᮡ ࠢ  樠⨢ ⥩஢, ᮤঠ 㪠⥫
ࠣ 21:   ⮬, ⮡ 㭪樨 ࠢ 頫 false  ࠢ 祭
ࠣ 22:  䨪樨 祩 < >  ⥩ set  multiset
ࠣ 23: ᬮ  樠⨢ ⥩஢ ஢묨 ࠬ
ࠣ 24: ⥫쭮 롨ࠩ  map::operator[]  map::insert,   䥪⨢.
ࠣ 25:   ⠭묨 ஢묨 ⥩ࠬ
 4: 
ࠣ 26: ࠩ ᯮ짮 iterator  const_iterator, reverse_iterator  const_reverse_iterator
ࠣ 27: ᯮ distance  advance  ८ࠧ const_iterator  iterator
ࠣ 28:  ᯮ짮  iterator, ᮮ⢥騩 reverse_iterator
ࠣ 29: 㬠  ਬ istreambuf_iterator  ᨬ쭮 
 5: 
ࠣ 30: ᯥ稢 筮 訥 楫   ஢
ࠣ 31:  ࠧ ਠ ஢
ࠣ 32: ᫥ 맮 ⬠ remove     뢠 맢  erase, ᫨ ⢨⥫쭮  - 㤠
ࠣ 33:  ஦  ᯮ짮 ⬠ remove     ⥩஢, ᮤঠ 㪠⥫
ࠣ 34:  뢠,     ஢ 
ࠣ 35:  ⮥ ᨬ  ॣ ࠢ ப   ⬮ mismatch  lexicographical_compare
ࠣ 36: ,  ࠢ쭮 ॠ  copy_if
ࠣ 37: ᯮ accumulate  for_each  㬬஢ 
 6: 㭪, 㭪 , 㭪樨  . .
ࠣ 38: ஥ -㭪  ।  祭
ࠣ 39:  । ᢮묨 㭪ﬨ
ࠣ 40:  -㭪 㥬묨
ࠣ 41: 祬 㦭 ptr_fun, mem_fun  mem_fun_ref
ࠣ 42: ,  less<T> 砥 operator<
 7: ணࠬ஢  ᯮ짮 STL
ࠣ 43: ।⠩ 맮 ⬮  ᠭ 横
ࠣ 44: ।⠩ 㭪樨-童 ⬠  ⥬  
ࠣ 45:  ࠧ  count, find, binary_search, lower_bound, upper_bound  equal_range.
ࠣ 46: ᬮ ਬ 㭪樮 ꥪ⮢  㭪権  ⢥ ࠬ஢ ⬮
ࠣ 47:  ᮧ  <⮫쪮  >
ࠣ 48: ᥣ 砩 室  䠩
ࠣ 49:   ᪨ ᮮ饭 , 騥 STL
ࠣ 50: 頩 Web-ᠩ, 饭 STL



ਫ 
⢥⢨ ࠢ  ஬  쥬 

  <䥪⨢ ᯮ짮 C++>   ⫨砥  ண, ⠪  ᮤন   ଠ樨.    ਠ  ண  ⠫   쥬,      ଥ  ᫥⥫쭮.  ਢ  ⠡ ,   ࠢ 쥣    ଠ  ண ,  .
 ⠡ ਢ ᮮ⢥⢨ ଠ樨,   ⥪. ਬ,   ࠢ 39 ண  (< ਢ ⨯   娨 ᫥>) ⥯ ७ᥭ  ࠢ 27 (< 㯮ॡ ਢ ⨯>), ᬮ    ⥪  ਬ  쥬  ᮢ襭 .   ᮤঠ⥫ ਬ:  ஬   ࠢ 18 (<६  ⠪ 䥩ᠬ ᮢ,   묨  묨>).   ᭮ 뢮 ⮣ ࠢ 뫮 ,  㭪樨,   㦥 ᯥ樠     ,     童.  쥬    뢮 ᭮뢠 㣨 ( 쥧묨) 稭, ⮬ ࠢ 18 ᮮ⢥  쥬  ࠢ 23 (<।⠩ 㭪-童 㭪樨,  騥  童,  ﬨ >),  ⢥,  ꥤ   ࠢ,- 魮 뢮.

஥   쥬


   ஬


ਬ砭



1

 ᪨ ॢ:  . 襭 ᫮   C++. ⥫᪨  <>, 2002 (ਬ. . ।.).



2

 ᪨ ॢ:  ஥஢. .:  (ਬ. . ।.).



3

 ஡ ଠ  㭪樨 unexpected   , ᯮ짮 ᢮  ᪮ ࢨᮬ    㪮⢥   C++ (, ⮨ ᪠ ଠ  㭪樨 set_unexpected,  ᯥ unexpected).



4

 砫 2005 ,  ᠫ  ,  㬥    襭,   URL  . ⮬  ४ 㧭   ⥪饬   ࠭ http://aristeia.com/EC3E/TR1_info.html.  URL  .




          See more books in http://www.e-reading.ws
