Best way to handle storing (possibly NULL) char * in a std::string
- by John
class MyClass
{
public:
void setVar(const char *str);
private:
std::string mStr;
int maxLength; //we only store string up to this length
};
What's the best approach to implement setVar when the external code is quite likely to pass in NULL for an empty string (and cannot be changed)? I currently do something a bit like:
void MyClass::setVar(const char *str)
{
mStr.assign(str ? str : "",maxLength);
}
But it seems kind of messy. ideas?