-3

In c# we can use:

function_name("This is test number: " + i);

How can I do it in c++?

Thank you guys

2
  • Your comment below says: i'm trying to call it: Send("Some text" + var) When var is char*. So is the i above supposed to be a char *? Are you trying to concatenate two strings? Or convert an integral type to a string and concatenate those two together? Most people will assume the latter given your choice of variable naming. Commented Jun 19, 2014 at 18:45
  • You should add the prototype for the function you want to call, as well as the types of the values you want to concatenate to your question. Commented Jun 19, 2014 at 19:16

3 Answers 3

5

Assuming that function_name takes an std::string argument, and you have C++11 support, you can use

function_name("This is test number: " + std::to_string(i));
Sign up to request clarification or add additional context in comments.

2 Comments

My function is int Send(char*); And i'm trying to call it: Send("Some text" + var) When var is char*
0

Let;s at first consider the expression

"This is test number: " + i

You want to get some new string that will contain the both operands of the operator +. So this string has to be allocated dynamically in memory. The only standard C++ class that allocates a memory dynamically for strings is std::string. However it has no operator + where one of operands has an integral type. So object i has to be converted to an object of type std::string explicitly. It can be done using standard function std::to_string In this case the call would look as

function_name("This is test number: " + std::to_string( i ) );

However if the function accepts only arguments of type char * then you can not use class std::string.

So what do you need?

As I mentioned you have to allocate the character array itself before calling the function. Let assume that you defined such an array that can accomodate the string literal and the number stored in i.

char s[SOME_ENOUGH_SIZE];

When you could write

std::sprintf( s, "&s %i", "This is test number: ", i );

function_name( s );

You could also allocate the array dynamically. For example

char *s = new char[SOME_ENOUGH_SIZE];

and call the function the same way as shown above.

2 Comments

@Deduplicator He may not do that because the type of the parameter is char * while the return type of c_str() is const char *.
@Deduplicator I repeat that the return type of c_str() is const char * while the type of the parameter is char *. You propose to use undefined behaviour.
0

Actually, if i were an integer, then string + i would yield a array shifted by that many elements(assuming this keeps base address in bounds, else garbage data is produced).

So, in your case if i=4, then your string is passed as " is test number: ", removing "This".

So, if want to concatenate strings you can use above solution using std::string:

function_name("This is test number: " + std::to_string(i));

EDIT: Since you have commented that i is not int so this might no more remain valid.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.