forked from parallel101/course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
103 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
cmake_minimum_required(VERSION 3.12) | ||
set(CMAKE_CXX_STANDARD 17) | ||
project(hellocpp LANGUAGES CXX) | ||
|
||
add_executable(cpptest main.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <cstdlib> | ||
#include <iostream> | ||
#include <cstring> | ||
|
||
struct Vector { | ||
size_t m_size; | ||
int *m_data; | ||
|
||
Vector(size_t n) { | ||
m_size = n; | ||
m_data = (int *)malloc(n * sizeof(int)); | ||
} | ||
|
||
~Vector() { | ||
free(m_data); | ||
} | ||
|
||
Vector(Vector const &other) { | ||
m_size = other.m_size; | ||
m_data = (int *)malloc(m_size * sizeof(int)); | ||
memcpy(m_data, other.m_data, m_size * sizeof(int)); | ||
} | ||
|
||
Vector &operator=(Vector const &other) { | ||
m_size = other.m_size; | ||
m_data = (int *)realloc(m_data, m_size * sizeof(int)); | ||
memcpy(m_data, other.m_data, m_size * sizeof(int)); | ||
return *this; | ||
} | ||
|
||
Vector(Vector &&other) { | ||
m_size = other.m_size; | ||
other.m_size = 0; | ||
m_data = other.m_data; | ||
other.m_data = nullptr; | ||
} | ||
|
||
Vector &operator=(Vector &&other) { | ||
this->~Vector(); | ||
new (this) Vector(std::move(other)); | ||
return *this; | ||
} | ||
|
||
size_t size() { | ||
return m_size; | ||
} | ||
|
||
void resize(size_t size) { | ||
m_size = size; | ||
m_data = (int *)realloc(m_data, m_size); | ||
} | ||
|
||
int &operator[](size_t index) { | ||
return m_data[index]; | ||
} | ||
}; | ||
|
||
int main() { | ||
Vector v1(32); | ||
Vector v2(64); | ||
|
||
v2 = std::move(v1); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
set -e | ||
|
||
cmake -B build | ||
cmake --build build | ||
build/cpptest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
C c1 = c2; // 拷贝构造函数 | ||
C c1 = std::move(c2); // 移动构造函数 | ||
|
||
c1 = c2; // 拷贝赋值函数 | ||
c1 = std::move(c2); // 移动赋值函数 | ||
|
||
C c1 = C(); // 移动构造函数 | ||
c1 = C(); // 移动赋值函数 | ||
return c2; // 移动赋值函数 |
Binary file not shown.