Skip to content

Commit 74964c0

Browse files
gabut
1 parent 86b67b1 commit 74964c0

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ add_subdirectory(args_handle)
22
add_subdirectory(assertion)
33
add_subdirectory(operator_overloading)
44
add_subdirectory(segfault_handler)
5+
add_subdirectory(move)

basic/move/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include_directories(${CMAKE_CURRENT_SOURCE_DIR} include)
2+
3+
set(SRC "src/")
4+
add_executable(move ${SRC}move.cxx)

basic/move/include/move.hxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
template <typename T>
2+
T move(T&& val) {
3+
return static_cast<T&&>(val);
4+
}

basic/move/src/move.cxx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)