string assignemnt
how do you assign the value of a string to another string. I tried string.copy but failed. Right now i am using swap but i think that is a poor way to going at it.
C++? C#? Java? What language?
In C++:
in Java:
in C#:
In C++:
string s = mystring;
in Java:
String s = mystring;
in C#:
string s = mystring;
its c++, didn't know you could just use =, cause thats not allowed with c style char *;
thanks.
thanks.
It's certainly *allowed* with char *'s, it just doesn't do what you want.
Being an object, std::string is free to overload the operator= (and also the copy constructor). This does what you want, and aren't you glad :)
Of course, you would have the same problem with char *'s if for some reason you tried to do it with std::string*'s. Pointers aren't objects, and they have basically set-in-stone behaviour as a result.
Being an object, std::string is free to overload the operator= (and also the copy constructor). This does what you want, and aren't you glad :)
Of course, you would have the same problem with char *'s if for some reason you tried to do it with std::string*'s. Pointers aren't objects, and they have basically set-in-stone behaviour as a result.
alright how about this, how do i display a string using cout.
C:\Documents and Settings\Jamie\Desktop\Jamie\Visual C++\Character.cpp(13) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<cha
i have a function defined as
string GetCharacterName() { return Character_Name; }
Character_Name being a string also.
C:\Documents and Settings\Jamie\Desktop\Jamie\Visual C++\Character.cpp(13) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<cha
i have a function defined as
string GetCharacterName() { return Character_Name; }
Character_Name being a string also.
Quote:Original post by Zahlman
It's certainly *allowed* with char *'s, it just doesn't do what you want.
Interestingly you would get the same behaviour if you were to do String s = otherString; in Java, since all objects are referenced. It's like every object variable in Java is implicitly a pointer, and assignment and comparisons only work on the pointers, but not the objects. Thus Washu's code snippets are not equivalent. (Don't know about the C# one, myself.)
Quote:Original post by ffx
std::cout << GetCharacterName().c_str() << std::endl;
Straight std::cout << GetCharacterName() << std::endl; ought to work. If you need to explicitly get the c_str() something is broken.
Quote:Original post by DocQuote:Original post by ffx
std::cout << GetCharacterName().c_str() << std::endl;
Straight std::cout << GetCharacterName() << std::endl; ought to work. If you need to explicitly get the c_str() something is broken.
Yes, indeed. Didn't thought of that(maybe because I didn't use cout for a long time[lol])
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement