xtd 0.2.0
Loading...
Searching...
No Matches
iclonable.cpp

Shows how to use xtd::iequatable 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}
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