Advertisement

Odd thing with references

Started by February 02, 2012 09:11 PM
1 comment, last by WitchLord 12 years, 9 months ago
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.
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.
Advertisement
:)

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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement