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
{{|
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;
}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 μ ꡬνν λ λ€μκ³Ό κ°μ λ°©μμ μ¬μ©νκ³ λ νλ€.
μ²μ μ μΈλ λλ₯Ό μ μΈνκ³ λ μ ν¨μλ
μ κ°μ μλ―Έκ° λλ€. μ΄κ²μ 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? )
~cpp Object& theObject() // μ΄ ν¨μλ ν΄λμ€μ μ μ λ©μλλ // friend ν¨μμμ νΈμΆλλ€. { static Object obj; return obj; }
~cpp Object& theObject() { return obj; }
κ·Έκ²μ λ°λ‘ 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
μνΌ,κ²°λ‘ μ΄ μ΄λ»κ² λλμ? singleton μ ꡬννλ μ©λλ‘ μμ£Ό μ°λ static λ³μλ₯Ό μ¬μ©νλ (μ£Όλ‘ getInstanceλ₯) λ©μλμμλ inline μ μ°μ§ λ§μ μΈκ°μ? --1002
- ꡬν μ»΄νμΌλ¬μμλ λ¬Έμ κ° λ μ μμ§λ§ μ΅κ·Όμ μ»΄νμΌλ¬μλ λ¬Έμ λ κ² μλ€κ³ λ§νλκ² κ°μ΅λλ€. μ μκ°μ΄ μλͺ»λ κ²μ΄λΌλ©΄ κ±°μΉ¨μλ μ§μ μ..^^; - μμΈν
μ¬κΈ°μ λ§νλ ꡬνμ΄λ, 1996λ μ λ³κ²½λ νμ€μ μ§ν€μ§ μμ μ»΄νμΌ κ²μ΄λ€. 99λ μ μ΄μ± μ μ²μ μ ν λ μ€λλμλ€λ μκ°μ μλ€μλλ°... MEC++ λ κ³ μ μ΄ λ μλ μλκ±ΈκΉ.. --NeoCoin
μ¬κΈ°μ λ§νλ ꡬνμ΄λ, 1996λ μ λ³κ²½λ νμ€μ μ§ν€μ§ μμ μ»΄νμΌ κ²μ΄λ€. 99λ μ μ΄μ± μ μ²μ μ ν λ μ€λλμλ€λ μκ°μ μλ€μλλ°... MEC++ λ κ³ μ μ΄ λ μλ μλκ±ΈκΉ.. --NeoCoin