CodeStyleConventions 代码风格约定(四)
变量时,类方法总是定义为‘const’【常量方法】
避免使用“const_cast”。 当对象需要修改时,但只有常量的版本都可以访问,创建一个函数,它清楚地提供了一个可编辑的对象版本。这保证了‘const-ness’的控制权在对象的手中,而不是在用户的手中。
除非常规使用的对象是改变其状态的,否则返回‘const’对象。例如,媒体对象,如idDecls在大部分的代码中应该是const,而身份的对象根据各种系统修改状态,并确定非const。
在大多数情况下,函数重载应该避免的。例如,而不是:
constidAnim * GetAnim( int index ) const;
constidAnim * GetAnim( const char *name )const;
constidAnim * GetAnim( float randomDiversity) const;
而应使用:
constidAnim * GetAnimByIndex( int index )const;
constidAnim * GetAnimByName( const char *name) const;
constidAnim * GetRandomAnim( floatrandomDiversity ) const;
命名准确、清晰的函数不容易造成
编程者的错误,也不容易由于传入错误数据类型的参数而造成不准确的函数调用。例如:
Anim =GetAnim( 0 );
这可能意味着调用得到一个随机的动画,但编译器将其解释为调用一个指数。
为了加入“常量”访问功能的重载函数是允许的:
class idAnimatedEntity : publicidEntity {
idAnimator * GetAnimator( void );
const idAnimator * GetAnimator( void ) const;
};
在这种情况下,提供一个const的GetAnimator版本,以允许GetAnimator函数能够被const函数调用。因为idAnimatedEntity通常是一个非常量对象,因此这是允许的。对于一个媒体类型,它通常是常量,操作符重载应该避免:
class idDecl
MD5 : public idDecl {
const idMD5Anim * GetAnim( animHandle_t handle ) const;
idMD5Anim * GetEditableAnim( animHandle_t handle );
};
id Studio Names
---------------
idDlg // dialog class
idCtrl // dialog control class
idFrm // frame window
idView //view window
id //any other class
文件名
---------
除非是有意义的若干个更小的类,否则每个类应该是在一个单独的源文件。文件名应该是和类的名称没有“id”前缀是一样的。(大/小写是保存。)
class idWinding;
files:
Winding.cpp
Winding.h
当一个类跨越多个文件,这些文件有一个名字,从类的名称没有“id”,其次是下划线和分段的名字。
class idRenderWorld;
files:
RenderWorld_load.cpp
RenderWorld_demo.cpp
RenderWorld_portals.cpp
When a class is a public virtual interface to a subsystem the public interface is implemented in a header file with the name of the class without "id". The definition of the class that implements the subsystem is placed in a header file with the name of the class without "id" and ends with "_local.h". The implementation of the subsystem is placed in a cpp file with the name of the class without "id".
class idRenderWorld;
RenderWorld.h // public virtual idRenderWorld interface
RenderWorld_local.h // definition of class idRenderWorldLocal
RenderWorld.cpp // implementation of idRenderWorldLocal