xtd 0.2.0
Loading...
Searching...
No Matches
xtd::iclonable Class Referenceabstract
Inheritance diagram for xtd::iclonable:
xtd::interface xtd::forms::context_menu xtd::forms::control xtd::forms::main_menu xtd::forms::menu_item xtd::forms::status_bar_panel xtd::forms::tool_bar_button xtd::forms::animation xtd::forms::button_base xtd::forms::collapsible_panel xtd::forms::color_picker xtd::forms::date_time_picker xtd::forms::dot_matrix_display xtd::forms::font_picker xtd::forms::group_box xtd::forms::label xtd::forms::lcd_label xtd::forms::list_control xtd::forms::loading_indicator xtd::forms::month_calendar xtd::forms::picture_box xtd::forms::progress_bar xtd::forms::scroll_bar xtd::forms::scrollable_control xtd::forms::seven_segment_display xtd::forms::splitter xtd::forms::status_bar xtd::forms::tab_control xtd::forms::text_box_base xtd::forms::tool_bar xtd::forms::track_bar

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/console>
#include <xtd/iclonable>
using namespace 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}
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Supports cloning, which creates a new instance of a class with the same value as an existing instance...
Definition iclonable.h:21
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
#define typeof_
Used to obtain the type object of a specified type or object.
Definition typeof.h:45
std::unique_ptr< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.h:25
@ f3
The F3 key.
@ f2
The F2 key.
@ f1
The F1 key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Examples
iclonable.cpp.

Public Methods

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

Member Function Documentation

◆ clone()

virtual xtd::uptr< xtd::object > xtd::iclonable::clone ( ) const
pure 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::forms::animation, xtd::forms::button, xtd::forms::check_box, xtd::forms::checked_list_box, xtd::forms::choice, xtd::forms::collapsible_panel, xtd::forms::color_picker, xtd::forms::combo_box, xtd::forms::command_link_button, xtd::forms::context_menu, xtd::forms::control, xtd::forms::date_time_picker, xtd::forms::debug_form, xtd::forms::domain_up_down, xtd::forms::dot_matrix_display, xtd::forms::fixed_layout_panel, xtd::forms::flow_layout_panel, xtd::forms::font_picker, xtd::forms::form, xtd::forms::fourteen_segment_display, xtd::forms::group_box, xtd::forms::h_scroll_bar, xtd::forms::horizontal_layout_panel, xtd::forms::label, xtd::forms::lcd_label, xtd::forms::light_button, xtd::forms::link_label, xtd::forms::list_box, xtd::forms::loading_indicator, xtd::forms::main_menu, xtd::forms::menu_item, xtd::forms::month_calendar, xtd::forms::nine_segment_display, xtd::forms::numeric_up_down, xtd::forms::panel, xtd::forms::picture_box, xtd::forms::popup_panel, xtd::forms::progress_bar, xtd::forms::radio_button, xtd::forms::seven_segment_display, xtd::forms::sixteen_segment_display, xtd::forms::split_container, xtd::forms::splitter, xtd::forms::splitter_panel, xtd::forms::status_bar, xtd::forms::status_bar_panel, xtd::forms::switch_button, xtd::forms::tab_control, xtd::forms::tab_page, xtd::forms::table_layout_panel, xtd::forms::text_box, xtd::forms::toggle_button, xtd::forms::tool_bar, xtd::forms::tool_bar_button, xtd::forms::trace_form, xtd::forms::track_bar, xtd::forms::up_down_button, xtd::forms::user_control, xtd::forms::v_scroll_bar, and xtd::forms::vertical_layout_panel.


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