Advertisement

Is this assignment operator correct?

Started by April 19, 2014 06:33 AM
2 comments, last by WitchLord 10 years, 7 months ago

Is the following assignment operator correct?

I am mainly wondering about if rhs should be released? Since its a const object I can't, Or does the fact it is passed by ref instead of handle means the ref count was not incremented?

Is it better to take the rhs by handle to avoid the copy of the object?


class ObjectPicker : public ScriptRef
{
public:
	std::vector<ObjectPick> mObjects;
	ObjectPicker & ScriptAssign(const ObjectPicker & rhs)
	{
		mObjects = rhs.mObjects;
		AddRef(); // because of returning object
		return *this;
	}
};
(ObjectPicker is registered as asOBJ_REF)
r = scriptEngine->RegisterObjectMethod("ObjectPicker", "ObjectPicker @ opAssign(const ObjectPicker &in)", asMETHOD(ObjectPicker,ScriptAssign), asCALL_THISCALL); assert( r >= 0 );

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

The implementation is correct with regards to how you've registered it. Since you're receiving the object by reference you shouldn't release the reference.

However, it is better to register method as returning by reference, that way you don't have to increment the reference of the returned pointer either. This way the implementation is identical to what you would do in C++, i.e.

    // Registered as "ObjectPicker & opAssign(const ObjectPicker &in)"
    ObjectPicker & ScriptAssign(const ObjectPicker & rhs)
    {
        mObjects = rhs.mObjects;
        return *this;
    }

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

Advertisement

Ah, So should I just register the =operator directly like this?


r = scriptEngine->RegisterObjectMethod("ObjectPicker", "ObjectPicker & opAssign(const ObjectPicker &in)", asMETHOD(ObjectPicker,operator=), asCALL_THISCALL); assert( r >= 0 );

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

Yes. smile.png

Unless you really want to do something different when called from the script (different error handling perhaps).

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