Jump to content
  • Advertisement
Sign in to follow this  
m0ng00se

VC++ returning a value in a function

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

okay I have a weird problem I can't solve. I have a function that reads in text from a txt file into a std::string. that function is fairly complex because it does a lot of string parsing but eventually I get the text I want in a std::string variable. I then need to get that text into another part of my program where it is displayed in an Edit Box (Win32 API, VC++ 2008). My problem is getting the string value from one part of my code into another. I know global variables are bad so I'm trying to return the value through a function. That should be easy but it isn't working at all and I don't know why. If I write ... myvariable = "this is a test string", return myvariable; then it works fine. If I try to return the original variable (var1) it loses it when it processes ... return var1; It also loses the value and returns an empty string if I go ...myvariable = var1; return myvariable; In a WATCH window I can see that it holds the value just fine and/or initialises just fine but loses the value as soon as it hits the "return" code. Remember though that if I go myvariable = "any string I like"; actually in the code itself then it returns the value just fine. Here is the code snippet: //the code below calls a function elsewhere to get a string //that is returned then converted to char* to be used in an Edit Box const std::string &stdString = Saphire::globaltest(mybuffer);// this is the call char *charPtrString = new char[stdString.size()+1]; std::strcpy(charPtrString, stdString.c_str()); ....etc //below is the function called std::string Saphire::globaltest(std::string mybuffer) { Saphire saphire; saphire.load_data(); saphire.start(); //var1 is loaded up inside this function // mybuffer = var1; //this doesn't work //var1 is a string read in from a text file mybuffer = "the pink goose"; //this works fine and returns value return mybuffer; } //in the WATCH window I can see that mybuffer initialises fine with var1 //but loses the value as it processes the "return mybuffer;" code hmmm, can somebody remind me please how to put the code in a nice code box when I use this forum. m0ng00se

Share this post


Link to post
Share on other sites
Advertisement
Looks like you're creating another copy of Saphire inside the Saphire object you created. The inner Saphire object loads the text and sets its own var1, not the outside object's. (Unless var1 is a static variable, but you haven't stated as much)

Try something like this:


std::string Saphire::globaltest(std::string mybuffer)
{
this->load_data(); //or just load_data()
this->start(); //var1 is loaded up inside this function

mybuffer = var1; //this doesn't work //var1 is a string read in from a text file
//mybuffer = "the pink goose"; //this works fine and returns value
return mybuffer;
}






[Edited by - skyfire360 on January 15, 2008 11:07:21 PM]

Share this post


Link to post
Share on other sites
Quote:

const std::string &stdString = Saphire::globaltest(mybuffer);// this is the call
You're making a reference to a variable that no longer exists.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:

const std::string &stdString = Saphire::globaltest(mybuffer);// this is the call
You're making a reference to a variable that no longer exists.

But it's a const reference, which extends the lifetime of the temporary to the lifetime of the reference.
(http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378.entry)

Share this post


Link to post
Share on other sites
Okay haven't tried that code you gave me yet but just to make things clearer. I have tried all sorts of things to make it work such as declaring the variable as a static string in the header file or making "var1" a global variable sort of by declaring it outside any function then loading it into "mybuffer" just before the return statement. But nothing works.

On that subject I could "cheat" and use a global variable if I could even get that working. What I mean is that if I go mybuffer = var1, return mybuffer; then it loses the string on return in mybuffer but var1 still holds it as I can see in the WATCH window. However if I try and access var1 from inside other functions they just go "variable var1 undefined"... yet I can see it in the WATCH window still holding the string I want but can only access it from functions on the same *.cpp page that I declared the variable.

Is there some syntax I don't know for accessing it elsewhere (extern?) although I'd rather not use a global and would prefer that my function returned the value properly.

m0ng00se

Share this post


Link to post
Share on other sites
Hmm... I think that skyfire360 might be onto something.

The globaltest function appears to belong to objects of the type Saphire. Within globaltest, you create a new Saphire object, and load data into a variable in it. However, you appear to be accessing not that variable, but the variable that exists within the object whose method is being called.

Given that it shows the appropriate data, your debugger may simply be guessing correctly at which version you want.

PS:
I believe that the tags used for source code are
[ source ] ... [ /source ]

(The tag [ code ] ... [ /code ] may also work for short snippets, but I don't think that it provides the syntax highlighting that source tags do.)

(Note: additional spaces have been added to the above tags in order that they not be interpreted as tags by the forum.)

Further information on the tags, I believe.

Share this post


Link to post
Share on other sites
Thanks guys, you're correct.

I'm constructing and deconstructing inside the saphire construction/deconstruction for some unknown reason though it still doesn't explain why I can't load var1 as an external global variable and return it through just the last function called. I did it that way because I was getting "tried to call non static member function" errors when I just used loadData(); as opposed to Saphire::loadData() or this->loadData();

Anyway I solved it by just using an external global variable. I couldn't get that working before because I was declaring var1 in a header file then calling it with extern from the *.cpp file instead of declaring it as extern in the header and then declaring it once normally in the *.cpp file. I had it all back to front. I plead lack of sleep.

I don't like using global variables at all, it sort of defeats the whole concept of OOP but I've wasted enough time (two days) on trying to return that one string so I'll just cheat for now with a global so I can get back to my animation code. I'll try to tidy it up later when I've got more time and more knowledge. Lot of animation stuff to do yet.

m0ng00se

Share this post


Link to post
Share on other sites
Stop.

You are doing huge amounts of very strange things.

Do you want 'mybuffer' to be modified or not, in the calling context? If so, you need to pass a non-const reference. If so, what is the point of assigning to the input parameter 'mybuffer' and then returning that? Just return what you want to return.

I am pretty sure that this is not a case where the const reference will extend the lifetime of a temporary, but even then, what purpose is served by assigning the function result to a reference? Just store it in a value.

The "constructing another Saphire object within the first one" objection is an obvious red herring. There is no "first" object here; 'globaltest' must be a *static* member function, because of how it is being called. However, I still can't figure out WTF you are doing. Where is var1?.

There is absolutely no need to allocate a char buffer like that to hold a converted std::string. Just pass the .c_str() of the string to the edit-box-manipulating function.

Finally, there is no such language as "VC++". Returning values from functions does not work differently simply because Microsoft wrote your IDE.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
I am pretty sure that this is not a case where the const reference will extend the lifetime of a temporary, but even then, what purpose is served by assigning the function result to a reference? Just store it in a value.

http://herbsutter.spaces.live.com/Blog/cns%212D4327CC297151BB%21378.entry

Pretty simple example given there, but as you can see, it does not apply to member variables.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!