gecko-dev/xpcom/doc/c++tips.html
1998-03-28 02:44:41 +00:00

94 lines
2.3 KiB
HTML

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Kipp E.B. Hickman">
<META NAME="GENERATOR" CONTENT="Mozilla/4.04 [en] (WinNT; U) [Netscape]">
<TITLE>C++ Tips</TITLE>
</HEAD>
<BODY>
<H1>
C++ Tips</H1>
This is a compilation of tips on how to write cross-platform C++ code that
compiles everywhere.
<H2>
General</H2>
<UL>
<LI>
Always use the nspr types for intrinsic integer types. The only exception
to this rule is when writing machine dependent code that is called from
xp code. In this case you will probably need to bridge the type systems
and cast from an nspr type to a native type. The other exception is floating
point; nspr defines PRFloat as a double (!).</LI>
<LI>
Exceptions do not work everywhere so don't use them anywhere except in
machine specific code, and then if you do use them in machine specific
code you must catch all exceptions there because you can't throw the exception
across xp code.</LI>
<LI>
Templates do not work everywhere so don't use them anywhere.</LI>
<LI>
Do not wrap include statements with an #ifdef. The reason is that when
the symbol is not defined, other compiler symbols will not be defined and
it will be hard to test the code on all platforms. An example of what <B>not</B>
to do:</LI>
<BR>&nbsp;
<UL><TT>#ifdef X</TT>
<BR><TT>#include "foo.h"</TT>
<BR><TT>#endif</TT>
<BR><TT></TT>&nbsp;</UL>
<LI>
For types that do not need operator= or a copy constructor, declare them
yourselves and make them private. Example:</LI>
<BR>&nbsp;
<UL><TT>class foo {</TT>
<BR><TT>...</TT>
<BR><TT>private:</TT>
<BR><TT>&nbsp; // These are not supported and are not implemented!</TT>
<BR><TT>&nbsp; foo(const foo&amp; x);</TT>
<BR><TT>&nbsp; foo&amp; operator=(const foo&amp; x);</TT>
<BR><TT>};</TT></UL>
<LI>
</LI>
</UL>
<H2>
Windows Compatability</H2>
<H2>
Metroworks Compatability</H2>
<UL>
<LI>
MAC compilers do not handle #include path names in the same manner as other
systems. Consequently #include statements should not contain path names,
just simple file names. An example of what <B>not</B> to do:</LI>
<BR>&nbsp;
<UL>#include "gemini/nsICSSParser.h"
<BR>&nbsp;</UL>
<LI>
</LI>
</UL>
<H2>
G++ Compatability</H2>
<UL>
<LI>
Use void in argument lists for functions that have no arguments (this works
around a bug in g++ 2.6.3)</LI>
</UL>
</BODY>
</HTML>