BUG?: Operator overloading problem

Started by
2 comments, last by virious 13 years ago
I have a problem with operators overloaded in C++ and registered in AS.

I have a class called Block_Integer which is registered with flags "asOBJ_VALUE | asOBJ_APP_PRIMITIVE".

I have operator + which looks like this:


Block_Integer operator+( const Block_Integer &a, const Block_Integer &b )
{
return Block_Integer(a.m_intValue + b.m_intValue);
}


and registered like this:
engine->RegisterObjectMethod("Integer", "Integer opAdd(const Integer &in) const", asFUNCTIONPR(operator+, (const Block_Integer&, const Block_Integer&), Block_Integer), asCALL_CDECL_OBJFIRST);

My test script looks like this:
Integer intA(5), intB(7), intC;
intC = intA + intB;


When I run my script, in my operator Block_Integer argument "a" comes properly with value "5", but argument Block_Integer "b" is null. I also tried to place this operator inside my Block_Integer class (with asCALL_THISCALL calling convention) but result was the same.

Is there any issue with overloaded operators registered to AS?

I'm using AngelScript 2.20:

#define ANGELSCRIPT_VERSION 22000
#define ANGELSCRIPT_VERSION_STRING "2.20.0 WIP"
Advertisement
I'm no expert, but I think asOBJ_APP_PRIMITIVE is reserved for truly primitive C++ types and not class like your Block_Integer. I guess that you have at least an assignment operator in your class and a constructor in order to make your declaration and "intC = intA + intB" to compile, so somewhere you might wanna have a "asOBJ_APP_CLASS_*" in your declartion flag (http://www.angelcode.com/angelscript/sdk/docs/manual/angelscript_8h.html#855d86fa9ee15b9f75e553ee376b5c7af15f3dd82be0e77e05ee0dbea096bb36)
Anyway, just to be sure I tried to reproduce your problem, and I did with flags "asOBJ_VALUE | asOBJ_APP_PRIMITIVE", but it worked fine with flags "asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA"

Note to self: remember to never ever again try to paste some italic code in a message body...
I'm not aware of any issues with overloaded operators in AngelScript, but I'll investigate this to see if I can reproduce the problem you reported.Can you tell me a bit more about how the Block_Integer class is declared in the C++ application? You say it is a class, but you registered it as if it was a primitive. Quittouff is correct, asOBJ_APP_PRIMITIVE is only meant for primitive C++ datatypes (except float/double), all classes and structures should be registered with the asOBJ_APP_CLASS flag + respective flag for constructor, destructor, assignment operator, and or copy constructor.

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

I solved this problem. I just changed flags to "asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA", added assignment operator (without registering it to AS) and it works fine now. So in this case the problem were: wrong flags and missing assignment operator. And additionally I've registered the default constructor.

This topic is closed to new replies.

Advertisement