this compiles :-)
string name;
name = 1;
this does not:
string name = 1;
any thoughts?
I know that this is wrong. . . that is not the point. The first gives a smiley face.
this compiles :-)
string name;
name = 1;
this does not:
string name = 1;
any thoughts?
I know that this is wrong. . . that is not the point. The first gives a smiley face.
The first compiles because the assignment operator is called what has one signature of "string& operator= ( char c )" and the compiler can convert 1 into a char.
The second won't compile because it calls the copy constructor which has no compatible signature.
The second example is really initialization rather than assignment, i. e. it calls a constructor rather than operator=
. Evidently class string
does not have a constructor that takes an integer as an argument, but it's assignment operator is ok with it. And the reason you get a smiley face is that it is the character with the ASCII value of 1.
By the way, this is not specific to Visual Studio. Any C++ compiler should behave the same way.
Not to do with the question, but why don't you (and many others) post compilable code. Would :
#include <string>
using namespace std;
int main() {
string name;
name = 1;
string name2 = 1;
}
have been too much to ask for? Given that, we can see that "string" actually refers to std::string and not some random class.