What is the meaning of this constructor std :: string

Today I tried to learn a piece of code and I am stuck on this line.

std::vector<std::string(SomeClassInterface::*)()> ListOfFnPointers; 

what is the meaning of this constructor std :: string? I went through, but I have no idea what that means.

It is used in code as

 if (!ListOfFnPointers.empty()) { std::vector<std::string> StringList; for (auto Fn : ListOfFnPointers) { StringList.push_back((pSomeClassObj->*Fn)()); } ... } 
  • What does this declaration mean?
  • What exactly does this function do with pSomeClassObj->*Fn ?
+5
source share
3 answers

It has nothing to do with the std::string constructor.

std::string(SomeClassInterface::*)() is a type pointer to a member function , the member member belongs to the SomeClassInterface class, returns std::string , does not accept parameters.

->* operator of access to the pointer to the element (as well as .* ). (pSomeClassObj->*Fn)() will call a member function on pSomeClassObj , which must be a pointer of type SomeClassInterface* .

+5
source

This is not a constructor; it is a pointer to a function without parameters returning std :: string.

 for (auto Fn : ListOfFnPointers) { StringList.push_back((pSomeClassObj->*Fn)()); } 

Tapping back above works because (pSomeClassObj → * Fn) () is a call to these functions, and the result is std :: string.

UPDATED:

  • This is a declaration of the function std :: vector pointers. Each function belongs to SomeClassInterface, takes no parameters and returns std :: string.

  • In the case of this code (pSomeClassObj → * Fn) () calls the function of the objects pSomeClassObj, where Fn is a pointer to this function and a member of pSomeClassObj.

+4
source

If you are using C++11 , you can write code like this:

 using FunctionPointer = std::string (SomeClassInterface::*) (); std::vector<FunctionPointer> ListOfFnPointers; 

you can read this link: http://en.cppreference.com/w/cpp/language/type_alias

+3
source

All Articles