我创建了ofstream的子类。我想传递文件的构造函数模式。例如ios::app
。我该怎么做 ?我应该在my_file
构造函数中写什么来将它放在ofstream
类构造函数中?我知道它是int
类型但是如何理解ios::app
的价值是什么?
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
class my_file : public ofstream {
string name;
public:
my_file(string name, const char* filename) : ofstream(filename) { this->name = name; }
inline const string get() { return this->name; }
};
int main(void) {
my_file file("Name","new.txt" /* , ios::app */ );
return 0;
}
答案 0 :(得分:0)
我知道它是int类型但是如何理解ios :: app的价值是什么?
错了,那不是int
!
转到ofstream
doc http://www.cplusplus.com/reference/fstream/ofstream/,然后点击(constructor)查看参数是什么,然后您可以看到该模式的类型为std::ios_base::openmode
(如所述{{ 3}})
如此简单:
class my_file : public ofstream {
string name;
public:
my_file(string name, const char* filename, std::ios_base::openmode mode ) : ofstream(filename,mode) { this->name = name; }
inline const string get() { return this->name; }
};
然后:
my_file file("Name","new.txt", ios::app);