📄 linux tutorial c++ coding style, standards and practices.mht
字号:
From: <Saved by Microsoft Internet Explorer 5>
Subject: Linux Tutorial: C++ Coding Style, Standards and Practices
Date: Wed, 4 Apr 2007 10:38:02 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=_NextPart_000_002F_01C776A5.523E3B40"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
This is a multi-part message in MIME format.
------=_NextPart_000_002F_01C776A5.523E3B40
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html
=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Linux Tutorial: C++ Coding Style, Standards and =
Practices</TITLE><LINK=20
href=3D"http://www.yolinux.com/TUTORIALS/yolinux.css" type=3Dtext/css=20
rel=3DSTYLESHEET>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
<META content=3D"C++,coding style,info,C++ links,linux" name=3DKEYWORDS>
<META=20
content=3D"C++ coding style. YoLinux: Linux Information Portal includes =
informative tutorials and links to many Linux sites."=20
name=3DDESCRIPTION>
<META content=3D"Greg Ippolito" name=3Dauthor>
<META content=3D"MSHTML 6.00.2900.3059" name=3DGENERATOR></HEAD>
<BODY text=3D#000000 bgColor=3D#cccccc>
<HR SIZE=3D5>
<TABLE width=3D"100%" callpadding=3D"4">
<TBODY>
<TR>
<TD vAlign=3Dtop><A href=3D"http://www.yolinux.com/"><IMG=20
alt=3D"Yolinux.com Tutorial"=20
=
src=3D"http://www.yolinux.com/TUTORIALS/images/YoLinux_Tutorial_logo.png"=
=20
border=3Dnone></A> </TD>
<TD vAlign=3Dtop>
<H1>C++ Coding Practices, Style, Standards, Practices and document =
generation</H1>
<DIV align=3Dright><!-- BEGIN RICH-MEDIA BURST! CODE -->
<SCRIPT language=3DJavaScript>=0A=
rnum=3DMath.round(Math.random() * 100000);=0A=
=
=0A=
document.write('<scr'+'ipt =
src=3D"http://www.burstnet.com/cgi-bin/ads/ad9427a.cgi/v=3D2.0S/sz=3D468x=
60A|728x90A/'+rnum+'/NI/RETURN-CODE/JS/"></scr'+'ipt>');=0A=
=
=0A=
</SCRIPT>
<NOSCRIPT><A=20
=
href=3D"http://www.burstnet.com/ads/ad9427a-map.cgi/ns/v=3D2.0S/sz=3D468x=
60A|728x90A/"=20
target=3D_top><IMG alt=3D"Click Here"=20
=
src=3D"http://www.burstnet.com/cgi-bin/ads/ad9427a.cgi/ns/v=3D2.0S/sz=3D4=
68x60A|728x90A/"=20
border=3D0></A> </NOSCRIPT><!-- END BURST CODE =
--></DIV></TD></TR></TBODY></TABLE>
<P><B>Tutorial Index:</B>=20
<UL>
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
#PRACTICES">C++=20
Coding Practices</A>=20
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
#CAMELCASE">Camel=20
Case names</A>=20
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
#STYLE">C++=20
Coding Style</A>=20
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
#HEADERCOMMENTS">C++=20
File Header Comments</A>=20
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
#AUTODOCUMENTATION">C++=20
Automated Documentation Generation</A>=20
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
#DOXYGEN">dOxygen=20
and C++ Automated Documentation Generation</A>=20
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
#SOFTWARE">Software=20
for C++ practices and style enforcement</A>=20
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
#LINKS">Links</A>=20
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
#BOOKS">Books</A>=20
</LI></UL><A name=3DPRACTICES></A>
<P>
<HR SIZE=3D5>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" bgColor=3D#000000 =
border=3D1>
<TBODY>
<TR bgColor=3D#cccccc>
<TD vAlign=3Dtop width=3D160 bgColor=3D#c0c0c0>
<P><FONT size=3D-1><B>Related YoLinux Tutorials:</B>=20
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++.html">C++=20
Info, links</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialSoftwareDevelopment=
.html">Software=20
development tools</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html=
">C++=20
String Class</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html">C++ =
STL=20
vector, list</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialXemacs.html">Emacs =
and=20
C/C++</A>=20
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/index.html">YoLinux=20
Tutorials Index</A> </FONT>
<P>
<SCRIPT type=3Dtext/javascript><!--=0A=
google_ad_client =3D "pub-8567479315106986";=0A=
google_ad_width =3D 160;=0A=
google_ad_height =3D 600;=0A=
google_ad_format =3D "160x600_as";=0A=
google_ad_channel =3D"";=0A=
google_ad_type =3D "text";=0A=
google_color_border =3D ["336699","000000","6699CC","003366"];=0A=
google_color_bg =3D ["FFFFFF","F0F0F0","003366","003366"];=0A=
google_color_link =3D ["0000FF","0000FF","FFFFFF","FF6600"];=0A=
google_color_url =3D ["008000","008000","AECCEB","99CCFF"];=0A=
google_color_text =3D ["000000","000000","AECCEB","FFFFFF"];=0A=
//--></SCRIPT>
<SCRIPT =
src=3D"http://pagead2.googlesyndication.com/pagead/show_ads.js"=20
type=3Dtext/javascript>=0A=
</SCRIPT>
<P><!-- BEGIN RICH-MEDIA BURST! CODE -->
<SCRIPT language=3DJavaScript>=0A=
rnum=3DMath.round(Math.random() * 100000);=0A=
document.write('<scr'+'ipt =
src=3D"http://www.burstnet.com/cgi-bin/ads/sk9427a.cgi/v=3D2.0S/sz=3D160x=
600A/'+rnum+'/RETURN-CODE/JS/"></scr'+'ipt>');=0A=
</SCRIPT>
<NOSCRIPT><A=20
=
href=3D"http://www.burstnet.com/ads/sk9427a-map.cgi/ns/v=3D2.0S/sz=3D160x=
600A/"=20
target=3D_top><IMG alt=3D"Click Here"=20
=
src=3D"http://www.burstnet.com/cgi-bin/ads/sk9427a.cgi/ns/v=3D2.0S/sz=3D1=
60x600A/"=20
border=3D0></A> </NOSCRIPT><!-- END BURST CODE -->
<P>
<HR>
<P><A href=3D"http://yolinux.tradepub.com/">Free Information =
Technology=20
Magazine Subscriptions and Document Downloads</A>=20
<P>
<HR>
<P><A =
href=3D"http://yolinux.tradepub.com/?pt=3Dcat&page=3DInfosoft">Free=20
Information Technology <B>Software and Development</B> Magazine=20
Subscriptions and Document Downloads</A>=20
<P>
<HR>
<P></P></TD>
<TD>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>C++ Coding =
Practices:</BIG></B></TD></TR></TBODY></TABLE>
<P>Coding practices should be in place to maintain a consistancy =
among=20
software projects to enforce a disciplines which improves the =
quality of=20
software, improves reuseability due to more generic interfaces and =
good=20
documentation, results in software with fewer bugs which is easier =
to use=20
and maintain.=20
<P>
<UL>
<LI>File Names:=20
<UL>
<LI>There should be one include file for each source code =
file. Each=20
include file should describe a single class or tightly =
integrated set=20
of classes. The general guideline is for include (.h and .hpp) =
files=20
to contain definitions and NOT instantiations. Thus the =
include file=20
can be used in multiple files. The source file (.C or .cpp) =
defines=20
code which when compiled gets loaded into memory. It =
instantiates=20
definitions defined in the include file. For example, a class=20
definition would be contained in the ".h" include file. A =
global=20
variable would be instantiated in the ".cpp" file and NOT the =
".h"=20
file. This is because the global variable would be defined =
more than=20
once if the include file was used more than once. An "extern"=20
statement is appropriate for an include file as it does not =
declare=20
memory for the variable but just recognises its existance.=20
<LI>File name should reflect the class name.=20
<LI>Class names should follow the following format:=20
<TT>CXxClassID</TT> where "Xx" refers to the component. (i.e. =
Mn main,=20
Fm File manager, Cp command processor) The first character of =
each=20
word shall be capitalized. Leave off the "C" prefix when =
naming the=20
file.=20
<LI>When referring to an include file within either another =
include=20
file or source file, do NOT include the full path. This avoids =
portability problems created by operating system dependent =
methods of=20
referencing directory paths. Use compiler command line flags =
to=20
include the path. (i.e. -I/<I>path-name</I>)=20
<LI>Use double quotes ("") for all project include files and =
angle=20
chevrons (<>) for include files provided by the =
operating system=20
or compiler.=20
<LI>If burning ISO CD's, remember it is limited to file names =
of up to=20
30 characters.=20
<LI>To ease use with scripts and make files, file names (and =
directory=20
paths) should not contain blanks, parentheses "(", or crazy =
characters=20
not supported by a majority of operating systems. </LI></UL>
<LI>File headers:=20
<UL>
<LI>File headers should include copyright statements, =
distribution=20
licensing references and reflect corporate practices. For =
example, if=20
source code is released under the GNU license it should be =
stated as=20
such. If the file contains corporate proprietary material, it =
should=20
be stated as such. The segragation of such code into separate =
files is=20
very important if the licensing, release practices for the =
file or=20
copyrights are different.=20
<LI>Comments shall be placed at the top of each file stating =
the name=20
of the file and comments on the file contents. Comments should =
be=20
directed towards the users of the class contained in the file. =
<BR>See=20
below: <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
#HEADERCOMMENTS">File=20
Header Comments</A> </LI></UL>
<LI>Header files must use the preprocessor to check for multiple =
inclusions.=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>#ifndef CLASS_NAME_H
#define CLASS_NAME_H
...
..
#endif
</PRE></TD></TR></TBODY></TABLE></DD></DL>
<LI>If C functions will be called from C++, then C include files =
will=20
use the following declaration:=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>#ifdef __cplusplus
extern "C" {
#endif
...
..
<I>C function declarations</I>
..
...
#ifdef __cplusplus
}
#endif
</PRE></TD></TR></TBODY></TABLE></DD></DL>
<LI>Class contents:=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>class ClassName
{
public:
// Constructors:
// Destructor:
// Functions: modifiers (set), selectors (get)
// itterators:
// Attributes visible by scope of instantiation and use
protected:
// Attributes visible to descendents
private:
// Local attributes
};
</PRE></TD></TR></TBODY></TABLE></DD></DL>
<LI>Machine dependent code shall, if possible, be placed in a =
separate=20
file so that it may ease porting tasks.=20
<LI>Length of functions/subroutines: It makes no sense to set a =
rule or=20
hard limit on the length of a function. A human should be able =
to fully=20
understand the complexity of a single function. If one can't, =
then it=20
needs to be broken down into a smaller understandable size. It =
makes no=20
sense to break a 3000 line function into smaller components if =
all it=20
consists of is a massive case/switch statement which can be =
grasped upon=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -