Advertisement Jump to content
Sign in to follow this  
BlackMoons

Is this assignment operator correct?

This topic is 1732 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!