Constructors...

Started by
3 comments, last by WitchLord 19 years, 7 months ago
It's rather a C++ than AS question here: Suggested way to pass a class constructor to AS is as follow : void MyClass::Construct( MyClass* pThis ) { new(pThis) MyClass(); } I've several questions regarding that way : - I can't make it running under VC++6.0 (compiling error is : "error C2660: 'new' : function does not take 2 parameters"). So what I'm missing ? Do I need to overload new operator, or is there any option to enable this feature, or maybe VC doesn't support it ? - Instead of passing the pThis argument, can't we just using this ? : void MyClass::Construct() { new(this) MyClass(); } Thanks for these purely c++ tips. Lbas
Lbas
Advertisement
don't know anything about AS but in C++ you need to do a new like this

pThis = new MyClass;
That's called placement new, and you must include <new.h> (or similar) to use it, unlike the default new.
CloudLine:
Excellent !!! thanks for the tip.
In fact, I missed to include <new.h> and now it works.

Anonymous Poster:
This placement new allows to exlicitly call the object constructor.

Lbas
Lbas
Ok, so you've resolved the placement new question (thanks CloudNine for your quick answer), now it is time for the AS question :)

Since you registered the constructor behaviour function as a class method you should use the following format:

void MyClass::Construct(){  new(this) MyClass();}engine->RegisterObjectBehaviour("MyClass", asBEHAVE_CONSTRUCT, "void f()", asMETHOD(MyClass, Construct), asCALL_THISCALL);


If you want to use a global function instead of a class method you'd use the following way:

void MyClass_Construct(MyClass *pThis){  new(pThis) MyClass();}engine->RegisterObjectBehaviour("MyClass", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(MyClass_Construct), asCALL_CDECL_OBJLAST);


The difference is the calling convention. With the object method AS will pass the object pointer in ECX, which will be accessed with the 'this' pointer. With the global function AS will pass the object pointer as the last parameter (in this case the only one) on the stack.

You can freely use either asCALL_THISCALL or asCALL_CDECL_OBJLAST anywhere an object method is expected, i.e. for some of the RegisterObjectBehaviour() calls, and RegisterObjectMethod().

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

This topic is closed to new replies.

Advertisement