Sign in to follow this  

Copy Constructor of asOBJ_VALUE not called

This topic is 2045 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

Hi,

Another day, another problem :(

i have this object (not a POD)
[CODE]
r = engine->RegisterObjectType("JsVariable", sizeof(JsVariable), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK); assert( r >= 0 ); // CDAK stands for class constructor assignment and copy constructor as i understand

// destructor
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(JsVariable::JsVariableDestructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// default constructor
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(JsVariable::JsVariableDefaultConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// copy constructor. not called :(
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_CONSTRUCT, "void f(const JsVariable &in)", asFUNCTION(JsVariable::JsVariableCopyConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// another constuctor
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_CONSTRUCT, "void f(float)", asFUNCTION(JsVariable::JsVariableFloatConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// another constuctor
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_CONSTRUCT, "void f(const string &in)", asFUNCTION(JsVariable::JsVariableStringConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// another constuctor
r = engine->RegisterObjectBehaviour("JsVariable", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(JsVariable::JsVariableBoolConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

// == operator
r = engine->RegisterObjectMethod("JsVariable", "bool opEquals(const JsVariable &in) const", asFUNCTION(JsVariable::JsVariableEquals), asCALL_CDECL_OBJFIRST); assert( r >= 0 );

[/CODE]

I create object as a part of CScriptArray
[CODE]
asIObjectType* t = ScriptManager::getSingletonPtr()->engine->GetObjectTypeById(ScriptManager::getSingletonPtr()->engine->GetTypeIdByDecl("array<JsVariable>"));

CScriptArray* arr = new CScriptArray(1, t); // default constructor successfully called.
JsVariable js = JsVariable((string)"test !");
arr->SetValue(0, &js);
[/CODE]

when this line hits on scriptarray.cpp
[CODE]
objType->GetEngine()->CopyScriptObject(ptr, value, subTypeId);
[/CODE]
i expect copy constructor to be called.
instead none of the constructors called. JsVariable is passed as it's created by CScriptArray* arr = new CScriptArray(1, t); . (default constructor)

copy constructor is this.
[CODE]
static void JsVariableCopyConstructor(const JsVariable &other, JsVariable *self)
{
new(self) JsVariable(other);
}
[/CODE]

script function called is this (not really much related to question, but i may be wrong)
[CODE]
void UIOnCallback(string funcname, array<JsVariable> @arr) // arr is passed here correct. JsVariables come empty (default constructor), array length is correct
{
//do stuff
}
[/CODE]

i kinda post a lot of code. I just want to clear the issue as it is.
i feel like i miss something very simple. but it's been 4 hours since i started staring at it.

[b]why does not the copy constructor called?[/b]

thank you for your time

Share this post


Link to post
Share on other sites
ok solved it.

i did not expect that i need = operator to call copy constructor. (in script there is never a need for = operator)

added this

[CODE]
r = engine->RegisterObjectMethod("JsVariable", "JsVariable opAssign(const JsVariable &in)", asFUNCTION(JsVariable::JsVariableCopyConstructor), asCALL_CDECL_OBJLAST) ; assert( r >= 0 );
[/CODE] Edited by saejox

Share this post


Link to post
Share on other sites
The CopyScriptObject() method doesn't create a new instance. It expects two existing objects, and will do a value assign.

The CreateScriptObjectCopy() method is the one that creates a new instance of the object. Though even that doesn't use the copy constructor yet, it creates a new instance using the default constructor then calls the opAssign method to copy the value.

Regards,
Andreas

Share this post


Link to post
Share on other sites
I don't think this is the first time someone had this confusion with the CopyScriptObject() function. Maybe deprecate it and add a function called something like AssignScriptObject()? I think that would be a little clearer that it wraps the assignment behavior.

Share this post


Link to post
Share on other sites

This topic is 2045 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.

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