inheriting from std::ifstream, safe???

Started by
5 comments, last by Seriema 20 years, 11 months ago
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]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
Advertisement
They have virtual destructers


  		// 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]
-keyboard
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.
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
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]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
I suppose that you''d usually overload the stream buffer, and not the stream itself. Depends what the problem is
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/

This topic is closed to new replies.

Advertisement