您的位置:首页 > 编程语言 > C语言/C++

TIPS FOR C++

2007-03-27 16:56 351 查看
[!]Represent concepts as classes;
[2]Use public data(structs) only when it really is just data and no invariant is meaningful for the data members;
[3]A concrete type is the simplest kind of class.When applicable,prefer a concrete type over more complicated class and over plain data structure;
[4]Make a function a member only if it needs direct access to the representation of a class;
[5]Use a namespace to make the association between a class and its helper functions explicit;
[6]Make a member function that does't modify the value of its object a const member function;
[7]Make a function that needs access to the representation of a class but needn't be called for a specific object a static member function;
[8]Use a constructor to establish an invariant for a class;
[9]If a constructor acquires a resource,its class needs a destructor to release the source;
[10]If a class has a pointer member,it needs copy operations(copy constructor and copy assignment);
[11]If a class has a reference member,it probably needs copy operations(copy constructor and copy assignment);
[12]If a class needs a copy operation or a destructor,it probably needs a constructor,a destructor,a copy assignment,and a copy constructor;
[13]Check for self-assignment in copy assignments;
[14]When writing a copy constructor,be careful to copy every element that needs to be copied
(beware of default initializers);
[15]When adding a new member to a class,always check to see if there are user-defined constructors that need to be updated to initialize the member;
[16]Use enumerators when you need to define integer constants in class declarations;
[17]Avoid order dependencies when constructing global and namespace objects;
[18]Use first-time switches to minimize order dependencies;
[19]Remember that temporary objects are destroyed at the end of the full expression in which they are created;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 职场 休闲