Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safely concatenate c-strings in class constructor

Tags:

c++

cstring

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*

like image 253
Xlaudius Avatar asked Oct 26 '25 03:10

Xlaudius


2 Answers

Using std::string makes your problem a trivial task:

const std::string SUFFIX = "suffix";

class widget {
public:
    widget(std::string const & prefix) 
           : key(prefix + SUFFIX), cls(key)
    { }       // ^^^^^^^^^^^^^^concatenation!

private:
    const std::string key;
    const important_class cls; 
}

widget("prefix_");

If you need const char*, you could still get it by callng key.c_str() which returns const char*. So in your case, it would give you c-string "prefix_suffix".

Also note that the order of declaration is important which you've done correctly : cls must be declared after key as its construction depends on key.

like image 89
Nawaz Avatar answered Oct 27 '25 17:10

Nawaz


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.

like image 44
Peter Wood Avatar answered Oct 27 '25 17:10

Peter Wood