David Johnstone
David Johnstone

Reputation: 24470

Can somebody explain this C++ typedef?

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);

Upvotes: 11

Views: 704

Answers (4)

Vijay
Vijay

Reputation: 67319

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.

Upvotes: 9

Andy
Andy

Reputation: 1045

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

Upvotes: 2

rerun
rerun

Reputation: 25505

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

Upvotes: 4

sth
sth

Reputation: 229914

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.

Upvotes: 26

Related Questions