Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, October 31, 2007

C/C++ Show macros

In gcc or g++, to show all of the macros that are defined for a given platform:
gcc -dM -E test.c
or
g++ -dM -E test.cpp

Thursday, March 8, 2007

VIM - Doxygen Syntax Highlighting

To have vim highlight Doxygen comments, add this to your vimrc:
let g:load_doxygen_syntax=1

For more help, try
:help doxygen

Friday, February 23, 2007

C++ - Forward Declarations

If a class declaration (in a header file) does not need to know the size of any included classes/structs (i.e., the class declaration does not need to contain an actual instantiation of another class or access member data or functions of another class), forward declare the dependent classes/structs to minimize dependencies.

In other words, use pointers and references of classes/structs with respective forward declarations instead of member objects instantiations and #include's. Then, #include dependent files to access the other classes' members and functions in the source file (.c,.cpp). Remember to delete the pointers in the destructor if the class owns the pointer.

In myClass.h:
//Forward declarations
class OtherClass; // defined in otherClass.h
struct OtherStruct; // defined in otherStruct.h

class MyClass
{
private:
OtherClass *mMyOtherClass; //pointer
OtherStruct &mMyOtherStruct; //reference
public:
MyClass(OtherStruct &_struct);
~MyClass();
};

In myClass.cpp
#include "myClass.h"
// include headers of forward declarations
#include "otherClass.h"
#include "otherStruct.h"

MyClass::MyClass(OtherStruct &_struct)
: mMyOtherClass(new MyOtherClass),
// references can only be initialized in
// constructor initialization list
mMyOtherStruct(_struct)
{
// Empty constructor body
}

MyClass::~MyClass()
{
delete mMyOtherClass;
}

C++ - Minimize Dependencies

A change in a header file (.h, .hpp) causes every other header file that includes it to be recompiled. Thus, to decrease incremental build times, strive to minimize the number of #include's in header files.

Note this cannot be done with every header file, such as files containing dependencies on template classes or typedefs.

C++ - const Keyword

Use the keyword const to signify (to developers, not just the compiler) that a variable does not change).

/**
* This method doesn't change its object
* @param[in] obj Reference to a constant MyObj
* @return A constant reference to obj
*/
const MyObj& MyObj::myMethod(const MyObj &obj) const
{
return obj;
}

C++ - Parameter Passing

Avoid passing large objects by value. This causes unnecessary copying of those objects on to the stack, slowing down performance. Instead, pass by reference (or pointer).

When needed, use prefixes "in" and "out" to avoid confusion with outgoing parameters.

Pass by value (NG=No Good):
void func(BigObject obj) // Use copy of obj
{
...
}

Pass by reference (Good):
void func(BigObject &obj) // Uses reference of obj
{
...
}

Note that smaller primitives (e.g. int, short, char) do not benefit from passing by reference because most the time, they are either equal to or smaller than the word size of the machine.

C++ - Initialization Lists

Constructing an object consists of two parts:
  1. Creating the object
    • Without initialization list: Assign default values to the object's member variables
    • With initialization list: Assign specified values in initialization list to member variables
  2. Running the constructor's body
In object creation without an initialization list, default values are assigned to the object's member data. If assignments exist inside the body of the constructor, member data will be assigned again. However, with an initialization list, specified values can be assigned in step 1 to avoid assignments in the constructor's body.

Warning: there is no guarantee on the order of assignments (see example).

Before:
MyClass::MyClass()
{
member1 = 1;
member2 = 2;
member3 = member2;
}

After:
MyClass::MyClass()
: member1(1),
member2(2)
//, member3(member2) // No guarantee member2
// was set before here
{
member3 = member2; // need to assign here
}