Sign in to follow this  
BlackMoons

Is this assignment operator correct?

Recommended Posts

BlackMoons    460

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 );

Share this post


Link to post
Share on other sites
WitchLord    4677

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;
    }

Share this post


Link to post
Share on other sites
BlackMoons    460

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 );

Share this post


Link to post
Share on other sites

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