📄 ei4.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN" "http://www.w3.org/TR/REC-html40/frameset.dtd">
<HTML LANG="EN">
<HEAD>
<title>Effective C++, 2E | Item 4: Prefer C++-style comments</TITLE>
<LINK REL=STYLESHEET HREF=../INTRO/ECMEC.CSS>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/COOKIE.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">var imagemax = 0; setCurrentMax(0);</SCRIPT>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/DINGBATS.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">
var dingbase = "EI4_DIR.HTM";
var dingtext = "Item E4, P";
if (self == top) {
top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<!-- SectionName="E4: Prefer C++-style comments" -->
<A NAME="1854"></A>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI3_FR.HTM" TARGET="_top">Item 3: Prefer new and delete to malloc and free.</A> <BR> Continue to <A HREF="EMEM_FR.HTM" TARGET="_top">Memory Management</A></FONT></DIV>
<P><A NAME="dingp1"></A><FONT ID="eititle">Item 4: Prefer C++-style comments.</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>
<A NAME="1855"></A>
<P><A NAME="dingp2"></A>
The good old C comment syntax works in C++ too, but the newfangled C++ comment-to-end-of-line syntax has some distinct advantages. For example, consider this <NOBR>situation:<SCRIPT>create_link(2);</SCRIPT>
</NOBR></P>
<A NAME="1856"></A>
<UL><PRE>if ( a > b ) {
// int temp = a; // swap a and b
// a = b;
// b = temp;
}
</PRE>
</UL><A NAME="1857"></A>
<P><A NAME="dingp3"></A>
Here you have a code block that has been commented out for some reason or other, but in a stunning display of software engineering, the programmer who originally wrote the code actually included a comment to indicate what was going on. When the C++ comment form was used to comment out the block, the embedded comment was of no concern, but there could have been a serious problem had everybody chosen to use C-style <NOBR>comments:<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>
<A NAME="1858"></A>
<UL><PRE>if ( a > b ) {
/* int temp = a; /* swap a and b */
a = b;
b = temp;
*/
}
</PRE>
</UL><A NAME="1859"></A>
<P><A NAME="dingp4"></A>
Notice how the embedded comment inadvertently puts a premature end to the comment that is supposed to comment out the code <NOBR>block.<SCRIPT>create_link(4);</SCRIPT>
</NOBR></P>
<A NAME="1860"></A>
<P><A NAME="dingp5"></A>
C-style comments still have their place. For example, they're invaluable in header files that are processed by both C and C++ compilers. Still, if you can use C++-style comments, you are often better off doing <NOBR>so.<SCRIPT>create_link(5);</SCRIPT>
</NOBR></P>
<A NAME="1861"></A>
<P><A NAME="dingp6"></A>
It's worth pointing out that retrograde preprocessors that were written only for C don't know how to cope with C++-style comments, so things like the following sometimes don't work as <NOBR>expected:<SCRIPT>create_link(6);</SCRIPT>
</NOBR></P>
<A NAME="1862"></A>
<UL><PRE>#define LIGHT_SPEED 3e8 // m/sec (in a vacuum)
</PRE>
</UL><A NAME="1863"></A>
<P><A NAME="dingp7"></A>
Given a preprocessor unfamiliar with C++, the comment at the end of the line
becomes <I>part of the macro! </I>Of course, as is discussed in <A
HREF="./EI1_FR.HTM#1790" target="_top">Item 1</A>, you shouldn't be using the
preprocessor to define constants <NOBR>anyway.<SCRIPT>create_link(7);</SCRIPT>
</NOBR></P>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI3_FR.HTM" TARGET="_top">Item 3: Prefer new and delete to malloc and free.</A> <BR> Continue to <A HREF="EMEM_FR.HTM" TARGET="_top">Memory Management</A></FONT></DIV>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -