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��� ����� 기��� ������������ �������� ������ ��������� ������ ��������� ������������ ������������.