The memory management behaviours are described with the registeration of registering reference types and value types.
Other advanced behaviours are described with the advanced types.
Only a few operators have special behaviours for them, the other operators are registered as ordinary class methods with predefined names.
// Simple implementation of the index operator int &MyClass::operator[] (int index) { return internal_array[index]; } // Non-mutable variant that works on const references to the object const int &MyClass::operator[] (int index) const { return internal_array[index]; } // Register both the const and non-const alternative for const correctness r = engine->RegisterObjectBehaviour("mytype", asBEHAVE_INDEX, "int &f(int)", asMETHODPR(MyClass, operator[], (int), int&), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectBehaviour("mytype", asBEHAVE_INDEX, "const int &f(int) const", asMETHODPR(MyClass, operator[], (int) const, const int&), asCALL_THISCALL); assert( r >= 0 );
By registering the behaviour either as asBEHAVE_VALUE_CAST or asBEHAVE_IMPLICIT_VALUE_CAST you let AngelScript know whether the behaviour may be used to implicitly cast the type or not.
// Convert a string to an int int ConvStringToInt(const std::string &s) { return atoi(s.c_str()); } // Register the behaviour r = engine->RegisterObjectBehaviour("string", asBEHAVE_VALUE_CAST, "int f() const", asFUNCTION(ConvStringToInt), asCALL_CDECL_OBJLAST); assert( r >= 0 );
The return type for the cast behaviour can be any type except bool and void. The value cast is meant to create a new value, so if the function returns a reference or an object handle make sure it points to a new value and not the original one.
The object constructors and factories also serve as alternative explicit value cast operators, so if a constructor or factory is already available then there is no need to register the explicit value cast operator.