Can somebody explain this C++ typedef?

放肆的年华 提交于 2021-02-15 11:38:39

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!