C Coding Suggestions
Ads:
- * Use // for comments everywhere but in macro definitions.
- * Use const variables or enumerations, instead of macros, to define mnemonics for constant values.
- * Use in-line functions and templates instead of function-like macros.
- * Define all member functions outside their respective class declaration.
- * Use in-line functions sparingly.
- * Use REF and other macros to define & and other C++ symbols.
- * Use the C++ new and delete operators instead of the malloc() and free() functions.
- * Where possible, use C++ stream I/O instead of the standard C library routines.
- * In a complex expression, use parentheses to explicitly define how the expression is evaluated.
- * Understand the lifetime of object data.
- * Implement assignment functions so that the same object can appear on both sides of the = operator.
- * Avoid overloading operators other than =.
Figure 1. Partial definition of an OutQ class
class OutQ {
OUTQ * hOutQ; // Handle for output queue
public:
OutQ(char * outqname) {// Constructor
hOutQ = open_outq(outqname);
}
const char * nextname() {
OUTQ_ENTRY * tmp_qentry = next_outq_entry(hOutQ);
return (tmp_qentry ? tmp_qentry->ofile_name : NULL);
}
};
Figure 2. Revised OutQ class
class OutQ {
OUTQ * hOutQ; // Handle for output queue
char HoldName[NAMESIZE];
public:
OutQ(char * outqname) { // Constructor
hOutQ = open_outq(outqname);
}
const char * nextname() {
OUTQ_ENTRY * tmp_qentry = next_outq_entry(hOutQ);
if (! tmp_qentry ) return NULL;
strcpy(HoldName, tmp_qentry->ofile_name);
return HoldName;
}
};
Figure 3. Partial definition of a String class
class String {
char * strdata;
int strlength;
public:
String(char * cs); // Constructor using standard
String& operator=(const String& ss); // Assignment
};
c
har * MakeString(const char * cs) {
char * tmpstr = new char[strlen(cs) + 1];
strcpy(tmpstr, cs);
return tmpstr;
}
String::String(char * cs) {
strdata = cs ? MakeString(cs) : NULL;
strlength = cs ? strlen(cs) : 0;
}
String& String::operator=(const String& ss) {
delete [] strdata;
strdata = ss.strdata ? MakeString(ss.strdata) : NULL;
strlength = ss.strdata ? ss.strlength : 0;
return * this;
}
Figure 4. Revised operator= member function
String& String::operator=(const String& ss) {
if (this != &ss) { // Source is not same as target
delete [] strdata;
strdata = ss.strdata ? MakeString(ss.strdata) : NULL;
strlength = ss.strdata ? ss.strlength : 0;
}
return * this;
}