Saturday, January 3, 2009

creating defines for C++ function pointers

Use defines to create function pointer typedefs

// *********************************************************************
// function pointers
// *********************************************************************
// the type name is THandler

typedef CLiTool::ECLiError (CLiArgumentList::*THandler)(const RefCLiArgumentBase&);
^^^^^^^
The type is THandler

Use it like this to avoid a complex function pointer as a param.

// *********************************************************************
// CLiArgumentEmpty class
// *********************************************************************
CLiArgumentEmpty::CLiArgumentEmpty(const RefCLiArgumentList& parent,
const RefTVec_RefString& keyWordList,
const THandler& handler,
TBool multiple)


The compiler can return unhelpful error messages related to syntax errors in defines containing function pointers

[cc] cslcCLI_Typedef.h:321: error: ISO C++ forbids declaration of ‘ECLiError’ with no type
[cc] cslcCLI_Typedef.h:321: error: typedef ‘NSCLi::ECLiError’ is initialized (use __typeof__ instead)
[cc] cslcCLI_Typedef.h:321: error: expected unqualified-id before ‘*’ token
[cc] cslcCLI_Typedef.h:321: error: ‘THandler’ was not declared in this scope
[cc] cslcCLI_Typedef.h:321: error: expected ‘,’ or ‘;’ before ‘(’ token

The above was caused by the missing scope qualifier before the return value in the define.

typedef ECLiError (CLiArgumentList::*THandler)(const RefCLiArgumentBase&);

instead of

typedef CLiTool::ECLiError (CLiArgumentList::*THandler)(const RefCLiArgumentBase&);

No comments:

Post a Comment