Original Post
Hi, I've upgraded to WIP2 and thats sorted out all my string passing problems. Now Im back to trying to get type conversion working. I'm only really intrested in converting the basic math types to string, not the other way around. I have registered my conversion function as: My first implementation of IntToString looked like this: Which doesn't work as its returning a reference to a temporary local object. So I tried the slighly more hideous: Which solves the problem of returning a local variable, but leaks memory, and doesn't work. The problem as I see it is, the VM allocates a new string to recieve the contents of the asBEHAVE_ASSIGNMENT function call, but there is no way for IntToString to know where that string is. The normal string::operator= which gets registered (and works) is working on itself (its a member function of the string object to be assigned to). I can think of two solutions. 1, use helper functions like AssignIntToString(string&,int&). Thats not really what I want to do as it means you cant write more natural expressions like file="file" + 2; 2, subclass std::string and add member functions for converting from int,float,etc. Any thoughts? Am I just missing something really obvious? Do you want bug reports of script code that triggers asserts in the compiler? If you do, Alan
engine->RegisterObjectBehaviour("string", asBEHAVE_ASSIGNMENT, "string &f(int ∈)", asFUNCTION(IntToString), asCALL_CDECL_OBJLAST);
string & IntToString(int &i)
{
stringstream stream;
stream << i;
return stream.str();
}
string & IntToString(int &i)
{
string * str = new string;
stringstream stream;
stream << i;
*str = stream.str();
return *str;
}
string a = "a";
string b = "b";
string c = "c";
string d = b + c = a;