I was working on one my projects, and while I was making the constructor for a class, I was setting some of my variables to a default value. I went to set an std::string
to NULL
, and it gave me an error. But when I define an std::string
and set it to NULL
on the same line, it works without any errors. I was wondering why
First Example
std::string text = NULL;
worked, and
Second Example
std::string text;
text = NULL;
didn't work. Now I know you shouldn't set a string to NULL, or 0, but I found this on accident.
Does the first example call a constructor that takes a char*
, and thinking that 0 is a pointer to a char? I thought =
called a constructor too, so I don't get why they wouldn't both work, unless std::string
specifically overloads the =
operator.
I am using Microsoft Visual Studio Express 2013