2

Hey guys, im having problems when i want to create a class Called Files that uses fstream

#include<iostream>
#include<fstream>
class Files:public fstream {
    public:
       Files(const char* s,ios_base::openmode mode = ios_base::in | ios_base::out):fstream(s,ios_base::openmode mode = ios_base::in | ios_base::out)
    };

Does anyone know which parameters should i use on the constructor?

Jam
  • 29
  • 2
  • it should be as simple as, `Files(const char* s,ios_base::openmode mode = ios_base::in | ios_base::out) : fstream(s,mode)` – iammilind May 27 '11 at 06:00
  • 3
    that's a perfect example of how *not* to use inheritance. You told it to us in the first sentence: "Files" uses an "fstream", and not "is an fstream". So better add a member variable of type fstream to Files and you don't need to solve your problem any more. – Doc Brown May 27 '11 at 06:01
  • To elaborate on @DocBrown, the code says "a Files is a fstream". Just the mix between singular and plural is already confusing and should be fixed. If you only intend to say "uses", inheritance is not the right tool. In any case, ask yourself which member functions of class `fstream` you would like to override in your class. If you don't have any, then inheritance is most probably wrong. – Ulrich Eckhardt Jan 13 '19 at 21:58

2 Answers2

4

Don't repeat the default parameters. Your code should look like this:

#include<iostream>
#include<fstream>

class Files : public std::fstream {
    public:
       Files(const char* s, 
             std::ios_base::openmode mode = ios_base::in | ios_base::out)
       :std::fstream(s, mode)
       {}
};

That being said you may want to consider using Boost.Iostreams if you want to define your own stream classes. Overriding all the correct methods from the standard streams is a pain.

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • @RobertAndrzejuk I take it back - I thought `fstream` would be an _injected-class-name_. Problem is, it's an alias. `basic_fstream` (not `std::basic_fstream`) does work. – Lightness Races in Orbit Jan 14 '19 at 00:21
  • @LightnessRacesinOrbit Hmm and here I though, it needs to be fully qualified everywhere. Interestingly: I had a different case when declaring a member function which returned an internal enum from that class. With traditional function declaration the enum had to be qualified with class name, BUT with trailing return type syntax, the enum did not have to be qualified with class name (VS 2017). – Robert Andrzejuk Jan 14 '19 at 00:28
  • @RobertAndrzejuk That'll happen when you _define_ the member function out-of-line. It's actually a feature of trailing-return-types and a reason to use them (in some cases!). See https://stackoverflow.com/a/11215414/560648, https://stackoverflow.com/a/11235245/560648 et al – Lightness Races in Orbit Jan 14 '19 at 00:36
-3

Don't inherit from classes that doesn't have virtual destructors

ognian
  • 11,451
  • 4
  • 35
  • 33
  • 1
    No problem if just non-virtual functions added to the class. – neuront May 27 '11 at 06:25
  • 7
    This isn't a good generalization and doesn't apply. `std::fstream` has a virtual destructor because in inherits from (e.g.) `std::ostream` which has a destructor declared virtual. (ISO/IEC 14882:2003 12.4 [class.dtor[ / 7 says "If a class has a base class with a virtual destructor, its destructor (whether user- or implicitly- declared) is virtual.") – CB Bailey May 27 '11 at 06:28