Skip to main content
gamedev.net
🔒 Locked

Implicit conversion

Started by blubberbernd Sep 1, 2011 at 1:44 PM 3 replies 2.7k views
Original Post
blubberbernd
blubberbernd
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)?
_orm_
_orm_
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.
blubberbernd
blubberbernd
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.
WitchLord
WitchLord
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 - <a href="http://www.angelcode.com/tower" rel
WitchLord
WitchLord
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 - <a href="http://www.angelcode.com/tower" rel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.