Here is the code for me to register a new type with its constructor, deconstructor, copy constructor. Assignment operator. Here are some parts of code. I did not include all the registration. If u need more information, let me know.
Note: I write * operator return by reference is for testing purpose.
nRet = (asERetCodes) pScriptEngine->RegisterObjectType("UniType", sizeof(UniType), asOBJ_VALUE | asOBJ_APP_CLASS_CDA /*asOBJ_APP_CLASS_CONSTRUCTOR | asOBJ_APP_CLASS_DESTRUCTOR | asOBJ_APP_CLASS_ASSIGNMENT*/);
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(uniTypeDefaultConstructor), asCALL_CDECL_OBJLAST);
assert( nRet >= 0 );
//nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("_String", asBEHAVE_CONSTRUCT, "void f()(const _String ∈)", asFUNCTION(stringCopyConstructor), asCALL_CDECL_OBJLAST);
//nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("_String", asBEHAVE_DESTRUCT, "void f(int)", asFUNCTION(stringINTConstructor), asCALL_CDECL_OBJLAST);
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT, "void f(const UniType ∈ )", asFUNCTION(uniTypeCopyConstructor), asCALL_CDECL_OBJLAST);
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT, "void f(longlong )", asFUNCTION(uniTypeLongConstructor), asCALL_CDECL_OBJLAST);
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT, "void f(double )", asFUNCTION(uniTypeDoubleConstructor), asCALL_CDECL_OBJLAST);
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT, "void f(const _String ∈ )", asFUNCTION(uniType_StringConstructor), asCALL_CDECL_OBJLAST);
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT, "void f(const DisplayType )", asFUNCTION(uniType_DisplayTypeConstructor), asCALL_CDECL_OBJLAST);
assert( nRet >= 0 );
..................................
nRet = (asERetCodes) pScriptEngine->RegisterGlobalBehaviour(asBEHAVE_DIVIDE , "UniType f(const UniType ∈, int64)", asFUNCTIONPR( globalOperatorDiv, ( UniType &, long long), UniType), asCALL_CDECL);
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterGlobalBehaviour(asBEHAVE_MULTIPLY , "UniType &f(const UniType ∈, int64)", asFUNCTIONPR( globalOperatorMult, ( UniType &, long long), UniType&), asCALL_CDECL);
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterGlobalBehaviour(asBEHAVE_SUBTRACT , "UniType f(const UniType ∈, int64)", asFUNCTIONPR( globalOperatorMinus, ( UniType &, long long), UniType), asCALL_CDECL);
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterGlobalBehaviour(asBEHAVE_ADD , "UniType f(const UniType ∈, int64)", asFUNCTIONPR( globalOperatorPlus, ( UniType &, long long), UniType), asCALL_CDECL);
assert( nRet >= 0 );
I think, from my reading, const + ∈, will not create a copy of object. Is it?
But in my case, it is always created a new object, either by assignment operator or copy constructor.
So, when will angel script call copy constructor, when did angel script call = operator?
Here is some experiences from coding:
UniType a(0); UniType b(3); a = a + 1;
I got an error:
ExecuteString (1, 35) : ERR : No conversion from 'UniType&' to match type avaiable.
UniType a(0); UniType b(3); a = a - 1;
I will go to operator - first, then I go into the copy constructor. is that means I assign the a(a-1)?
UniType a(0); UniType b(3); a = a * 1;
Note: the return of operator * is by reference.
I go to operator * firstly, and then go to operator = twice. is that mean:
UniType temp = a * 1; a = temp;
UniType a(0); UniType b(3); a = a/1;
I got the same result as - operator.
UniType buffer(DISPLAY_DOUBLE_VARYDECISION_POINT);
bool neg() {
// Just to get a handle, this can make sure that we have a handle.
// The result.Value is not changed.
_Save@ result = current['RESULT'];
// result.Value is a attribute of _Save handle.
buffer = result.Value;
result.Value = buffer * -1;
return true;
}
This is the section to be build in the script engine.
Then in the program I called:
neg();
It get unpredicatable results.
it first coming to operator =, which I believe is this sentence:
buffer ] result.Value
, then it use a default constructor to create a temp object, and then called operator = to assign value to temp. and then use temp to * -1; Then it did not assign value back to result.Value, since not break on the operator.
Another kind of sequence would be: result.Value = buffer; result.Value = result.Value * -1;
But the result.Value suppose not changed, so, it should never come to default constructor in the mid edge.
Can you make this clear for me plz? By the same. The copy constructor and assignment operator are working differently, I need to know when the script will call which, otherwise, my results are totally wrong.