在 C++ 11 之前, 假设有这么一个 FileHandler
类, 实现如下
#include <iostream>
#include <cstdio> // for FILE*
#include <vector>
class FileHandler {
private:
FILE* file;
FileHandler(const FileHandler&);
FileHandler& operator=(const FileHandler&);
public:
FileHandler(const char* filename, const char* mode) : file(std::fopen(filename, mode)) {
if (!file) {
std::perror("Failed to open file");
throw std::runtime_error("File open error");
}
}
~FileHandler() {
if (file) {
std::fclose(file);
}
}
void write(const char* data) {
if (file) {
std::fputs(data, file);
}
}
void flush() {
if (file) {
std::fflush(file);
}
}
};
int main() {
try {
FileHandler fh("test.txt", "w");
fh.write("Hello, World!\n");
fh.flush();
// std::vector<FileHandler> fileHandlers;
// fileHandlers.push_back(fh); // 此处会编译出错
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
拷贝构造与赋值问题:
FileHandler 类封装了文件句柄 FILE*,但是未定义拷贝构造函数和拷贝赋值运算符。
如果尝试将对象存储在 STL 容器中(如 std::vector),会导致编译错误,因为 STL 容器需要对象支持拷贝或移动操作。
拷贝 FILE* 是不安全的,因为多个对象可能共享同一个文件句柄,导致资源释放冲突。
资源管理问题:
文件句柄是系统资源,必须确保在对象析构时正确释放。
在异常情况下(如文件打开失败),需要通过 try-catch 捕获错误,防止资源泄露。
C++11 优化:引入移动语义
通过实现移动构造函数
和移动赋值运算符
,可以解决上述问题,使 FileHandler
支持资源转移,兼容 STL
容器,并提高效率。
#include <iostream>
#include <cstdio> // for FILE*
#include <vector>
#include <stdexcept>
#include <utility> // for std::move
class FileHandler {
private:
FILE* file;
FileHandler(const FileHandler&);
FileHandler& operator=(const FileHandler&);
public:
FileHandler(const char* filename, const char* mode) : file(std::fopen(filename, mode)) {
if (!file) {
std::perror("Failed to open file");
throw std::runtime_error("File open error");
}
}
~FileHandler() {
if (file) {
std::fclose(file);
}
}
// 移动构造函数
FileHandler(FileHandler&& other) noexcept : file(other.file) {
other.file = nullptr; // 将其他对象的文件句柄置空
}
// 移动赋值运算符
FileHandler& operator=(FileHandler&& other) noexcept {
if (this != &other) {
if (file) {
std::fclose(file); // 释放当前对象的资源
}
file = other.file;
other.file = nullptr; // 将其他对象的文件句柄置空
}
return *this;
}
void write(const char* data) {
if (file) {
std::fputs(data, file);
}
}
void flush() {
if (file) {
std::fflush(file);
}
}
};
int main() {
try {
FileHandler fh("test.txt", "w");
fh.write("Hello, World!\n");
fh.flush();
std::vector<FileHandler> fileHandlers;
fileHandlers.push_back(std::move(fh));
}
catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
改进点分析
移动构造函数:
- 接收一个右值引用 FileHandler&& other。
- 将 other.file 的资源转移到当前对象。
- 将 other.file 设置为 nullptr,确保资源的唯一性。
移动赋值运算符:
- 检查自赋值后,释放当前对象持有的资源。
- 转移 other.file 的资源到当前对象,并将 other.file 置空。
容器兼容性:
- 实现移动语义后,可以使用 std::vector 等容器存储 FileHandler 对象,动态管理文件句柄。
效率提升:
- 使用 std::move 转移资源避免不必要的拷贝。
- 移动语义的引入减少了资源分配与释放的开销。