xtd 1.0.0
Loading...
Searching...
No Matches
xtd::iclonable Class Referenceabstract
Inheritance diagram for xtd::iclonable:
xtd::interface xtd::collections::bit_array

Definition

Supports cloning, which creates a new instance of a class with the same value as an existing instance.

Namespace
xtd
Library
xtd.core
Remarks
The xtd::iclonable interface enables you to provide a customized implementation that creates a copy of an existing object. The xtd::iclonable interface contains one member, the xtd::iclonable::clone method, which is intended to provide cloning support beyond that supplied by xtd::object::memberwise_clone. For more information about cloning, deep versus shallow copies, and examples, see the xtd::object::memberwise_clone method.
Notes to Implementers
The xtd::iclonable interface simply requires that your implementation of the xtd::iclonable::clone() method return a copy of the current object instance. It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between. Nor does it require all property values of the original instance to be copied to the new instance. For example, the xtd::iclonable::clone() method performs a shallow copy of all properties; it always sets this property value to false in the cloned object. Because callers of xtd::iclonable::clone() cannot depend on the method performing a predictable cloning operation, we recommend that xtd::iclonable not be implemented in public APIs. The following example shows how to use xtd::iclonable interface.
#include <xtd/xtd>
class foo : public object, public iclonable {
public:
explicit foo(int value) : value_ {value} {}
uptr<object> clone() const override {return new_uptr<foo>(value_);}
string to_string() const noexcept override {return string::format("{}", value_);}
private:
int value_ = 0;
};
auto main() -> int {
auto f1 = foo {42};
console::write_line("f1 {{type = {}, value = {}}}", typeof_(f1), f1);
auto f2 = f1.clone();
console::write_line("f2 {{type = {}, value = {}}}", typeof_(f2), *f2);
auto f3 = as<foo>(f1.clone());
console::write_line("f3 {{type = {}, value = {}}}", typeof_(f3), *f3);
}
// This code produces the following output :
//
// f1 {type = foo, value = 42}
// f2 {type = std::unique_ptr<xtd::object, std::default_delete<xtd::object>>, value = 42}
// f3 {type = std::unique_ptr<foo, std::default_delete<foo>>, value = 42}
Supports cloning, which creates a new instance of a class with the same value as an existing instance...
Definition iclonable.hpp:21
virtual auto clone() const -> xtd::uptr< xtd::object >=0
Creates a new object that is a copy of the current instance.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
virtual auto to_string() const -> xtd::string
Returns a xtd::string that represents the current object.
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
#define typeof_
Used to obtain the type object of a specified type or object.
Definition typeof.hpp:24
xtd::unique_ptr_object< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.hpp:25
auto new_uptr(args_t &&... args) -> xtd::uptr< type_t >
xtd::new_uptr operator. This operator creates a xtd::uptr object.
Definition new_uptr.hpp:24
@ f3
The F3 key.
Definition console_key.hpp:184
@ f2
The F2 key.
Definition console_key.hpp:182
@ f1
The F1 key.
Definition console_key.hpp:180

Public Methods

virtual auto clone () const -> xtd::uptr< xtd::object >=0
 Creates a new object that is a copy of the current instance.

Member Function Documentation

◆ clone()

virtual auto xtd::iclonable::clone ( ) const -> xtd::uptr< xtd::object >
nodiscardpure virtual

Creates a new object that is a copy of the current instance.

Returns
A new object that is a copy of this instance.
Remarks
The resulting clone must be of the same type as, or compatible with, the original instance.
An implementation of xtd::iclonable::clone can perform either a deep copy or a shallow copy. In a deep copy, all objects are duplicated; in a shallow copy, only the top-level objects are duplicated and the lower levels contain references. Because callers of xtd::iclonable::clone cannot depend on the method performing a predictable cloning operation, we recommend that xtd::iclonable not be implemented in public APIs.
See xtd::object::memberwise_clone for more information on cloning, deep versus shallow copies, and examples.

Implemented in xtd::collections::bit_array.


The documentation for this class was generated from the following file: