[MoreEffectiveC++]ì˜ Item 26 'Limiting the number of objects of a class. 를 ë³´ë©´ 다ìŒê³¼ ê°™ì€ ë¶€ë¶„ì´ ìžˆë‹¤. {{| The second subtlety has to do with the interaction of inlining and static objects inside functions. Look again at the code for the non-member version of thePrinter: ¤ Item M26, P17 Printer& thePrinter() { static Printer p; return p; } Except for the first time through this function (when p must be constructed), this is a one-line function — it consists entirely of the statement "return p;". If ever there were a good candidate for inlining, this function would certainly seem to be the one. Yet it's not declared inline. Why not? ¤ Item M26, P18 Consider for a moment why you'd declare an object to be static. It's usually because you want only a single copy of that object, right? Now consider what inline means. Conceptually, it means compilers should replace each call to the function with a copy of the function body, but for non-member functions, it also means something else. It means the functions in question have internal linkage. You don't ordinarily need to worry about such linguistic mumbo jumbo, but there is one thing you must remember: functions with internal linkage may be duplicated within a program (i.e., the object code for the program may contain more than one copy of each function with internal linkage), and this duplication includes static objects contained within the functions. The result? If you create an inline non-member function containing a local static object, you may end up with more than one copy of the static object in your program! So don't create inline non-member functions that contain local static data.(9) ------ 9) In July 1996, the °ISO/ANSI standardization committee changed the default linkage of inline functions to external, so the problem I describe here has been eliminated, at least on paper. Your compilers may not yet be in accord with °the standard, however, so your best bet is still to shy away from inline functions with static data. ¤ Item M26, P61 |}} ---- C++ ì—ì„œ SingletonPattern ì„ êµ¬í˜„í• ë•Œ 다ìŒê³¼ ê°™ì€ ë°©ì‹ì„ ì‚¬ìš©í•˜ê³ ëŠ” 한다. {{{~cpp Object& theObject() // ì´ í•¨ìˆ˜ëŠ” í´ëž˜ìŠ¤ì˜ ì •ì 메소드나 // friend 함수ì—ì„œ 호출ëœë‹¤. { static Object obj; return obj; } }}} ì²˜ìŒ ì„ ì–¸ë 때를 ì œì™¸í•˜ê³ ëŠ” ì € 함수는 {{{~cpp Object& theObject() { return obj; } }}} 와 ê°™ì€ ì˜ë¯¸ê°€ ëœë‹¤. ì´ê²ƒì€ inline 으로 ì„ ì–¸í• ê±°ë¦¬ê°€ ë 것 ê°™ê¸°ë„ í•˜ì§€ë§Œ inline 으로 ì„ ì–¸ë˜ì§€ 않았다. 왜ì¼ê¹Œ? (Except for the first time through this function (when p must be constructed), this is a one-line function — it consists entirely of the statement "return p;". If ever there were a good candidate for inlining, this function would certainly seem to be the one. Yet it's not declared inline. Why not? ) ê·¸ê²ƒì€ ë°”ë¡œ InternalLinkage 때문ì´ë‹¤. InternalLinkage 란, ì»´íŒŒì¼ ë‹¨ìœ„(translation unit -> Object Codeë¡œ ìƒê°í•´ ë³´ìž) ë‚´ì—ì„œ ê°ì²´ë‚˜ í•¨ìˆ˜ì˜ ì´ë¦„ì´ ê³µìœ ë˜ëŠ” ë°©ì‹ì„ ì¼ì»«ëŠ”다. 즉, ê°ì²´ì˜ ì´ë¦„ì´ë‚˜ í•¨ìˆ˜ì˜ ì´ë¦„ì€ ì£¼ì–´ì§„ ì»´íŒŒì¼ ë‹¨ìœ„ 안ì—서만 ì˜ë¯¸ë¥¼ 가진다. 예를들어, 함수 fê°€ InternalLinkage를 가지면, 목ì 코드(Translation Unit) a.obj ì— ë“¤ì–´ìžˆëŠ” 함수 f와 목ì 코드 c.obj ì— ë“¤ì–´ìžˆëŠ” 함수 f는 ë™ì¼í•œ 코드임ì—ë„ ë³„ê°œì˜ í•¨ìˆ˜ë¡œ ì¸ì‹ë˜ì–´ ì¤‘ë³µëœ ì½”ë“œê°€ ìƒì„±ëœë‹¤. ''DeleteMe ì´ ë§ë„ ì´í•´ê°€ 안갑니다. ì£¼ì œë¡œ 시작한 inlineì€ ì¤‘ë³µ 코드를 ê°ì•ˆí•˜ê³ ì„±ëŠ¥ì„ ìœ„í•´ì„œ 쓰는 ê²ƒì´ ì•„ë‹ˆ ì—ˆë˜ê°€ìš”? ë¬´ì—‡ì´ ë¬¸ì œì¸ê°€ìš”? inline ì´ ì•„ë‹Œ í•¨ìˆ˜ë“¤ì€ ExternalLinkageë¡œ ì „ì œ ë˜ì—ˆë‹¤ê³ 볼수 있는ë°, 지ì 해야 í• ê²ƒì€ inlineì˜ operationì— í•´ë‹¹í•˜ëŠ” 코드가 아니ë¼, static ê°™ì€ ë³€ìˆ˜ê°€ ì¤‘ë³µì„ ì˜ˆë¡œ 들어야 í• ê²ƒì„... --NeoCoin'' 하지만 InternalLinkageê°€ 초례하는 ë¬¸ì œëŠ” 1996 {{{~cpp ISO/ANSI C++ }}} 표준화 ìž‘ì—…ì—ì„œ ì¸ë¼ì¸í•¨ìˆ˜(InlineFunction)를 ExternalLinkage ë¡œ 변경해서 ë¬¸ì œê°€ ë˜ì§€ 않는다.(ìµœê·¼ì˜ ì»´íŒŒì¼ëŸ¬ë“¤ì€ 지ì›í•œë‹¤.). (->)''즉.. static ì´ë¼í•´ë„ ê°ê°ì˜ obj 파ì¼ì— 코드가 ë”°ë¡œ 들어가.. ê°ì²´ê°€ 중복 ìƒì„±ëœë‹¤ëŠ” ì´ì•¼ê¸°ì¸ê°€..?'' ''암튼,ê²°ë¡ ì´ ì–´ë–»ê²Œ ë˜ë‚˜ìš”? singleton ì„ êµ¬í˜„í•˜ëŠ” ìš©ë„ë¡œ ìžì£¼ 쓰는 static 변수를 사용하는 (주로 getInstance류) 메소드ì—서는 inline ì„ ì“°ì§€ ë§ìž ì¸ê°€ìš”? --[1002]'' - 구형 컴파ì¼ëŸ¬ì—서는 ë¬¸ì œê°€ ë 수 있지만 ìµœê·¼ì˜ ì»´íŒŒì¼ëŸ¬ì—는 ë¬¸ì œë 게 ì—†ë‹¤ê³ ë§í•˜ëŠ”것 같습니다. ì œ ìƒê°ì´ ìž˜ëª»ëœ ê²ƒì´ë¼ë©´ 거침없는 지ì ì„..^^; - [ìž„ì¸íƒ] ''여기서 ë§í•˜ëŠ” 구형ì´ëž€, 1996ë…„ì— ë³€ê²½ëœ í‘œì¤€ì„ ì§€í‚¤ì§€ ì•Šì€ ì»´íŒŒì¼ ê²ƒì´ë‹¤. 99ë…„ì— ì´ì±…ì„ ì²˜ìŒ ì ‘í• ë•Œ 오래ë˜ì—ˆë‹¤ëŠ” ìƒê°ì€ 안들었는ë°... MEC++ 는 ê³ ì „ì´ ë 수는 없는걸까.. --NeoCoin'' == InternalLinkageë¬¸ì œê°€ 등장하는 다른 ì¼€ì´ìŠ¤ == ---- See also [MoreEffectiveC++], [DesignPatterns] ---- [ìƒì‹ë¶„류]