Archived

This topic is now archived and is closed to further replies.

Seriema

inheriting from std::ifstream, safe???

Recommended Posts

Seriema    634
hmmm saw a class for writing files class blockwriter : public std::ifstream { ... }; I thought you never should inherit from the STD because they don''t have virtual destructors (or virtual functions you can overload)? "No lies of sugar can sweeten the sournes of reality" }+TITANIUM+{ A.K.A. DXnewbie[onMIRC]

Share this post


Link to post
Share on other sites
quant    100
They have virtual destructers


  
// TEMPLATE CLASS basic_ifstream

template<class _E, class _Tr = char_traits<_E> >
class basic_ifstream : public basic_istream<_E, _Tr> {
public:
typedef basic_ifstream<_E, _Tr> _Myt;
typedef basic_filebuf<_E, _Tr> _Myfb;
basic_ifstream()
: basic_istream<_E, _Tr>(&_Fb) {}
explicit basic_ifstream(const char *_S,
ios_base::openmode _M = in)
: basic_istream<_E, _Tr>(&_Fb)
{if (_Fb.open(_S, _M | in) == 0)
setstate(failbit); }
virtual ~basic_ifstream()
{}
_Myfb *rdbuf() const
{return ((_Myfb *)&_Fb); }
bool is_open() const
{return (_Fb.is_open()); }
void open(const char *_S, ios_base::openmode _M = in)
{if (_Fb.open(_S, _M | in) == 0)
setstate(failbit); }
void open(const char *_S, ios_base::open_mode _M)
{open(_S, (openmode)_M); }
void close()
{if (_Fb.close() == 0)
setstate(failbit); }
private:
_Myfb _Fb;
};




[edited by - QUANT on May 1, 2003 7:09:41 AM]

Share this post


Link to post
Share on other sites
SabreMan    504
quote:
Original post by Seriema
I thought you never should inherit from the STD because they don''t have virtual destructors (or virtual functions you can overload)?

It depends. Some do and some don''t. However, inheriting from a stream is often the wrong answer to a common problem, but since you weren''t asking about that...

Share this post


Link to post
Share on other sites
George2    187
quote:
Original post by quant
They have virtual destructers
[edited by - QUANT on May 1, 2003 7:09:41 AM]


Not with gcc, so I wouldn''t count on them having virtual destructors with all compilers.

Share this post


Link to post
Share on other sites
Seriema    634
quote:
Original post by SabreMan
It depends. Some do and some don''t. However, inheriting from a stream is often the wrong answer to a common problem, but since you weren''t asking about that...



well then I''ll ask about that How come it''s the wrong answer to a common problem?

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by George2
Not with gcc, so I wouldn''t count on them having virtual destructors with all compilers.
It''s sufficient that the root of the hierarchy has virtual destructors (just like with other virtual functions; the overriding function doesn''t need to explicitely state ''virtual''). C++ streams are based on an inheritance tree so they''re guaranteed to have virtual destructors. Well, one could always have a very flawy implementation, but I doubt gcc is. So check the root classes before claming otherwise.

http://www.cplusplus.com/ref/iostream/

Share this post


Link to post
Share on other sites