首页 C++编码规则

C++编码规则

举报
开通vip

C++编码规则C++编码规则 C++编码规则(共28条) Effective C++ 1 Adhere to convention when writing new and delete. 编写new和delete时要固守常规。 2 Avoid hiding the global new. 避免隐藏全局的new。 3 Avoid returning "handles" to internal data from const member functions. 避免从一个声明为const类型的成员函数返回句柄指向内部数据成员。 4...

C++编码规则
C++编码规则 C++编码规则(共28条) Effective C++ 1 Adhere to convention when writing new and delete. 编写new和delete时要固守常规。 2 Avoid hiding the global new. 避免隐藏全局的new。 3 Avoid returning "handles" to internal data from const member functions. 避免从一个声明为const类型的成员函数返回句柄指向内部数据成员。 4 Declare a copy constructor and operator= for classes with dynamically allocated memory 为需要动态内存分配的类声明一个拷贝构造函数和一个赋值操作符 5 Have operator= return a reference to *this. 让赋值操作符返回*this指针的引用。 6 Make destructors virtual in base classes. 在基类中定义虚析构函数。 7 Never return a reference to a local object. 不要返回局部对象的引用。 8 Pass objects by reference instead of by value. 尽量用引用来传递对象而不是用值传递。 9 Write delete if your write new. delete和new要成对使用。 10 Initialization 初始化列表中成员列出的顺序和它们在类中声明的顺序相同 11 All member variables should be initialized in constructor. 所有的成员变量应该在构造函数中初始化。 Meyers-Klaus 12 Avoid calling virtual functions from constructors and destructors 避免调用构造函数和析构函数中的虚函数 Object Oriented 13 Do not directly access global data from a constructor 不要从构造函数中直接获取全局变量。 14 Return reference to *this in operator= functions 用operator = 库函数返回*this指针的引用。 Possible Bugs 15 A pointer to an abstract class shall not be converted to a pointer of a class that inherits from the abstract class 指向抽象类的指针不要转换为一个从抽象类继承来的类的指针。 16 Avoid assignment in if statement condition 避免在if条件语句中赋值 17 Do not call delete on non-pointers 不要对非指针成员调用delete。 18 Do not compare chars to constants out of char range 不要将字符型变量和在字符型变量表示范围之外的常量做比较。 19 Do not compare chars to constants out of char range 不要将字符型变量和在字符型变量表示范围之外的常量做比较。 20 Don't check floats for equality; check for greater than or less than 不要用等式来判断浮点型值,用>=或<=来对浮点型变量做比较。 21 Don't check floats for equality; check for greater than or less than 不要用等式来判断浮点型值,用>=或<=来对浮点型变量做比较。 22 If a class has virtual functions it shall have a virtual destructor 含有虚函数的类应该有一个虚析构函数。 23 Local variables and class variables should have different name. 局部变量和对象变量应该用不同的标识符。 24 Never covert consts to non-consts 决不要将常量转换为非常量。 25 Public member functions shall always return const handles to member data 公有型成员函数总是返回常量句柄指向成员数据。 Sutter-Alexandrescu 26 Avoid providing implicit conversions 避免隐式类型转换。 27 Make base class destructors public and virtual, or protected and nonvirtual 将基类的析构函数声明为公有的虚函数或者受保护型的非虚函数。 28 Throw by value, catch by reference 用值来throw,用引用catch 参考:http://www.kuqin.com/effectivec2e/index.html
本文档为【C++编码规则】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_199788
暂无简介~
格式:doc
大小:36KB
软件:Word
页数:3
分类:互联网
上传时间:2011-01-15
浏览量:38