By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,129 Members | 1,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,129 IT Pros & Developers. It's quick & easy.

Simple inheritance question.

P: n/a
I have written a C methods to support a medical imaging file format.
I wish to port this class to C++.

I think it should inherit from ifstream.

That is I want to define a class called d3p10_stream.

class d3p10_stream : public ifstream
{
private:
unsigned short myStreamState;
public:
d3p10_stream(void);
d3p10_stream & operator>>(d3p1o_stream & str, someObject & o);
d3p10_stream & operator>>(d3p1o_stream & str, someOtherObject & o);
~d3p10_stream(void);
};
Such that when methods like read, operator>>, close, open are
called my methods are called.

Do you think this is the correct way to implement a specialize
file format reader class?

Do you agree with the methods I've decided to over-ride?

Should other methods be overridden as well?
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
JustSomeGuy wrote:
I have written a C methods to support a medical imaging file format.
I wish to port this class to C++.

I think it should inherit from ifstream.
Why?
That is I want to define a class called d3p10_stream.

class d3p10_stream : public ifstream
{
private:
unsigned short myStreamState;
public:
d3p10_stream(void);
d3p10_stream & operator>>(d3p1o_stream & str, someObject & o);
d3p10_stream & operator>>(d3p1o_stream & str, someOtherObject & o);
The functions above are not the right definitions. If you want them to be
members, only one argument is needed. However, the idea is not to change
the behaviour of the stream (what you're trying to do) but instead create
proper functions for interpreting regular characters coming from a regular
file stream.
~d3p10_stream(void);
};
Such that when methods like read, operator>>, close, open are
called my methods are called.

Do you think this is the correct way to implement a specialize
file format reader class?
No.
Do you agree with the methods I've decided to over-ride?
Well... No.

Generally speaking you need a class that, given a stream (whether file or
any other kind), will take the stream characters and interpret them the
way you need. Such class would be an _adapter_, but not a stream itself.
Should other methods be overridden as well?


You should concentrate on interpreting the stream as it comes from
a normal file instead of trying to change the way it comes from a file.

V
Jul 23 '05 #2

P: n/a
Ian
JustSomeGuy wrote:
I have written a C methods to support a medical imaging file format.
I wish to port this class to C++.

I think it should inherit from ifstream.

Have a more detailed look at streambufs, if you are going to provide
your own specialised input, create your own streambuf and pass this to
ifstream.

Ian
Jul 23 '05 #3

P: n/a

"Ian" <no***@nowhere.com> wrote in message
news:11***************@drone2-svc-skyt.qsi.net.nz...
JustSomeGuy wrote:
I have written a C methods to support a medical imaging file format.
I wish to port this class to C++.

I think it should inherit from ifstream.

Have a more detailed look at streambufs, if you are going to provide
your own specialised input, create your own streambuf and pass this to
ifstream.

Ian


I guess that makes sence then the derived class would be a peer of ifstream
rather than a subclass of it.

Does anyone else but me think that a subclass is a bad analogy? Should it
be
a super-class? Why does this seem upside down to me?
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.