Cloning (programming): Difference between revisions

Content deleted Content added
Clean up/copyedit
 
(One intermediate revision by one other user not shown)
Line 41:
 
copyObj = originalObj.clone(); // duplicates the object and assigns the new reference to 'copyObj'
</syntaxhighlight>
 
C++ objects in general behave like primitive types, so to copy a C++ object one could use the '<code>=</code>' (assignment) operator. There is a default assignment operator provided for all classes, but its effect may be altered through the use of [[operator overloading]]. There are dangers when using this technique (see [[Object slicing|slicing]]). A method of avoiding slicing can be implementing a similar solution to the Java <code>clone()</code> method for the classes and using pointers. (Note that thereThere is no built-in <code>clone()</code> method.)
 
A C++ example of object cloning: