string assignemnt

Started by
15 comments, last by Rydis 19 years, 6 months ago
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.
Advertisement
what language?
C++? C#? Java? What language?
In C++:
string s = mystring;

in Java:
String s = mystring;

in C#:
string s = mystring;

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

its c++, didn't know you could just use =, cause thats not allowed with c style char *;

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.

makes sense thanks.
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.
std::cout << GetCharacterName().c_str() << std::endl;
So... Muira Yoshimoto sliced off his head, walked 8 miles, and defeated a Mongolian horde... by beating them with his head?

Documentation? "We are writing games, we don't have to document anything".
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.
My stuff.Shameless promotion: FreePop: The GPL god-sim.
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])
So... Muira Yoshimoto sliced off his head, walked 8 miles, and defeated a Mongolian horde... by beating them with his head?

Documentation? "We are writing games, we don't have to document anything".

This topic is closed to new replies.

Advertisement