Shouldn't this work?
string s;
s = "some string";
Shouldn't this work?
string s;
s = "some string";
Yes!
It's default constructing a string, then assigning it from a const char*
.
(Why did you post this question?... did you at least try it?)
Shouldn't this work?
string s; s = "some string";
Well, actually it's spelled std::string
, but if you have a using namespace std;
(evil) or using std::string;
(somewhat less evil) before that, it should work - provided that you also have a #include <string>
at the top of your file.
Note, however, that it is wasteful to first initialize s
to be an empty string, just to replace that value in the very next statement. (And if efficiency wasn't your concern, why would you program in C++?) Better would be to initialize s
to the right value immediately:
std::string s = "some string"
or
std::string s("some string");