• Advertisement
Sign in to follow this  

Constructors...

This topic is 4895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
don't know anything about AS but in C++ you need to do a new like this

pThis = new MyClass;

Share this post


Link to post
Share on other sites
That's called placement new, and you must include <new.h> (or similar) to use it, unlike the default new.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement