If your goal is to concatenate variables, use a std::stringstream
:
std::stringstream ss;
// Concatenar variable en un texto:
int variable = 0;
ss << "Texto " << variable;
// Mostrar resultado por consola
std::cout << ss.str() << '\n';
If your goal is "method that should return char*
" I'll tell you never to do a method like this:
In C ++ it is better to manage character strings with std::string
, not with character pointers.
If you manage a character string with pointers to character, you will have to go through all the tedium of managing the memory manually.
1
std::string concatena(const std::string &texto, int valor) {
return texto + std::to_string(valor);
}
2
char* concatena(const std::string &texto, int valor) {
auto v = std::to_string(valor);
char *resultado = new char[v.length() + texto.length() + 1];
auto p = std::copy(texto.begin(), texto.end(), resultado);
*p = ' ';
++p;
std::copy(v.begin(), v.end(), p);
return p;
}
// Si usamos 'concatenar' debemos borrar la memoria manualmente:
auto v = concatenar("Texto ", 0);
// ... usar v ...
delete[] v;