问题
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);
回答1:
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
.
回答2:
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.
回答3:
this is a pointer to a member function of OptionManager that takes a const ABString refrence and returns a bool
回答4:
It is a typedef to a pointer to member function. Please check C++ FAQ.
来源:https://stackoverflow.com/questions/2278490/can-somebody-explain-this-c-typedef