Consider the two classes below. To reduce redundancy in my code I want to combine the two.
class input_file: public ifstream {
string str;
public:
input_file();
~input_file();
input_file(string);
void _name(string);
void _open(string);
}; // input_file
class output_file: public ofstream {
string str;
public:
output_file();
~output_file();
output_file(string);
void _name(string);
void _open(string);
}; // output_file
void output_file::_open(string str) {
open(str);
if (!this->is_open() || !this->good()) {
error("Can't open output file: " + str + "!"); // This will exit
}
if(DEBUG){debug("output_file::_open \"(" + str + ")\"");}
}
I have red something about templates
in C++, and I belive the function below will work. I may have to use to_string
in the output functions.
template<typename T>
class io_file { // Different inheritance
string str;
public
...
};
void output_file::_open(string str) {
open(str);
if (!this->is_open() || !this->good()) {
error("Can't open " + T + " file: " + str + "!"); // This will exit
}
if(DEBUG){debug(T + "_file::_open \"(" + str + ")\"");}
}
The problem is about the class inheritances, how would I write the class definition with different inheritances?