Your function accepts a modifiable array (not anymore, OP changed that in an edit) of char
and std::string
is not an array of char
, but some unrelated object (that provides read-access to its internal char
buffer, but that does not make it some kind of pretty array).
Additionally, using .c_str()
-pointers into destroyed string objects is a common bug. Even if your function was to accept a const char*
instead, you need to be aware that the pointer passed into it would only be valid until the end of the full expression the temporary std::string
object was created in. This might or might not be what you want here, but is something you really need to watch out for. As I said, people get it wrong quite often.
So std::string
probably (in the new const char*
setting, it might) is not the right tool for this job as it is described right now.
The best solution would be to make the argument of foo()
an std::string
(of some reference variant, depending on what it is doing). Then you can concatenate the inputs with +
as long as one of the first summands already is an std::string
.
If this should not be possible, copy the characters into an std::vector<char>
which actually is the C++ way to get a char
array (again, unlike string
).