c++ - Is there any danger of assigning a const char* to a string? -
i assigning const char* value string. checking condition , setting string empty default value.
in parameterised constructor setting string empty follows
classname:: classname(x x, string name):x(x), name(){}
i set string empty once done using
name=""; is above approach of initialisation correct?
also there risk of assigning const char* string?
const char* diag; string name; name= diag;
the class std::string has corresponding constructors , assignment operators objects of type char *. problem can arise relative objects of type when initializer null pointer.
take account if there declared variable this
const char *name = ""; then name not null pointer. pointer first character (character '\0') of array of type const char[1] corresponds "empty" string literal , has static storage duration.
if write example
std::strig s( name ); or
std::string s = name; then empty object s of type std::string becsuse there nothing copy "empty" string literal.
thus not make sense declare objects of type std::string such way. enough write
std::string s;
Comments
Post a Comment