• Advertisement
Sign in to follow this  

Is this assignment operator correct?

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

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
Advertisement

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

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

Yes. smile.png

 

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement