c++cstring

Safely concatenate c-strings in class constructor


I have a class that needs to concatenate two const char* strings during construction, and even use the result (concatenated string) later on in the initialization list.

const char* SUFFIX = "suffix";

class widget {
public:
    widget(const char* prefix) : key(???), cls(key) { };

private:
    const char* key;
    const important_class cls;
}

widget("prefix_"); // key should be prefix_suffix

There is a global (in widget's cpp) const char* suffix that I want to append to user supplied prefix.

How to do it?


BTW. I've heard about string. If I could use string I wouldn't ask here, about const char*


Solution

  • Use std::string as an intermediate value:

    const char* SUFFIX = "suffix";
    
    class widget {
    public:
        widget(const char* prefix) :
        intermediate(string(prefix) + suffix),
        key(intermediate.c_str()),
        cls(key) {
        }
    
    private:
        const std::string intermediate;
        const char* key;
        const important_class cls;
    }
    
    widget("prefix_"); // key should be prefix_suffix
    

    The only thing different to your code here is the private member variable, intermediate.

    The std::string object, intermediate, manages the dynamic memory needed to hold the concatenation. It will clean up nicely in the event of exceptions, assignment, copying, etc.

    As long as the string isn't mutated the const char* returned by c_str() will remain valid. Since intermediate is const, no mutating functions can be called on it, so you shouldn't have problems with cls using the internal buffer.