Jump to content
  • Advertisement
Sign in to follow this  
Alundra

Clone functions

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

Hi,

All clone functions should fully clone ? Meaning return an instance of the class with variables copied as well ?

Actually I mix full copy with only instance but to be coherent if one function fully clone all other should.

The second option should to have 2 functions Clone and CloneCompletely, but is it the good option ?

Sometimes you only need to have the instance and then read from file the data.

What do you think about that ?

Edited by Alundra

Share this post


Link to post
Share on other sites
Advertisement
It also depends on the language you're using. Shallow clones can be fine in garbage-collected languages like C# or Java, assuming that having two objects point to the same internal objects is ok (this may cause threading issues, unexpected behavior when contents of object A change when you modify B, etc)

It gets much more dangerous in something like C++ if you're using raw pointers as you may try to then double-delete the shared object (can be mitigated with shared_ptr or similar).

Share this post


Link to post
Share on other sites

It also depends on the language you're using. Shallow clones can be fine in garbage-collected languages like C# or Java, assuming that having two objects point to the same internal objects is ok (this may cause threading issues, unexpected behavior when contents of object A change when you modify B, etc)

It gets much more dangerous in something like C++ if you're using raw pointers as you may try to then double-delete the shared object (can be mitigated with shared_ptr or similar).

This depends on the usage pattern in C++, if your objects are only holding on to references to other objects the shallow pointer value copy is fine.  I would only provide a copy function into a class when the deep copy is needed and just rely on the compiler provided one if a  shallow copy will do.

Share this post


Link to post
Share on other sites

It also depends on the language you're using. Shallow clones can be fine in garbage-collected languages like C# or Java, assuming that having two objects point to the same internal objects is ok (this may cause threading issues, unexpected behavior when contents of object A change when you modify B, etc)

It gets much more dangerous in something like C++ if you're using raw pointers as you may try to then double-delete the shared object (can be mitigated with shared_ptr or similar).

This depends on the usage pattern in C++, if your objects are only holding on to references to other objects the shallow pointer value copy is fine.  I would only provide a copy function into a class when the deep copy is needed and just rely on the compiler provided one if a  shallow copy will do.


You still have to be conscious of the problem. If you're doing older C++ style coding (i.e. raw pointers allocated in constructor, deallocated in destructor) then you have to be very conscious that shallow copies will kill you.

Modern C++ coding with unique_ptr and its siblings make this much less of a problem, at least in terms of the double delete issue.

And again, shallow copies have problems that no language will mitigate (as mentioned before, threading issues, changing data behind another object you may have thought was unrelated, etc). Shallow copies do end up being faster then deep copies though - so just be conscious of what you're doing and why. (Some languages make this easier than others)

If I was writing a Clone function, then I would expect the resulting object from the clone to be completely independent - and I would expect the cost of clone to be potentially higher as a result. In other words, a function called "Clone" should always be a deep copy. Anything else isn't really "cloning". (IMHO) Edited by SmkViper

Share this post


Link to post
Share on other sites

The problem is sometimes you only need to have :

inline virtual IBaseClase* Clone() const override
{
  return new Class;
}

Because you will read the data from a file, a deep copy will copy then you will read from the file, the first pass is not needed.

But if you have an undo command which need to copy an object you need the deep copy.

Another case is to clone a property, "clone" here has only one meaning : deep copy.

Edited by Alundra

Share this post


Link to post
Share on other sites

The problem is sometimes you only need to have :



inline virtual IBaseClase* Clone() const override
{
  return new Class;
}
Because you will read the data from a file, a deep copy will copy then you will read from the file, the first pass is not needed.
But if you have an undo command which need to copy an object you need the deep copy.
Another case is to clone a property, "clone" here has only one meaning : deep copy.


That's not a clone, that's a factory. Used for different things smile.png

Clone when you want a clone of an object you have - factory for when you want a new object instance which you will fill out later (or fill out based on constructor parameters passed to the factory).

Share this post


Link to post
Share on other sites

Yes it's part of a factory but for the property system the clone function is also used when undo command need to store a copy to redo the command.

This is where all start about the question, sometime only a simple instance is needed and sometimes a full copy.

To be coherent all clone function should do the same thing full or just instance.

But sometimes, the full copy is not needed.

Edited by Alundra

Share this post


Link to post
Share on other sites

See the monster spawn example here.

 

Now think about it. If you kill one monster, is your expected behaviour that one monster dies, or all of them (and only dead monsters are spawned thereafter)?

 

Thus, all threading and memory management issues aside, I think it is rather safe to assume from a "user perspective" that a clone function should do a deep copy (there may of course be exceptions, but in general that's what you would/should expect).

Share this post


Link to post
Share on other sites

Yes it's part of a factory but for the property system the clone function is also used when undo command need to store a copy to redo the command.

This is where all start about the question, sometime only a simple instance is needed and sometimes a full copy.

To be coherent all clone function should do the same thing full or just instance.

But sometimes, the full copy is not needed.

 

If all you need is a new, empty/default instance, then use the constructor or factory. If you need a copy, then use Clone (or a copy constructor).

 

Calling a method that does nothing but create a default instance "Clone" is very misleading.

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!