inheriting from std::ifstream, safe???
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]
They have virtual destructers
[edited by - QUANT on May 1, 2003 7:09:41 AM]
// TEMPLATE CLASS basic_ifstreamtemplate<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]
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...
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.
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]
I suppose that you''d usually overload the stream buffer, and not the stream itself. Depends what the problem is
quote:Original post by George2It''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.
Not with gcc, so I wouldn''t count on them having virtual destructors with all compilers.
http://www.cplusplus.com/ref/iostream/
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement