I have a line of code that seems to convert a char array to a string
foobar(string("text")+anotherString);
foobar expects a std::string as an argument
I’ve never seen a conversion done this way… what function is being called on “text”. or is it some tricky way of casting?
std::stringhas a constructor that takes a char-array* (assumed null-terminated), which I’m sure you have seen before:So
std::string("test")just creates a temporary string object with value"test".Furthermore, there’s a free
operator+forstringandconst char *which appends the data in the char-array (again assumed null-terminated) to the string.Equivalently you can write
std::string("test").append(anotherString), for the same effect (i.e. a temporary containing the two strings, concatenated).For a list of operations supported by
std::string, consult any decent manual.*) or rather, “a pointer to the first element of an array of chars”