mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-29 21:25:35 +00:00
94 lines
2.3 KiB
HTML
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>
|
|
<UL><TT>#ifdef X</TT>
|
|
<BR><TT>#include "foo.h"</TT>
|
|
<BR><TT>#endif</TT>
|
|
<BR><TT></TT> </UL>
|
|
|
|
<LI>
|
|
For types that do not need operator= or a copy constructor, declare them
|
|
yourselves and make them private. Example:</LI>
|
|
|
|
<BR>
|
|
<UL><TT>class foo {</TT>
|
|
<BR><TT>...</TT>
|
|
<BR><TT>private:</TT>
|
|
<BR><TT> // These are not supported and are not implemented!</TT>
|
|
<BR><TT> foo(const foo& x);</TT>
|
|
<BR><TT> foo& operator=(const foo& 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>
|
|
<UL>#include "gemini/nsICSSParser.h"
|
|
<BR> </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>
|