xtd 0.2.0
Loading...
Searching...
No Matches

◆ memberwise_clone()

template<typename object_t >
xtd::uptr< object_t > xtd::object::memberwise_clone ( ) const
inline

Creates a shallow copy of the current object.

Returns
A shallow copy of the current object.
Examples
The following example illustrates the xtd::object::memberwise_clone method. It defines a shallow_copy method that calls the xtd::object::memberwise_clone method to perform a shallow copy operation on a person object. It also defines a deep_copy method that performs a deep copy operation on a person object.
#include <xtd/console>
using namespace xtd;
class id_info : public object {
public:
int id_number = 0;
explicit id_info(int id_number) : id_number {id_number} {}
};
class person : public object {
public:
int age = 0;
ptr<string> name = new_ptr<string>();
ptr<::id_info> id_info = new_ptr<::id_info>(0);
uptr<person> shallow_copy() {
return memberwise_clone<person>();
}
uptr<person> deep_copy() {
auto other = memberwise_clone<person>();
other->id_info = new_ptr<::id_info>(this->id_info->id_number);
other->name = new_ptr<string>(*name);
return other;
}
};
void display_values(const person p) {
console::write_line(" name: {0:s}, age: {1:d}", *p.name, p.age);
console::write_line(" value: {0:d}", p.id_info->id_number);
}
auto main() -> int {
// Create an instance of person and assign values to its fields.
auto p1 = new_uptr<person>();
p1->age = 42;
*p1->name = "Sam";
*p1->id_info = id_info {6565};
// Perform a shallow copy of p1 and assign it to p2.
auto p2 = p1->shallow_copy();
// Display values of p1, p2
console::write_line("Original values of p1 and p2:");
console::write_line(" p1 instance values: ");
display_values(*p1);
console::write_line(" p2 instance values:");
display_values(*p2);
// Change the value of p1 properties and display the values of p1 and p2.
p1->age = 32;
*p1->name = "Frank";
p1->id_info->id_number = 7878;
console::write_line("\nValues of p1 and p2 after changes to p1:");
console::write_line(" p1 instance values: ");
display_values(*p1);
console::write_line(" p2 instance values:");
display_values(*p2);
// Make a deep copy of p1 and assign it to p3.
auto p3 = p1->deep_copy();
// Change the members of the p1 class to new values to show the deep copy.
*p1->name = "George";
p1->age = 39;
p1->id_info->id_number = 8641;
console::write_line("\nValues of p1 and p3 after changes to p1:");
console::write_line(" p1 instance values: ");
display_values(*p1);
console::write_line(" p3 instance values:");
display_values(*p3);
}
// This code produces the following output :
//
// Original values of p1 and p2:
// p1 instance values:
// name: Sam, age: 42
// value: 6565
// p2 instance values:
// name: Sam, age: 42
// value: 6565
//
// Values of p1 and p2 after changes to p1:
// p1 instance values:
// name: Frank, age: 32
// value: 7878
// p2 instance values:
// name: Frank, age: 42
// value: 7878
//
// Values of p1 and p3 after changes to p1:
// p1 instance values:
// name: George, age: 39
// value: 8641
// p3 instance values:
// name: Frank, age: 32
// value: 7878
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.h:27
std::unique_ptr< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.h:25
@ other
The operating system is other.
@ p
The P key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
In this example, the person::id_info property returns an id_info object. As the output from the example shows, when a person object is cloned by calling the xtd::object::memberwise_clone method, the cloned person object is an independent copy of the original object, except that they share the same person::id_info object reference. As a result, modifying the clone's person::id_info property changes the original object's person::id_info property. On the other hand, when a deep copy operation is performed, the cloned person object, including its person::id_info property, can be modified without affecting the original object.
Remarks
The xtd::object::memberwise_clone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.
For example, consider an object called X that references objects A and B. Object B, in turn, references object C. A shallow copy of X creates new object X2 that also references objects A and B. In contrast, a deep copy of X creates a new object X2 that references the new objects A2 and B2, which are copies of A and B. B2, in turn, references the new object C2, which is a copy of C. The example illustrates the difference between a shallow and a deep copy operation.
There are numerous ways to implement a deep copy operation if the shallow copy operation performed by the xtd::object::memberwise_clone method does not meet your needs. These include the following:
  • Call a class constructor of the object to be copied to create a second object with property values taken from the first object. This assumes that the values of an object are entirely defined by its class constructor.
  • Call the xtd::object::memberwise_clone method to create a shallow copy of an object, and then assign new objects whose values are the same as the original object to any properties or fields whose values are reference types. The deep_copy method in the example illustrates this approach.
  • Serialize the object to be deep copied, and then restore the serialized data to a different object variable.
  • Use reflection with recursion to perform the deep copy operation.