Jump to content
  • Advertisement
Sign in to follow this  
codehunter13

std string problem

This topic is 4859 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

Hi all, I use the following function:
void test(std::string& str)
{
    str = "aa"
}

The assignement works as long as str is empty. When str is not empty then is str ??? after the assignement. thx in advance

Share this post


Link to post
Share on other sites
Advertisement
Unless I'm missing something, str, and whatever string is being passed, would become "aa" because the string is being passed by reference (std::string& str).

Share this post


Link to post
Share on other sites
yes that work as long as the string is empty when calling this function.If str is not empty when calling the function and i do the assignement, str = ???.

Share this post


Link to post
Share on other sites
It should overwrite whatever is in the memory address of the string being passed to the function. For example:

void test(std::string& str)
{
str = "aa";
}
std:string name = "bb";
test(name);

The name string would contain the value "aa" due to the fact that it's memory address (passed by reference - &) was used for the str string variable. By modifying the contents of str, the contents of whatever string is passed to the function will be modified at the same time.

Share this post


Link to post
Share on other sites
Firstly, the line 'str = "aa"' should be
str = "aa";
Apart from that it works fine for me. 'str' should equal to "aa" even if it originally had been assigned a value.

Share this post


Link to post
Share on other sites
Quote:
Original post by codehunter13
yes that work as long as the string is empty when calling this function.If str is not empty when calling the function and i do the assignement, str = ???.


Wait, are you talking about in the deugger when you are debugging? If that's the same, that seems normal, because the variable went out of scope after the assigment if you set a breakpoint on that line. To get around that with Visual Studio:

void test(std::string& str)
{
str = "aa";
str = str; // <--- Breakpoint here to see the current contents
}

Share this post


Link to post
Share on other sites
If you happen to be using Visual Studio.NET or .NET 2003, there is a problem in the debugger where it will not always display the actual contents of a string. Try writing your string to a file or to the screen - it may be that your code is working fine but the debugger is not.

Share this post


Link to post
Share on other sites
That's fine, but I had a similar problem where the debugger worked fine for a little while but would die at seemingly random times.

Share this post


Link to post
Share on other sites
Quote:
Original post by codehunter13
no it's not due to the debugger because i can see it works when str is empty

That's not necessarily relevant. In MSVC at least std::string can store the string in a couple different ways. Some versions of the debugger can't properly display strings if they're stored in some ways.

Try doing OutputDebugString(str.c_str()) and see what you get that way.

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!