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 of their declaration in the class, not by the order in which they are listed in a member initialization list.
Example
/*
* Item 13 - List members in an initialization list in the
* order in which they are declared
*/
class A
{
public:
// Effective C++ 條款 item item 13 violation
A(int i1, int i2, int i3) : idata3(i3), idata2(i2),
idata1(i1) {}
private:
int idata1, idata2, idata3;
};
int main()
{
return 0;
}
Output
List members in an initialization list
in the order in which they are declared
Violation: Effective C++ item 13
Item Effective C++ 條款 item_14
Make destructors virtual in base classes
This rule verifies that destructors in base classes are virtual.
Reason for rule: Declaring the destructor virtual tells the compiler that it must examine the object being deleted to see where to start calling destructors.
Example
/*
* Item 14 - Make destructors virtual in base classes
*/
class Base
{
public:
Base() {}
~Base() {} // Effective C++ 條款 item item 14 violation
};
class Derived: public Base
{
public:
Derived() {};
~Derived() {};
};
int main()
{
return 0;
}
Output
Make destructors virtual in base classes
Severe violation: Effective C++ item 14
Class Base is a base class but does not have a virtual
destructor
Item Effective C++ 條款 item_15
Have operator= return a reference to *this
This rule makes sure your assignment operators return a reference to their left-hand argument, *this.
Reason for rule: Having operator= return a reference to *this protects you from not knowing where the temporary gets destroyed and allows you to declare the operator='s parameter as a reference to const, which is safer than just declaring it to be a reference.
Example
/*
* Item 15 - Have operator= return a reference to *this
*/
class A
{
public:
explicit A(int i = 0) : _i(i) {}
void operator=(const A& a) // Effective C++ 條款 item item 15 violation
{
if (&a == this) {
return;
}
int _i = a._i;
return;
}
private:
int _i;
};
int main()
{
return 0;
}
Output
Have operator= return a reference to
*this
Severe violation: Effective C++ item 15
Item Effective C++ 條款 item_16
Assign to all data members in operator=
If you write operator=, this rule ensures that you assigned to every data member of your object.
Reason for rule: Assigning to all data members in operator= allows you to take control of your assignments.
Example
/*
* Item 16 - Assign to all data members in operator=
*/
class A
{
public:
A() {}
A& operator=(const A&);
private:
int _x, _y, _z;
};
A& A::operator=(const A& a) // Effective C++ 條款 item item 16 violation
{
if (&a == this) {
return *this;
}
_x = a._x;
_y = a._y;
return *this;
}
int main()
{
return 0;
}
Output
Assign to all data members in operator=
Possible severe violation: Effective C++ item 16
Members not assigned:
_z
Item Effective C++ 條款 item_17
Check for assignment to self in operator=
This rule checks your code for aliasing in assignment operators.
Reason for rule: Not checking for assignment to self in operator= can free resources that might be needed during the process of allocating new resources. Checking for assignment to self may also save you a lot of work that you would otherwise have to do to implement assignments.
Example
/*
* Item 17 - Check for assignment to self in operator=
*/
class A{
public:
A() {}
A& operator=(A& a) // Effective C++ 條款 item item 17 violation
{
_i = a._i;
return *this;
}
private:
int _i;
};
int main()
return 0;
}
Output
Check for assignment to self in operator=
Possible violation: Effective C++ item 17
expected: if (&a == this) return *this;