Rik Smits
Rik Smits

Reputation: 63

C++ compose string in one line to give as parameter

In my program I have a program that takes a std::string as parameter. When I call this function I want to give it a large (about 5) composition of strings. Is there a native toString() function that can spit out strings#? Can it be done on one line?

What I want:

std::string a = "sometext";
std::string b = "someothertext";
somefunction(ToString(a+b+"text"));

Upvotes: 0

Views: 202

Answers (2)

NathanOliver
NathanOliver

Reputation: 180630

std::string already has a operator+ that will concatenate strings. If you have

void foo(std::string some_name) { code in here }

And you want to pass it a combination of a bunch of string you can just use

foo(some_string + another_string + "some text" + even_another_string);

If all of your strings that you want to pass a literal strings then you will either have to add the custom string literal to one of them or convert one to a string

foo("this is a string"s + "another string" + "some more text");
//or
foo(std::string("this is a string") + "another string" + "some more text");

Upvotes: 1

rozina
rozina

Reputation: 4232

This works aswell:

std::string a = "sometext";
std::string b = "someothertext";
somefunction(a + b + "text");

Upvotes: 3

Related Questions