Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

Constructors...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Lbas   Members   -  Reputation: 122

Like
0Likes
Like

Posted 25 August 2004 - 02:56 AM

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

Sponsor:

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 25 August 2004 - 03:15 AM

don't know anything about AS but in C++ you need to do a new like this

pThis = new MyClass;

#3 CloudNine   Members   -  Reputation: 224

Like
0Likes
Like

Posted 25 August 2004 - 03:18 AM

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

#4 Lbas   Members   -  Reputation: 122

Like
0Likes
Like

Posted 25 August 2004 - 04:25 AM

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

#5 Andreas Jonsson   Moderators   -  Reputation: 3445

Like
0Likes
Like

Posted 25 August 2004 - 05:36 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS