Simple inheritance question.

J

JustSomeGuy

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?
 
V

Victor Bazarov

JustSomeGuy said:
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
 
I

Ian

JustSomeGuy said:
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
 
J

JustSomeGuy

Ian said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top