Sign in to follow this  
Koldo

operator+ outside class definition

Recommended Posts

Hello all

I am a happy new AngelScript user and I had a question.

Implementing a class with arithmetical operations like + it happens this:

[font=courier new,courier,monospace]MyClass a, b;
...
b = a + 5; [/font][font=courier new,courier,monospace]// It works with operator+ inside class
b = 5 + a; // It would require an "outside class definition" like
// MyClass operator+(const double lhs, const MyClass &rhs) {return MyClass(lhs) += rhs;}[/font]

Do you know how to implement this [font=courier new,courier,monospace]operator+ [/font]outside MyClass?.
Do you know how to do it in case of MyClass is defined in main C++ program instead of in script?

Thank you
Koldo

Share this post


Link to post
Share on other sites
AngelScript doesn't use global operator overloads. [url="http://www.angelcode.com/angelscript/sdk/docs/manual/doc_script_class_ops.html"]Instead there are two variants of the operator overload methods[/url], one for implementing operations with the other value on the right side, and another on the left side.

[code]
// AngelScript
class MyClass
{
MyClass opAdd(int rhs) const { ... } // Used for object + value
MyClass opAdd_r(int lhs) const { ... } // Used for value + object. The postfix r in the name means (reversed)
}
[/code]

To register a C++ class with operator overloads, you can do the following:

[code]
// C++
class MyClass
{
public:
MyClass operator+ (int rhs) const { ... }
static MyClass operator+ (int lhs, const MyClass &rhs) { ... }
}

void Register(asIScriptEngine *engine)
{
engine->RegisterObjectType("MyClass", ... );
engine->RegisterObjectMethod("MyClass", "MyClass opAdd(int) const", asMETHOD(MyClass, operator+), asCALL_THISCALL);
engine->RegisterObjectMethod("MyClass", "MyClass opAdd_r(int) const", asFUNCTIONPR(MyClass::operator+, (int, const MyClass &), MyClass), asCALL_CDECL_OBJLAST);
}
[/code]

The std script string add-on gives a more complete example of how this is done.


Regards,
Andreas

Share this post


Link to post
Share on other sites
Thank you Andreas!

I read the documentation and string plugin but I did not realized how to do it. Now I understand [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] .

Finally it has has worked well for me with slight changes:

[CODE]
// C++
class MyClass
{
public:
MyClass operator+ (int rhs) const { ... }
}
MyClass operator+ (int lhs, const MyClass &rhs) { ... }

void Register(asIScriptEngine *engine)
{
engine->RegisterObjectType("MyClass", ... );
engine->RegisterObjectMethod("MyClass", "MyClass opAdd(int) const", asMETHOD(MyClass, operator+), asCALL_THISCALL);
engine->RegisterObjectMethod("MyClass", "MyClass opAdd_r(int) const", asFUNCTIONPR(operator+, (int, const MyClass &), MyClass), asCALL_CDECL_OBJLAST);
}
[/CODE]

Best regards
Koldo

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this