I need a way to get this PHP behaviour in C++:
$foo = "PHP";
$bar = "this is a " . $foo . " example.";
Is there something close to that, or do I have to do lots of strcat?
Easy enough with std::string:
std::string foo = "C++";
auto bar = "this is a " + foo + " example.";
Just make sure one of the first two operands is a std::string, not both const char * or something.
As noted below, this result is being used in CreateProcess as a char * (LPSTR) argument. If the argument was const char *, c_str() would be perfectly acceptable to pass in. However, it is not, which means you should assume it modifies the string. MSDN says this:
The Unicode version of this function, CreateProcessW, can modify the contents of this string.
Since this is char *, it's evidently using CreateProcessA, so I'd say a const_cast<char *> should work, but it's better to be safe.
You have two main options, one for C++11 and later, and one for pre-C++11.
std::string's internal buffer is now guaranteed to be contiguous. It's also guaranteed to be null-terminated. That means you can pass a pointer to the first element:
CreateProcess(..., &str[0], ...);
Make sure the function only overwrites indices within [0, size()) in the internal array. Overwriting the guaranteed null-terminator is not good.
std::string is not guaranteed to be contiguous or null-terminated. I find it best to make a temporary std::vector, which guarantees the contiguous part, and pass a pointer to its buffer:
std::vector<char> strTemp(str.begin(), str.end());
strTemp.push_back('\0');
CreateProcess(..., &strTemp[0], ...);
Also note MSDN again:
The system adds a terminating null character to the command-line string to separate the file name from the arguments. This divides the original string into two strings for internal processing.
That seems to suggest that the null-terminator here isn't necessary, but there's no size parameter, so I'm not completely sure.
std::string("this is a") to ensure it's a string?operator+(const char *, operator+(const std::string &, const char *)). The inner returns a std::string."this is a " + foo is the first argument to result + " example.". I'm just hopelessly confusing myself.cannot convert 'std::string' to 'LPSTR' when I use the concatenated string? I've tried converting with c_str() but then I get invalid conversion from 'const char*' to 'LPSTR'.static_cast<LPSTR>(somestring.c_str()) should work - but don't use that if the string is being modified by the API function).Yes, you can use std::string:
std::string foo = "PHP";
std::string bar = std::string("This is a") + foo + std::string(" example.")
#include <string> otherwise you'd get some errorsIn C++, you can use std::string:
std::string foo = "C++"
std::string bar = std::string("this is a") + foo + " example.";
You need the std::string(...) to make the first string into a std::string, since otherwise it's a const char *, which doesn't have operator+ to join it with string.
There are probably at least 5 other possible ways to do this, like almost always in C++.
[Again being too slow in my typing]
If you are using C++ with the standard C++ library, you can use a std::stringstream to accomplish that. The code would look something like this:
#include <sstream>
std::string const foo("C++");
std::stringstream bar;
bar << "this is a " << foo << " example";
std::string const result(bar.str());
If for some reason you cannot use the C++ standard library you are unfortunately stuck with the likes of strcat.
C++ provides the string class.
string foo = "PHP";
string bar = string("this is a ") + foo + string(" example.");
std::string, but will fail to concatenate string literals. Sadly (or luckily, depends on who you ask), string literals in C++ are notstd::string, but arrays of constant characters.