Best way to handle storing (possibly NULL) char * in a std::string
Posted
by John
on Stack Overflow
See other posts from Stack Overflow
or by John
Published on 2010-04-30T10:38:44Z
Indexed on
2010/04/30
10:57 UTC
Read the original article
Hit count: 135
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?
© Stack Overflow or respective owner