Effective C++Rules

1. ecpp-02_PreferIOStream: 首选Prefer iostream.h 而非 stdio.h,使用>> <<而不是scanf/printf

2. ecpp-03_NewInsteadMalloc: 使用new代替malloc, delete代替free。new和delete可以处理构造函数和析构函数

3. ecpp-05_FormOfNewAndDelete:new 和delete要配对使用 

    new  数据类型--------------------------------->delete p

    new 数据类型(初始值)------------------->delete p

    new 数据类型[常量表达式]    --------------> delete p[]

4. ecpp-06_CallDeleteMemberPointer: 在构造函数里new,在析构函数里delete

5. ecpp-07_CheckNewReturnValue: Check the return value of new

6. BenefitsFollowing this rule prevents access to a null pointer when memory is not allocated. When new cannotallocate the requested memory, it will return 0.

Example:

#includevoid main()

{

    char *pc;

    pc = new char[10*10*10]; // Violation

    pc[0] = 'x';

    delete [] pc;

}

 Repair:

void main()

{

    char *pc;

    try {

        pc = new char[10*10*10];

    } catch (std::bad_alloc&) { }

    pc[0] = 'x';

    delete [] pc;

}

7. ecpp-08_NewAdhereToConvention: 重载new或delete,应该注意什么?

8. ecpp-09_AvoidHidingGlobalNew:Avoid hiding the global new

9. ecpp-10_CallDeleteIfWriteNew:Write delete if you write new、

10. ecpp-11_CopyConstructorAndAssignmentOperator:

    Define a copy constructor and operator= for classes with dynamically allocated memory

11. ecpp-11_CopyConstructorAndAssignmentOperator1:

    Define a copy constructor and operator= for classes with dynamically allocated memory

    This rule detects classes that have dynamically allocated memory but do not define a copy     constructor and assignment operator.

12. ecpp-12_PreferInitializationToAssigmentInConstructors:常量初始化列表, 不要写在函数体内赋值。

13. ecpp-14_VirtDestrInBaseClass:在基类中声明析构函数为virtual

14. ecpp-15_AssignmentOperatorRetPntThis:

    Have operator= return a reference to *this

15. ecpp-16_AssignmentOperatorForAllMemb:Assign to all data members in operator=

16. ecpp-17_InAssignmentOperatorAssignToSelf: Check for assignment to self in operator=

17. ecpp-19_DifferentiateFunctionKind: Differentiate among member functions, global functions, and friend functions

18. AvoidPublicDataMember: Avoid data members in the public interface

19. ReturnObjectByReference: Pass and return objects by reference instead of by value

20. OperatorShouldReturnAnObject: Don't try to return a reference when you must return an object(This rule detects when you return a reference where you are supposed to return an object. The followingoperators should not return value by reference: a&b , a^b , a|b , ~a , a!=b , a<=b , a=b , a>b , a!b , a&&b , a||b , +a , -a , a%b , a*b , a+b , a-b , a/b.)

21. ZeroConversionProblem:

22. AvoidNonConstAccessToInternalData: 

23. AvoidRelaxedAccessToInternalData: 

24. DereferencedPointerReturn:

25. LocalVariableAsReturn: Never return a reference to a local object

26. AvoidOverloadNonVirtual: Do not redefine an inherited non-virtual, non-operator function

27. AvoidOverloadNonVirtualTempl

28. AvoidOverloadVirtualAndDef: Do not overload an inherited virtual non-operator functions or

default parameter value

29. AvoidOverloadVirtualAndDefTemp: Do not overload an inherited virtual non-operator functions or

default parameter value

30. AvoidCastsDownTheInheritanceHierarchy: Avoid casts down the inheritance hierarchy

你可能感兴趣的:(Effective C++Rules)