Item Effective C++ 条款 item_3
Use new and delete instead of malloc and free
This rule warns you if malloc and free are used in your code.
Reason for rule: new and delete can handle constructors and destructors.
Item Effective C++ 条款 item_5
Use the same form in corresponding calls to new and delete
This rule checks calls to new and delete to make sure that they use the same form; it reports a violation if you call new but forget to use [ ] when calling delete.
Reason for rule: If you do not use the same form in corresponding calls to new and delete, an incorrect number of destructors may be called.
Example
/*
* Item 5 - Use the same form in corresponding calls to
* new and delete
*/
class A
{
public:
A() {}
};
int main()
{
A *a = new A[100];
delete a; // Effective C++ 条款 item item 5 violation
}
Output
Use the same form in corresponding
calls to new and delete
Violation: Effective C++ item 5
Found new[] with delete.
Item Effective C++ 条款 item_6
Call delete on pointer members in destructors
This rule warns you if it finds a pointer member that has no corresponding delete in the destructor.
Reason for rule: Calling delete on pointer members in destructors prevents memory leaks now and as the code evolves in the future.
Item Effective C++ 条款 item_7
Check the return value of new
This rule warns you if you do not check the return value of new.
Reason for rule: In cases where new cannot allocate the requested memory, it will return 0.
Note: This item is suppressed by default.
Example
/*
* Item 7 - Check the return value of new
*/
int main()
{
char *pc;
pc = new char[10*10*10]; // Effective C++ 条款 item item 7 violation
pc[0] = 'x';
delete [] pc;
return 0;
}
Output
Check the return value of new
Informational: Effective C++ item 7
Item Effective C++ 条款 item_11
Define a copy constructor and assignment operator for classes with dynamically allocated memory
This rule ensures that copy constructors and assignment operators have been defined in classes with dynamically allocated memory.
Reason for rule: By defining a copy constructor and assignment operator, you achieve significant memory savings and increased speed.
Item Effective C++ 条款 item_12
Prefer initialization to assignment in constructors
This rule checks constructors to see if you are assigning data members when you should be initializing them.const and reference can only be initialized, never assigned.
Reason for rule: If you use initialization instead of assignment, you will increase efficiency and call fewer member functions.
Exception: The only time you should use assignment instead of initialization for data members in a class is when you have a large number of data members of built-in types and you want them all to be initialized in the same way in each constructor.
Example
/*
* Item 12 - Prefer initialization to assignment in
* constructors
*/
class A
{
public:
A(int i, float j) { // Effective C++ 条款 item item 12 violation
_idata = i;
_fdata = j;
}
private:
int _idata;
float _fdata;
};
int main()
{
return 0;
}
Output
Prefer initialization to assignment
in constructors
Informational: Effective C++ item 12
Item Effective C++ 条款 item_13
List members in an initialization list in the order in which they are declared
This rule checks initialization lists to make sure that members are listed in the same order there as they were when declared in the class.
Reason for rule: Class members are initialized in the order o