You can use a std::stringstream
for this purpose. It works almost the same way as printing all the variables to std::cout
.
Example:
#include <iostream>
#include <sstream>
#include <string>
int main()
{
// Your variables
int int_type = 5;
float float_type = 3.14;
std::string string_type = "I'm";
char char_type = ' ';
char char_arr_type[9] = "a pirate";
// Creating a new stream
std::stringstream s;
// Print all the variables to the stream
s << int_type << float_type << string_type << char_type << char_arr_type;
// retrieve the result as std::string
std::string merged = s.str();
std::cout << merged; // output: 53.14I'm a pirate
}
PS: I'm not sure what String
in your code means, C++ only has std::string
. Unless this is a typo you might have to do some casting or provide a custom operator<<(std::ostream&, const String&)
.