0

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?

  • You do not inherit from standard library classes. http://stackoverflow.com/questions/4353203/thou-shalt-not-inherit-from-stdvector – The Vivandiere Oct 05 '15 at 19:52
  • Anyhow I do not understand why you would need two different classes for inputfile and output file. Depends of course what you want to do, but to me writing a class `MyFile` that privately uses an `ifstream` and `ofstream` would make more sense. – 463035818_is_not_an_ai Oct 05 '15 at 19:54
  • I'm new to the C++ concept. Why don't I inherit from standard classes? edit: missed the link. please ignore. – David Kristiansen Oct 05 '15 at 19:54
  • 2
    @FirstJens That is true most of the time, but there are some classes that it is safe to derive from such as [`std::basic_stream`](https://stackoverflow.com/questions/772355/how-to-inherit-from-stdostream) and [`std::exception`](https://stackoverflow.com/questions/1669514/should-i-inherit-from-stdexception) – Cory Kramer Oct 05 '15 at 19:55
  • @DavidKristiansen The problem is the lack of `virtual` methods, specifically many classes lack `virtual` destructors. https://stackoverflow.com/questions/1073958/extending-the-c-standard-library-by-inheritance – Cory Kramer Oct 05 '15 at 19:56
  • But that does not apply to streams, which are explicitly intended to be inherited. – Puppy Oct 05 '15 at 19:59
  • @CoryKramer: It's also not always a problem that the types are not polymorphic. – Lightness Races in Orbit Oct 05 '15 at 20:00
  • Ok, I'm confused now. The reason is because there is no destructors, but I can use it for my case? – David Kristiansen Oct 05 '15 at 20:00
  • 1
    Is using `fstream` and specifying `ios::in` or `ios::out` on the `io_file` constructor a viable option for this task? – user4581301 Oct 05 '15 at 20:31
  • @user4581301: That could be an option, yes. – David Kristiansen Oct 05 '15 at 20:35

1 Answers1

2

try this :

template<typename T>
class io_file : public T // where T can be either ifstream or ofstream 
{ // Different inheritance
    string str;
  public
    ...
};
M. Yousfi
  • 578
  • 5
  • 24