Advertisement

Implicit conversion

Started by September 01, 2011 01:44 PM
3 comments, last by WitchLord 13 years, 2 months ago
Hi,

I'm currently playing around with regsitering behaviour for implicit conversions. I tested this with the string to int conversion. But I need some clarification here, because I encountered a problem:

I regsitered the behavior like it is in the manual (except that I used the implicit version), I'm using the stdstring addon:
r = engine->RegisterObjectBehaviour("string", asBEHAVE_IMPLICIT_VALUE_CAST, "int f() const", asFUNCTION(stringToInt), asCALL_CDECL_OBJLAST);

Then I registered a C++ testFunction(const int&) :
void testFunction(const int&)

My test code in the script file looks like this:
string testStr = "4";

//this works
testFunction(1 * testStr)

//this DON'T work
testFunction(testStr)


In the second case I get the error that there's no testFunction(string), candiates are testFunction(int&).

Does the conversion only work in operations? How do I manage it to also have it working in function calls (pass a string to a function that needs an int, for example)?
I have the same problem, only it's the other way around with a print fuction.
void main()
{
Println(""+9001); // works
Println(9001); // doesn't
}


My advice is to just have a special function that lets you convert these things instead of relying on implicit conversion.
Advertisement
I already have convert functions that work. But I wanted to register these conveert functions as implicit conversion behavior so that AS calls them implicitly (automatically).

It would be really nice if that works somehow.
This is probably a bug. I'll look into it.

The implicit cast from primitive to object is still something I need to implement. It requires a way to register implicit constructors / factories to work.

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

Actually, it isn't a bug. The function is expecting a reference to an integer, and implicit conversions are allowed when the target type is a reference.

However, considering that the parameter is also a const, I can probably allow this conversion anyway. I'll look into this for a future release.

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