在 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.
标签:11,std,const,右值,FileHandler,C++,char,file,include
From: https://blog.csdn.net/magicworkshop/article/details/144463664