1.1. Item 1: Distinguish between pointers and references. ¶
- Item 1: Pointer์ Reference๊ตฌ๋ณํด๋ผ.
Pointers use the "*" and "->" operators, references use "."
~cpp char *pc = 0; char& rc = *pc;ํ๋ก๊ทธ๋๋จธ ์ด๊ธ์๋ฉด ์์ ์๋ ๋ฐ๋ณด ์ง์ด๋ค. ํ์ง๋ง ๋ฒํ๊ธฐ ์ฌ์ด๊ฒ ๋ฌธ์ ๋ค.
~cpp string& rs; // Reference(์ฐธ์กฐ)๊ฐ ์ด๊ธฐํ๊ฐ ๋์ง ์์์ ์๋ฌ string s("xyzzy"); // ์ด๊ฑด ๋๋ค ์ ๊ธฐํ๋ค string& rs = s;์์ง string์จ๋ณธ์ ํ๋ฒ๋ ์๋ค. ๋ฐ์ฑ..
~cpp void printDouble(const double& rd) { cout << rd; // rd์ ๊ดํ ํน๋ณํ ๊ฒ์ฌ ํ์ ์๋ค. reference๋๊น. } //////////////////////////////////////////////////////////////////// void printDouble (const double* pd) { if (pd){ cout << *pd // pd๊ฐ null์ธ์ง ๊ฒ์ฌ ํด์ผ ํ๋ค. pointer๋๊น. } }pointer์ ์ ์ ์ฌํญ์ธ null์ ๊ดํ ๋ด์ฉ์ ๋ค์ ์๋ ค์ค๋ค.
~cpp string s1("Nancy"); // ์ด์ ๋ ๋ฌด์จ ํด์ฆ ๊ฐ๋ค. string s2("Clancy"); string& rs = s1; string* ps = &s1; rs = s2; // s1์ ์ธ์๊ฐ "Clancy" ๋ก ๋ฐ๋๋ค๋ ์๋ฏธ๋ค. ps = &s2; // ๊ทธ๋ฅ ํฌ์ธํฐ๋ง ์ธํ ํ๋ ๊ฑฐ๋ค. // ์ด๊ฑธ๋ก ps๋ s2๋ฅผ ๊ฐ๋ฆฌํค๊ณ s1์๋ ์๋ฌด๋ฐ ์ํฅ์ ๋ผ์น์ง ์๋๋ค.์ฌ๊ฒฌ: Call by Value ๋ณด๋ค Call by Reference์ Const์ ์กฐํฉ์ ์ ํธํ์. ์ ์์ Effective C++์ ์ ๋ฐ์ ์ผ๋ก ์ธ๊ธ๋์ด ์๊ณ , ํ๋ก๊ทธ๋๋ฐ์ ํด๋ณด๋ ๊ด์ฐฎ์ ํธ์ด์๋ค. ๋จ return์์ ๋ง์ฝ์ด ์๊ธฐ๋๋ฐ, ํ์ฌ ๋ด ์๊ฐ์ return์ ๋ํด์ ํ์์ ์ด๋ค. ๊ทธ๋์ ๋๋ COM์ ํํ์ธ in, out ์ ๋์ด๋ฅผ ์ฌ์ฉํด์ ์์ ์ธ์๋ฅผ ๋๊ฒจ์ ๊ด๋ฆฌํ๋ค. C++์ ๊ฒฝ์ฐ return์ ์ํด ๊ฐ์ฒด๋ฅผ Call by Referenceํ๋ฉด {} ๋ฅผ ๋ฒ์ด๋๋ ์ ์ด ๋๋๋ฐ ์ด๋์ ํ๊ดด๋๋ ๊ฒ์ธ๊ฐ. ๋ค ๊ณต๋ถ๊ฐ ๋ถ์กฑํด์์ผ ์ฉ --;
์ง์ญํจ์ ์์์ ์ง์ญ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ Reference๋ก ๋ฆฌํดํ ๊ฒฝ์ฐ ์ง์ญ ํจ์๊ฐ ๋ฐํ๋๋ฉด ์ง์ญ ๊ฐ์ฒด๋ ์์ด์ง ๊ฒ์ด๊ณ , ๋ฆฌํด๋๋ Reference๋ ์กด์ฌํ์ง ์๋ ๊ฐ์ฒด์ ๋ํ ๋ค๋ฅธ ์ด๋ฆ์ด ๋ ๊ฒ์ด๋ฉฐ, ๊ฒฝ์ฐ์ ๋ฐ๋ผ์ ์ปดํ์ผ์ ๋ ์ง๋ชจ๋ฅด๋ ๋์ ์ฝ๋๋ผ๊ณ ํ๋ค์.-์ฐจ์ญ-
์คํด์ ์์ง๊ฐ ์๋๋ก ๊ธ์ ์ ์ด ๋จ๊ตฐ์. in, out ์ ๋์ด๋ฅผ ์ด์ฉํด์ reference๋ก ๋๊ธธ ์ธ์๋ค์์๋ in์ ํํ์ฌ reference, out์ pointer๋ก new, delete๋ก ๋์ ์ผ๋ก ๊ด๋ฆฌํ๋๊ฒ์ ์๋ํ ๋ง์ด์์ต๋๋ค. ์ ์ ํ๋ก์ ํธ์ ์ด๋ฐ์์ ํ๋ก๊ทธ๋๋ฐ์ ์ ์ฉ ์์ผฐ๋๋ฐ, ํจ์ ๋ด๋ถ์์ ํฌ์ธํฐ๋ก ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค in์ ํด๋นํ๋ ๊ฐ์ฒด ์ฌ์ฉ ์ฝ๋ฉ์ด ํธํ๋๊ตฐ์. ๊ทธ๋ฆฌ๊ณ ๋ง์ํ์ ๋๋ก, MEC++ ์ ๋ฐ์ ์ง์ญ๊ฐ์ฒด๋ก ์์ฑํ Refernece๋ฌธ์ ์ ๊ดํ ์ธ๊ธ์ด ์๋๋ฐ, ์ด๊ฒ์ ๊ด๋ฆฌ๊ฐ C++์ ๊ฐ์ฅ ํฐ ๋ฒฝ์ผ๋ก ์์ฉํ๋ ๊ฒ์ด ์๋๊น ์๊ฐ์ด ๋ฉ๋๋ค. OOP ์ ์ด๋ ค๋ฉด ๋ฐํ์ ๊ฐ์ฒด๋ก ํด์ผ ํ๋๋ฐ, ์ด๋ฅผ ํฌ์ธํฐ๋ก ๋๊ธฐ๋ ๊ฒ์ ์์น์ ์ผ๋ก ๊ฐ์ฒด๋ฅผ ๋๊ธด๋ค๊ณ ๋ณผ์ ์๊ณ , ํด์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉฐ, reference๋ก ๋๊ธฐ๋ฉด ๋ง์ํ์ ๋ฐ๋ก, ํด๋น scope๊ฐ ๋ฒ์ด๋๋ฉด ์ธ์ด์์ lifetime์ด ๋๋ ๊ฒ์ด๋ฏ๋ก ์์ญ์ ๋ํ ๋ฉ๋ชจ๋ฆฌ ์ ๊ทผ์ OS์์ ๋ง์์ง๋ ๋ชจ๋ฆ
๋๋ค. ๋จ, inline์ ํํ์ฌ๋ ์ด์ผ๊ธฐ๊ฐ ๋ฌ๋ผ์ง๋๋ค. (inline์ ์ฝ๋ ๊ต์ฒด๊ฐ compiler์ ์ํ์ฌ ๊ฒฐ์ ๋๋ฏ๋ก ์ด๊ฒ๋ ์ญ์ ๋ชจํธํด ์ง๋๋ค.) ์์ COM์์๋ OOP์์ ๋ฒ์ด ๋๋๋ผ๋, ๋ฒ์ฉ์ ์ผ๋ก ์ฐ์ผ์ ์๋๋ก C์คํ์ ํจ์์ ๊ฐ์ด in, out ์ ์ ๋์ด์ ํด๋น ์ ๋์ด๋ pointer๋ก ํ๋ ๊ท์น์ ์ธ์๋์์ง์. ์ด ์ค๊ณ๊ฐ C#์์ buil-in type์ scalarํ์ ํด๋นํ๋ ๊ฒ๊น์ง ๋ฐ์๋ ๊ฒ์ด ์ธ์์ ์ด ์์ต๋๋ค.(MS๊ฐ ์ด๊ธฐ .net์ธ๋ฏธ๋์์ ์ด ๋๋ฌธ์ String ์ฐ์ฐ ์ฐจ์ด๊ฐ 10~20๋ฐฐ ์ ๋ ๋๋ค๊ณ ๊ด๊ณ ํ๊ณ ๋ค๋
์๋๋ฐ, ์ง๊ธ ์๊ฐํด ๋ณด๋ฉด ๋ค ๋ถ์ง์๋ ์ด์ผ๊ธฐ ๊ฐ์ต๋๋ค.) -์๋ฏผ
1.2. Item 2 : Prefer C++ style casts. ¶
- Item 2 : C++ ์คํ์ผ์ ํ๋ณํ์ ๊ถ์ฅํ๋ค.
~cpp (type) expressionC++ style cast
~cpp static_cast<type>(expression) const_cast<type>(expression) dynamic_cast<type>(expression) reinterpret_cast<type>(expression)
- static_cast<type>(expression)๋ ๊ธฐ์กด์ C style์์ ์ฌ์ฉํ๋ (type)expression ์ ๋์ผํ๋ค.
- const_cast<type>(expression)์์
~cpp class Widget{ ...} class SpecialWidget:public Widget{...} void update( SpecialWidget *psw); SpecialWidget sw; const SpecialWidget& csw = sw; update(&csw); // ๋น์ฐํ ์๋จ const์ธ์์ด๋ฏ๋ก ๋ณํ(ํ์ด์ฃผ๋ ์ผ) ์์ผ ์ฃผ์ด์ผ ํ๋ค. update(const_cast<SpecialWidget*>(&csw)); // ์ณํ์ฟ ๋ update((SpecialWidget*)&csw); // C style์ธ๋ฐ ์ ๋์๊ฐ๋ค. // C++๋ C์์ ํธํ์ฑ ๊ณ ๋ ค๋ฅผ ์ํ ๊ฒ์ด๋ฏ๋ก ์์ ๊ฐ์ด // ๋์ ํ์ง๋ง ๋ช ์์ ์ด์ง ๋ชปํ๋ฉด์ด ์ง์ ๋๋ค. Widget *pw = new SpecialWidget; update(pw); update(const_cast<SpecialWidget*>(pw)); // error! // const_cast<type>(expression) ๋ // ์ค์ง ์์(constness)๋ ๋ณ์(volatileness)์ ์ํฅ์ ๋ฏธ์น๋ค. // ์ด๋ฐ๋ง ํ๋ฉด ๋ค ๊ฐ๋ฅํ ๋ฏ ์ถ๊ณ static_cast ์ ์ฐจ์ด๊ฐ ์๋ ๊ฒ // ๊ฐ์๋ฐ ์์ ์์ค ์ฒ๋ผ down cast๊ฐ ๋ถ๊ฐ๋ฅํ๋ค.
์์ ์์ ์ด์ด์ ์๊ฐ
- dynamic_cast<type>(expression) ์์
~cpp Widget *pw ... update( dynamic_cast<SpecialWidget*>(pw)); // ์ณ๋ค. // const_cast ๊ฐ down cast๊ฐ ๋ถ๊ฐ๋ฅ ํ ๋์ ์ dynamic_cast ๋ ๋ง๊ทธ๋๋ก // ํด๋น ๋ถ๋ชจ ๊ฐ์ฒด์ ํฌ์ธํฐ์ ์์ ๊ฐ์ฒด๊ฐ ๊ฐ๋ฅด์ผ ์์ผ๋ฉด ํ๋ณํ์ด ๊ฐ๋ฅํ๋ค. // ๋ถ๊ฐ๋ฅ ํ๋ฉด null ์ ๋ฐํํด์ฃผ๋ ๊ธฐํนํ ์ญํ ์ด ํต์ฌ์ด๋ค. void updateViaRef(SpecialWidget& rsw); updateViaRef(dynamic_cast<SpecialWidget&>(*pw)); // ์ณ๋ค. // reference์์๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค? ๊ทธ๋ผ ๋ถ๊ฐ๋ฅ ํ ์์ ์ฒ๋ฆฌ๋? // ์ด๋ด๋๋ ์์ธ(exception)์ ๋ฐ์์์ผ ์ค๋ค.
- reinterpret_cast<type>(expression) ์ ์ฐจํ ๋ค์ ์ฝ์๋ค ์ ๋ฆฌํ๋ค.
- C ์์ ์ง์ํ์ง ์์ ๊ฒฝ์ฐ ๋ค์ ๋งคํฌ๋ก๋ฅผ ์ฌ์ฉํ์ฌ
~cpp #define static_cast(TYPE, TEXPR) ((TYPE) (EXPR)) #define const_cast(TYPE, TEXPR) ((TYPE) (EXPR)) #define reinterpret_cast(TYPE, TEXPR) ((TYPE) (EXPR))
์ด๋ ๊ฒ ๊ตฌํ ํด์ ๋๋นํ๋๊ฑธ ์ถ์ฒํ๋ค. (dynamic_cast ๋ C์ ์ ์ฐจ์ ํ๋ก๊ทธ๋๋ฐ์์ ๋ฑ์ฅ ๋ถ๊ฐ)
~cpp double result = static_cast(double, firstNumber)/ secondNumber; update(const_cast(SpecialWidget*, &sw)); funcPtrArray[0] = reinterpret_cast(FuncPtr, &doSomething);
1.3. Item 3: Never treat arrays polymorphically ¶
- Item 3: ์ ๋๋ก! ํด๋์ค ๊ฐ์ ๋คํ์ฑ์ ํตํ ๋ฐฐ์ด ์ทจ๊ธ์ ํ์ง ๋ง๋ผ
~cpp class BST { ... }; class BalancedBST : public BST { ... };์ด๋ฐ ํด๋์ค๋ฅผ ์ ์ธํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ค์๊ณผ ๊ฐ์ ํจ์๋ก ํด๋น ํด๋์ค์ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ค๊ณ ๊ฐ์ ํ์
~cpp void printBSTArray( ostream& s, const BST array[], int numElements) { for ( int i = 0; i < numElements; ++i ){ s << array[i]; } }๊ทธ๋ฆฌ๊ณ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ๋ค.
~cpp BST BSTArray[10]; ... printBSTArray(cout, BSTArray, 10); // ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํ๋ค. ์๋ฌด ๋ฌธ์ ์๋ค. ////////////////////////////////////// BalancedBST bBSTArray[10]; ... printBSTArray(cout, bBSTArray, 10); // ๊ณผ์ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํ๋๊ฐ?
์์ ๋๋ฒ์งธ ํธ์ถ์ ํด๋์ค ์์์ ๋คํ์ ์ฑ์ง์ ์ด์ฉํ ํจ์ ์ด์ฉ ์ฆ
~cpp printBSTArray( cout, bBSTArray, 10 );์ ์ ๋ง ์ต์ ์ ๊ฒฐ๊ณผ๋ฅผ ๋ถ๋ฌ ๋ค์ธ๋ค.
C++์์์ ๋ฐฐ์ด์ arrayi ์ ์๋ฏธ๋ *(array+i) ์ธ๋ฐ ์ด๊ฒ์ ์ฃผ์ ์ถ์ ์ ์ํด์ ์๋ฐํ ๋ฐ์ง๋ฉด
*(array+ ( i *sizeof(an object in the array) )
๋ก ์ฌ์ฉํ๋ค. ๋๋์ด ์ค๊ฒ ์ง! ๋น์ฐํ ์์์ child๋ parent๋ณด๋ค ํฐ ๊ฒฝ์ฐ๊ฐ ๋ค๋ฐ์ฌ์ด๊ณ ๋ฐฐ์ด์ ์์น ์ถ์ ์ด ์๋ง ์ง์ฐฝ์ด ๋์ด ๋ฒ๋ฆฐ๋ค.
*(array+ ( i *sizeof(an object in the array) )
๋ก ์ฌ์ฉํ๋ค. ๋๋์ด ์ค๊ฒ ์ง! ๋น์ฐํ ์์์ child๋ parent๋ณด๋ค ํฐ ๊ฒฝ์ฐ๊ฐ ๋ค๋ฐ์ฌ์ด๊ณ ๋ฐฐ์ด์ ์์น ์ถ์ ์ด ์๋ง ์ง์ฐฝ์ด ๋์ด ๋ฒ๋ฆฐ๋ค.
* ๊ฐ์ฒด์ ์ผ๊ด์ ์ธ ์ญ์ ์ ๊ดํด์๋ ์์ ๋ณด๋ฉด
~cpp void deleteArray( ostream& logStream, BST array[]) { logStream << "Deleting array at address " << static_cast<void*>(array) << '\n'; delete [] array; } BalanceBST *balTreeArray = new BalancedBST[50]; ... deleteArray(cout, balTreeArray); // ์ด๊ฒ ์ญ์ ์ ๋๋ก ์ง์์ง๋ฆฌ๊ฐ ์๋ค.์ ์์ ๊ฐ์ด ๊ฐ์ฒด์ ์ง์์ ๋ด๋นํ๋ ํจ์๋ฅผ ์์ฑํ์๋ ์ญ์ ๋ฌธ์ ๊ฐ ์๋ค.
~cpp delete [] array๋ ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ก ๊ต์ฒด๋๋๊ฒ๊ณผ ๊ฐ๋ค.
~cpp for( int i = the number of elements in the array -1; i>0; --i) { array[i].BST::~BST(); }๋ถ๋ชจ ๊ฐ์ฒด์ ํ๊ดด์๋ง ๋ถ๋ฅด๋ฏ๋ก ์ญ์ memory leak ํ์์ ์ผ์ผํจ๋ค.
1.4. Item 4: Avoid gratuitous default constructors. ¶
- Item 4: ์์์ ์ผ๋ก ์ ๊ณต๋๋ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ํผํ๋ผ. ํน์ ๊ธฐ๋ณธ ์์ฑ์์ ๋ชจํธ์ฑ์ ํ์
ํ๋ผ.
์์ ์์ ์์ํ์
~cpp class EquipmentPiece { public: EquipmentPiece(int IDNumber); ... }
ํด๋น EquipmentPiece ๋ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ๋ถ์ค(?) ํ๋ค. ์ด๊ฑด ํฌ๊ฒ 3๊ฐ์ง์ ์ฃผ์ ๋ก ์ค๋ช
ํ ์ ์๋ค.
- ์ฒซ๋ฒ์งธ ๋ฌธ์ ๋ ํด๋น ํด๋์ค๋ฅผ ์ด์ฉํ์ฌ ๋ฐฐ์ด์ ์์ฑ ํ ๋์ด๋ค. . ( The first is the creation of arrays )
~cpp EquipmentPiece bestPieces[10]; EquipmentPiece bestPieces = new EquipmentPiece[10]; // ๋๊ฒฝ์ฐ ์ด๊ธฐํ ํด์ค ๋ฐฉ๋๊ฐ ์์ด์ ์๋ฌ์ด๋ค.์ด๊ฑธ ์ด๋ ๊ฒ ํ์ด ๋ณด์
~cpp int ID1, ID2, ID3, ... ID10; ... EquipmentPiece bestPiece[] = { EquipmentPiece(ID1), EquipmentPiece(ID2), EquipmentPiece(ID3), ..., EquipmentPiece(ID10), }ํ์ง๋ง ์ด๋ด ๊ฒฝ์ฐ์๋ array๋ฅผ heap arrays(heap์์ญ ์ฌ์ฉ array๋ผ๋ ์๋ฏธ๋ก ๋ฐ์๋ค์)๋ก์ ํ์ฅ์ด ๋ถ๊ฐ๋ฅํ ๊ณ ์ ๋ ๋ฐฉ๋ฒ์ด๋ค.
์กฐ๊ธ ๋ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ด pointer๋ฅผ ์ด์ฉํ ์ ๊ทผ์ ์ ์ํ๋ค.
~cpp typedef EquipmentPiece* PEP; PEP bestPieces[10]; PEP *bestPieces = new PEP[10]; for ( int i = 0; i< 10; ++i) bestPiece[1] = new EquipmentPiece( ID Number );ํ์ง๋ง ์ด๋ฌํ ๋ฐฉ๋ฒ์ ํ๋์ ๋ด๋ ๋ฌธ์ ๊ฐ ์์๋๋ค. ๋ฐ๋ก delete๋ฌธ์
๋๊ฐ์ง๋ฅผ ๊ตฌ์ฒด์ ์ผ๋ก ์ด์ผ๊ธฐ ํด๋ณด๋ฉด, ์ฒซ๋ฒ์งธ๋ก for ๋ฌธ์์์ ํ ๋น๋๋ ๊ฐ์ฒด์ ์ซ์๋ฅผ ๊ธฐ์ตํ๊ณ ์์ด์ผ ํ๋ค. ์์ด๋ฒ๋ฆฌ๋ฉด ์ฐจํ resouce leak์ด ๋ฐ์ ํ ๊ฒ์ด๋ค. ๋๋ฒ์งธ๋ก๋ pointer๋ฅผ ์ํ ๊ณต๊ฐ ํ ๋น์ผ๋ก ์ค์ ํ์ํ memory ์ฉ๋๋ณด๋ค ๋ ์ด๋ค.
๊ทธ๋ผ ๋ ์๊ธฐ๋ ๋ฐฉ๋ฒ์ ์ ์ํด ๋ณด์. (์ด ์ฑ
์ ๋ณด๊ณ ์๋
ธ๋ผ๋ฉด, ์ด๋ ๊ฒ๋ ์ธ์ ์๋ค๋๊ฑธ ๋ณด์ฌ์ฃผ๋๊ฑฐ ๊ฐ๋ค.)
์ผ๋จ ํ๋ณดํ ์์ญ์ ์ง์ฐ๊ธฐ๋ ์ฝ๋ค. ์ด๋ ๊ฒ
~cpp void *rawMemory = operator new[](10*sizeof(EquipmentPiece)); EquipmentPiece *bestPieces = static_cast<EquipmentPiece*>(rawMemory); for ( int i = 0; i< 10; ++i) new (bestPieces+1) EquipmentPiece ( ID Number ); // ์ด๊ฑด placement new ๋ผ๊ณ ํ์ฌ Item 8 ์์ ์ธ๊ธํ๋ค. // ์ฒ์ ๋ณด๊ณ ๋๋๋ค. ์ด์ ์ฌ๊ธฐ์ผ ํ๋ฉด์ --;; // ์ด ์ฑ ์ฃผํน๊ธฐ๋ค. ๋ค์ ์ค๋ช ํ๋๊น ๊ทธ๋ฅ ๋์ด๊ฐ ํ๋๊ฑฐ // ์ํผ ์๋ฏธ๋ ์ดํด ๊ฐ๊ฒ์ด๋ค.์ฐธ ๊ฑฐ์ง ๊ฐ์ ์ง ์ํด ๋๋๋ค.
์ผ๋จ ํ๋ณดํ ์์ญ์ ์ง์ฐ๊ธฐ๋ ์ฝ๋ค. ์ด๋ ๊ฒ
~cpp delete [] rawMemory;์ญ์๋ ์ด๊ฒ๋ delete์ ๊ดํ ๋ชจํธ์ฑ์ ์ ๊ณตํ๋ค. ๋ฌธ์ ์ ๋๋ ์ ์ rawMemory์์ ๋ฐฐ์น๋ EquipmentPiece์ destructor ํธ์ถ ์ ๋ฌด์ด๋ค. ์์๋๋ก destructor๋ฅผ ๊ฐ์ ํธ์ถํด ์ค์ผ ํ๋ค. ๊ทธ๋์ ์๊ฐ ์๋๋ผ, ์ด๋ฐ์์ ์ญ์ ๊ฐ ๋๋ค.
~cpp for ( int i = 9; i>= 0; --i) bestPieces[i].~EquipmentPiece(); // ์ธ์ ๋ ๋๋ผ๋ ๊ฑฐ์ง๋ง C++์ ๋ฐฉ์ข ์ ๊ฐ์ ธ๋ค ์ค์ ์๋ค. operator delete[](rawMemory);์ฐธ๊ณ ๋ก
~cpp delete [] bestPieces; // ์ด๋ ๊ฒ ์ง์ฐ๋๊ฑด new operator๊ฐ ๊ฑด๋ค์ง ์์์ ๋ถ๊ฐ๋ฅํ๋ค.
- ๋๋ฒ์งธ ๋ฌธ์ ๋ ๋ง์ template class(ํนํ STL์์) ๋ค์๊ฒ ์ํ์ ์๊ฒจ์ค๋ค.
~cpp template<class T> class Array{ public: Array(int size); ... private: T *data; }; template<class T> Array<T>::Array(int size) { data = new T[size]; .... }์ฒซ๋ฒ์งธ์์ ์ ๊ธฐ๋ ๋ฌธ์ ๊ฐ ์ด๋ฒ์๋ template class ๋ด๋ถ์์ ์ผ์ด ๋๊ณ ์๋ ์ ์ด๋ค. ๊ฑฐ์ฐธ ์๋ดํ ๊ฒฐ๊ณผ๋ฅผ ์ด๋กํ๋ค. ๋ฌธ์ ๋ ์ด๋ฌํ template class ๊ฐ ์ด์ ๋ ์์ STL๊ฐ์ library๋ก ๊ตฌ์ถ๋์๋จ ์ฌ์ค. ์์์ ์ ๊ธฐ๋ณธ ์์ฑ์ ๋ง๋ค์ด ์ ์ฉํ์๋ผ. ๋ค๋ฅธ ๋ฐฉ๋ ์๋ค.
DeleteMe ์ด ๋ถ๋ถ์ด ๋ถ์คํ๋ค ์ฐจํ ๋ณด๊ฐ ํ์
- ์ธ๋ฒ์งธ(๋ง์ง๋ง) ๋ฌธ์ ๋ virtual base class์ ๊ฐ์ด ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๊ฐ์ ธ์ผ ํ๋ ๋ง์์ผ ํ๋ ๋ฏธ๋ฌํ ๋๋ ๋ง์ ์ฐ์ถ์ด๋ค.