Writing Portable C++Spanning the OS chasm |
|
|
Table of Contents
Most (but not all) of the people who helped put this together: Stephan Kulow, Claus Reimann, Uwe Thiem and Denis Pershin. Thanks, fellows. Use config.hUse config.h, which is created by configure, very liberally. There are many constants that it defines to help make your code portable across platforms.Avoid NULLThe constantNULL is known to cause trouble on some
platforms due to different word sizes and stricter compilers. This is
especially important for method declarations:
class foo {
// DO this
void member( char *value = 0 );
// DON'T do this
void member( char *value = NULL );
};
On the scope of temporariesSome compilers do not accept the following code:
for(int i=0; i<n; i++){ /* do something */ };
for(int i=0; i<n; i++){ /* do something else*/ };
In this case, Don't initialize const in definitionsDo not initialize
class foo {
const int value = 42; // DON'T do this!!
};
The correct way is to define it inside the class constructor:
class foo {
foo();
const int value;
};
foo::foo() : value(42){ /* do something */ }
But: Since this is a constant, I would think you want a static
data member. Otherwise, every
class foo {
static const int value;
};
const foo::value = 42;
BTW: Actually, initialization of const static members is a
new feature. The ANSI C++ Draft dated 2 December 1996
allows the following code:
class foo {
static const int value = 42;
};
That's what you want, right? But there are only a VERY few compiler allowing this! Program in the spirit of portable code and use the other way. Don't use arrays with runtime sizingYou may use this with gcc:
{
char array[count + 3];
fillArray(array);
}
but this is not portable. AIX' xlC doesn't know it and Solaris' CC doesn't know it too. So, please use the following:
{
char *array = new char[count + 3];
fillArray(array);
.
.
.
delete [] array;
}
Remove default args from method implementationDefault arguments should only be specified in the class declaration. Do NOT include them in the implementation of a method.
// myclass.cpp
// *WRONG*
void KMyClass:myMethod( int a=10, const char *str="A String")
{
...
}
// Correct
void KMyClass:myMethod( int a, const char *str)
{
...
}
Don't use gethostname() or getdomainname()gethostname() and getdomainname() are not
part of the POSIX specification. Use utsname() instead.
It is declared in sys/utsname.h.
Using
|
| Function | Relevant Argument |
|---|---|
| bind() | 3rd |
| getsocketname() | 3rd |
| connect() | 3rd |
| getpeername() | 3rd |
| sendto() | 6th |
| recvfrom() | 6th |
| getsockopt() | 5th |
| setsockopt() | 5th |
| accept() | 3rd |
class KFoo : public QWidget
{
// NO
Q_OBJECT;
// Yes
Q_OBJECT
The HP-UX CC compiler cannot handle two consecutive semicolons. It is a silly compiler, but we must comply if we want HP-UX to be a supported platform.
// Naughty! setColor( &(QColor(black)) ); // Nice QColor color(black); setColor( &color );
|
Written and maintained by:
Sirtaj S. Kang
(taj@kde.org)
Last modified: Mon Jan 24 15:00:48 EST 2000 |
|