Effective C++ 条款总结 读书笔记(一) (二)

2014-11-24 00:59:31 · 作者: · 浏览: 13
f 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: