Jump to content
  • Advertisement
Sign in to follow this  
Quittouff

Odd thing with references

This topic is 2512 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,

I got a very odd thing going on with my strings when I use const references in the script.
I exposed these methods:
const CString& GetString(const CString&in);
void SetText(const CString&in);

then in the script i do something like:
SetText(GetString("foo"));

All is right until the destructor is called on the string returned by GetString and it destroys the original string. I can't understand why it would call the destructor when I only use references.

The thing is, I use an 'old' version of AngelScript (v2.22.0 WIP changelist 1010). If this issue is corrected after my version, tell me. I didn't change to newer version because I'd like to have a "final" version as my shipping date approach.

Share this post


Link to post
Share on other sites
Advertisement
Okay, I'm just dumb, but the solution may help others in the future:
By looking to the byte code, I realized that the return of GetString is copied and stored in a temp local variable. My String class was declared as POD with no copy constructor so the string was copied "as is". Removing the POD flag and adding a CopyConstructor did the trick and solved my problem as the copy is now correct.

Share this post


Link to post
Share on other sites
:)

Indeed. One need to be careful with the POD flag. A class might look like a POD structure in C++ if no constructor or copy operator has been explicitly declared, but if the class has any non-POD members the class itself also cannot be considered a POD.

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!