我想传入文件的构造函数模式

I want to pass in constructor mode of file

我创建了 child class ofstream。我想传入文件的构造函数模式。例如 ios::app。我该怎么做 ?我应该在 my_file 构造函数中写些什么才能将其放入 ofstream class 构造函数中?我知道它是 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;
}

我知道是int类型,但是怎么理解ios::app的值呢?

错了,那不是 int!

转到 ofstream 文档 http://www.cplusplus.com/reference/fstream/ofstream/, then click (constructor) to see what the parameters are and then you can see that mode is of type std::ios_base::openmode (as described here)

所以简单地做:

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);