Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Is this assignment operator correct?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 BlackMoons   Members   -  Reputation: 427

Like
0Likes
Like

Posted 19 April 2014 - 12:33 AM

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


Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3293

Like
0Likes
Like

Posted 19 April 2014 - 03:19 PM

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

#3 BlackMoons   Members   -  Reputation: 427

Like
0Likes
Like

Posted 20 April 2014 - 05:22 AM

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


#4 Andreas Jonsson   Moderators   -  Reputation: 3293

Like
0Likes
Like

Posted 20 April 2014 - 09:17 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS