tags:

views:

382

answers:

5

I've just started working with C++ after not having worked with it for quite a while. While most of it makes sense, there are some bits that I'm finding a bit confuddling. For example, could somebody please explain what this line does:

typedef bool (OptionManager::* OptionHandler)(const ABString& value);
+25  A: 

It defines the type OptionHandler to be a pointer to a member function of the class OptionManager, and where this member function takes a parameter of type const ABString& and returns bool.

sth
+4  A: 

this is a pointer to a member function of OptionManager that takes a const ABString refrence and returns a bool

rerun
+2  A: 

It is a typedef to a pointer to member function. Please check C++ FAQ.

Andy
+9  A: 
typedef bool (OptionManager::* OptionHandler)(const ABString& value);

Let's start with:

OptionManager::* OptionHandler

This says that ::* OptionHandler is a member function of the class OptionManager. The * in front of OptionHandler says it's a pointer; this means OptionHandler is a pointer to a member function of a class OptionManager.

(const ABString& value) says that the member function will take a value of type ABString into a const reference.

bool says that the member function will return a boolean type.

typedef says that using "* OptionHandler" you can create many function pointers which can store that address of that function. For example:

OptionHandler fp[3];

fp[0], fp[1], fp[2] will store the addresses of functions whose semantics match with the above explanation.

Vijay Sarathi
Nice that you break the type into smaller pieces, but "`::* OptionHandler` is a member function": why not a plain member? This is only detected when looking at the surroundings.
xtofl
A: 

FYI: don't do such a stupid mistake that I did :)

typedef bool (* OptionManager::OptionHandler)(const ABString& value);
minjang