Registering operator behaviours

In order for AngelScript to know how to work with the application registered types, it is necessary to register some behaviours, for example for memory management.

The memory management behaviours are described with the registration of reference types and value types.

Other advanced behaviours are described with the advanced types.

Most behaviours are implemented as ordinary class methods, except with specific names that the compiler can understand.

# Operator overloads

In AngelScript all operator overloads are implemented as class methods with predefined names, which is different from C++ where both class methods and global functions may be used. Especially the dual operators, i.e. those that take two operands, usually has one implemented as a class method, and a global function for the reverse order.

To register C++ operator overloads you'll use the methods described in How to get the address of the application function or method.

Example on how to register operator overloads

class MyClass
{
...
// The operator 'MyClass - int' has been implemented as a method
MyClass operator-(int) const;
// The operator 'int - MyClass' has been implemented as a global function
static MyClass operator-(int, const MyClass &);
}
void RegisterMyClass(asIScriptEngine *engine)
{
// Registering the operator 'MyClass - int'
engine->RegisterObjectMethod("MyClass", "MyClass opSub(int) const", asMETHODPR(MyClass, operator-, (int) const, MyClass), asCALL_THISCALL);
// Registering the operator 'int - MyClass'
engine->RegisterObjectMethod("MyClass", "MyClass opSub_r(int) const", asFUNCTIONPR(operator-, (int, const MyClass &), MyClass), asCALL_CDECL_OBJLAST);
}
asIScriptEngine::RegisterObjectMethod
virtual int RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv, void *auxiliary=0, int compositeOffset=0, bool isCompositeIndirect=false)=0
Registers a method for the object type.
asCALL_CDECL_OBJLAST
@ asCALL_CDECL_OBJLAST
A cdecl function that takes the object pointer as the last parameter.
Definition: angelscript.h:234
asIScriptEngine
The engine interface.
Definition: angelscript.h:1092
asFUNCTIONPR
#define asFUNCTIONPR(f, p, r)
Returns an asSFuncPtr representing the function specified by the name, parameter list,...
Definition: angelscript.h:684
asMETHODPR
#define asMETHODPR(c, m, p, r)
Returns an asSFuncPtr representing the class method specified by class, method name,...
Definition: angelscript.h:742
asCALL_THISCALL
@ asCALL_THISCALL
A thiscall class method.
Definition: angelscript.h:232