Registering operator behaviours

You can register operator behaviours for your types as well. By doing this you'll allow the script to work with the types in expressions, just like the built-in types.

There two forms of operator behaviours, either object behaviours or global behaviours. An object behaviour is implemented as a class method, and a global behaviour is implemented as a global function.

// Registering an object behaviour
int &MyClass::operator[] (int index)
{
  return internal_array[index];
}

r = engine->RegisterObjectBehaviour("mytype", asBEHAVE_INDEX, "int &f(int)", asMETHOD(MyClass,operator[]), asCALL_THISCALL); assert( r >= 0 );

// Registering a global behaviour
MyClass operator+(const MyClass &a, const MyClass &b)
{
  MyClass res = a + b;
  return res;
}

r = engine->RegisterGlobalBehaviour(asBEHAVE_ADD, "mytype f(const mytype &in, const mytype &in)", asFUNCTIONPR(operator+, (const MyClass &, const MyClass &), MyClass), asCALL_CDECL); assert( r >= 0 );

You can find a complete list of behaviours here.


Generated on Sun Aug 17 17:11:13 2008 for AngelScript by  doxygen 1.5.6