Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the meaning of this std::string constructor

Today I tried to study some piece of code and I am stuck with this line.

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

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

It is used in the code as,

if (!ListOfFnPointers.empty())
{
    std::vector<std::string> StringList;
    for (auto Fn : ListOfFnPointers)
    {
        StringList.push_back((pSomeClassObj->*Fn)());
    }
    ...
}
  1. What that declaration means?
  2. what exactly this function doing with pSomeClassObj->*Fn?
like image 368
Gilson PJ Avatar asked Aug 09 '16 06:08

Gilson PJ


People also ask

What is string constructor in C++?

It is used to construct a string object. and initializing its value depending on the constructor version used.

What is the meaning of std::string?

C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

Does std::string constructor copy?

std::string::string. Constructs an empty string, with a length of zero characters. Constructs a copy of str. Copies the portion of str that begins at the character position pos and spans len characters (or until the end of str, if either str is too short or if len is string::npos).

What type is std::string?

The std::string type is the main string datatype in standard C++ since 1998, but it was not always part of C++. From C, C++ inherited the convention of using null-terminated strings that are handled by a pointer to their first element, and a library of functions that manipulate such strings.


Video Answer


3 Answers

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

std::string(SomeClassInterface::*)() is type of pointer to member function, the member function belongs to class SomeClassInterface, returns std::string, takes no parameters.

->* is pointer-to-member access operator (and also .*). (pSomeClassObj->*Fn)() will call the member function on pSomeClassObj, which is supposed to be a pointer with type SomeClassInterface*.

like image 159
songyuanyao Avatar answered Oct 28 '22 03:10

songyuanyao


It's not a constructor it's a pointer to function without parameters returning std::string.

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

Push back's above are working because (pSomeClassObj->*Fn)() is a call to these functions and result is std::string.

UPDATED:

  1. It is declaration of std::vector of pointers to function. Each function belongs to SomeClassInterface, takes no parameters and return std::string.

  2. In case of this code (pSomeClassObj->*Fn)() calls the function of objects pSomeClassObj where Fn is a pointer to this function and member of pSomeClassObj.

like image 39
Pustovalov Dmitry Avatar answered Oct 28 '22 05:10

Pustovalov Dmitry


If you use 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

like image 3
BlackMamba Avatar answered Oct 28 '22 04:10

BlackMamba