Jump to content
  • Advertisement
Sign in to follow this  
Rydis

string assignemnt

This topic is 5475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
C++? C#? Java? What language?
In C++:

string s = mystring;

in Java:

String s = mystring;

in C#:

string s = mystring;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Doc
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.


Yes, indeed. Didn't thought of that(maybe because I didn't use cout for a long time[lol])

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!