Chap
Chap

Reputation: 3837

C++ string/char* concatenation

I wrote a small function to accept a "string" and log it.

void
IPC::bomb (char * msg) {  /* etc */ }

This instance of calling it doesn't compile:

bomb( __FUNCTION__": socket() call failed: " + strerror(errno));

Diagnostics:

./c/IPC.cpp:26: error: expected ')' before string constant
./c/IPC.cpp:26: error: invalid conversion from 'const char*' to 'char*'

I'm very confused about how to work effectively with quoted literals, std::string, char*, and how const-ness figures into it. How do I solve the above problem? And in general, are there some rules of thumb about string concatenation, esp. when mixing char* and strings?

UPDATE 1: there may be another issue at work: the C preprocessor shows this expansion:

bomb( std::string(__FUNCTION__ ": socket() call failed: ") + strerror((*__errno_location ())));

Upvotes: 1

Views: 2410

Answers (4)

nio
nio

Reputation: 5289

Edit: i could not simply concatenate __FUNCTION__ "somestring" by placing them together. I was using mingw compiler.

So to be safe, concatenate all three parts together like this:

bomb( string(__FUNCTION__) + ": socket() call failed: " + string(strerror(errno)) );

Change void IPC::bomb (char * msg) into void IPC::bomb (const char * msg) to get rid of the second error so it can accept constant string that are to be protected from any modification.

Upvotes: 2

Mats Petersson
Mats Petersson

Reputation: 129524

If you change IPC::bomb so that it is IPC::bomb(const std::string& msg), then you could do:

bomb(std::string(__FUNCTION__) + ": socket() call failed: " + strerror(errno));

and not have any errors.

Here's a complete program to do somehting similar:

#include <string>
#include <iostream>
#include <cstring>


void func(std::string str)
{
    std::cout << str << std::endl;
}

int main() {
    func(std::string(__FUNCTION__) + ":Some string " + strerror(2)); 
    return 0;
}

Upvotes: 1

Ivan Smirnov
Ivan Smirnov

Reputation: 4435

Your function signature is designed to accept char*. However, the value "string1" + ... is a const char*, because it is a temporary object. Change your function signature to IPC::bomb(const char* msg), and it should be OK.

By the way, you cannot just concatenate two C-style strings using operator+, because it just performs pointer arithmetic. Try using std::string instead, casting every string you have to this type.

So, this example could look like

void IPC::bomb (const std::string& msg) {  /* etc */ }

bomb(std::string(__FUNCTION__": socket() call failed: ") + 
     std::string(strerror(errno)));

if you use strings.

Upvotes: 4

pattivacek
pattivacek

Reputation: 5833

You cannot concatenate c-strings (char* strings) with the + operator (unless you were to write an overloaded operator to do so). You'll have to create a new character string long enough to contain the two that you want to combine and then manually copy them over with memcpy, strncpy or something similar.

Upvotes: 1

Related Questions