Init
This commit is contained in:
51
ACMOJ-2095.cpp
Normal file
51
ACMOJ-2095.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include "ACMOJ-2095.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
struct koishi {
|
||||
int id;
|
||||
koishi() = default;
|
||||
koishi(int id) : id(id) {}
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const koishi &k) {
|
||||
return os << "Komeiji Koishi " << k.id;
|
||||
}
|
||||
|
||||
void test0() {
|
||||
using sjtu::unique_ptr;
|
||||
unique_ptr<int> ptr1(new int(114514));
|
||||
|
||||
// 移交所有权
|
||||
unique_ptr<int> ptr2(std::move(ptr1));
|
||||
|
||||
// 释放所有权
|
||||
int *tmp = ptr2.release();
|
||||
delete tmp;
|
||||
}
|
||||
|
||||
void test1() {
|
||||
using sjtu::unique_ptr;
|
||||
unique_ptr<koishi> ptr1 = sjtu::make_unique<koishi>(koishi(1));
|
||||
|
||||
// 这个行为可能是未定义的,但是我们要求其应该可以正常工作
|
||||
ptr1 = std::move(ptr1);
|
||||
|
||||
// 成员访问
|
||||
ptr1->id = 514;
|
||||
|
||||
// 解引用
|
||||
std::cout << *ptr1 << std::endl;
|
||||
|
||||
// 重置,现在 ptr1 应该为空
|
||||
ptr1.reset();
|
||||
if (!ptr1.get()) {
|
||||
std::cout << "ptr1 is empty!" << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
signed main() {
|
||||
test0();
|
||||
test1();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user