8.7.1 C++ 实现
01-C++ 实现 — Comprehensive Rust
1分钟内可阅读完
译文 · 基于 Comprehensive Rust
8.7.1 C++ 实现
#include <cstddef>
#include <cstring>
class SelfReferentialBuffer {
std::byte data[1024];
std::byte* cursor = data;
public:
SelfReferentialBuffer(SelfReferentialBuffer&& other)
: cursor{data + (other.cursor - other.data)}
{
std::memcpy(data, other.data, 1024);
}
};
可在 Compiler Explorer 上查看
SelfReferentialBuffer包含两个成员:data是一 KB 内存,cursor是指向data的指针。其移动构造函数确保 cursor 更新到新的内存地址。
这种类型在 Rust 中不易表达。