File tree Expand file tree Collapse file tree 4 files changed +39
-0
lines changed Expand file tree Collapse file tree 4 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -2,3 +2,4 @@ add_subdirectory(args_handle)
22add_subdirectory (assertion)
33add_subdirectory (operator_overloading)
44add_subdirectory (segfault_handler)
5+ add_subdirectory (move)
Original file line number Diff line number Diff line change 1+ include_directories (${CMAKE_CURRENT_SOURCE_DIR} include )
2+
3+ set (SRC "src/" )
4+ add_executable (move ${SRC} move.cxx)
Original file line number Diff line number Diff line change 1+ template <typename T>
2+ T move (T&& val) {
3+ return static_cast <T&&>(val);
4+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < utility> // untuk std::move
3+
4+ class Abstract {
5+ int a;
6+
7+ public:
8+ Abstract (int a) : a(a) {}
9+
10+ // copy constructor
11+ Abstract (const Abstract &a) noexcept : a(a.a) {}
12+
13+ // move constructor
14+ Abstract (Abstract &&a) noexcept : a(a.a) { a.a = 0 ; }
15+
16+ int getA () const { return a; }
17+ };
18+
19+ std::ostream &operator <<(std::ostream &os, const Abstract &abs) {
20+ return os << abs.getA ();
21+ }
22+
23+ int main () {
24+ std::cout << " isi a dengan 8" << std::endl;
25+ Abstract a (8 );
26+ std::cout << " move a ke b" << std::endl;
27+ Abstract b = std::move (a);
28+ std::cout << " a sekarang berisi " << a << " b sekarang berisi " << b
29+ << std::endl;
30+ }
You can’t perform that action at this time.
0 commit comments