Cpp Coding Standards/FDESIGN/Params

From Apache OpenOffice Wiki
< Cpp Coding Standards‎ | FDESIGN
Revision as of 23:03, 19 July 2007 by Kirk (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Unambiguous Parameters (Params)

<- Summary

Details

in, out, and inout

It is recommended to mark each parameters direction of passing (in, out or inout) explicitely.

It is necessary to at least distinguish between out and inout parameters explicitely, because the difference can not be derived from the parameters signature, but a misuse leads to bugs, for example memory leaks.

The marking can be done by prefixes (i_, o_, io_):

int foo(X & io_object, int i_param);

or by comments in the function documentation:

/** @param x (inout)
*/
int foo(T & x, int param);

Prefixing all parameters, including in-parameters, has the advantage they are clearly distinguishable from local or member variables, and so make the code easier readable.

Passing Heap Objects as Parameters or Return Values

When you pass a heap object to a function, and afterwards the receiving function or its class has the responsibility to delete this object, use std::auto_ptr<> to make this obvious:

void T::ReceiveHeapObject(std::auto_ptr<X> pass_obj);

The same applies to the return value of factory functions:

std::auto_ptr<X> CreateX();
Personal tools