Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple inheritance question.

Reply
Thread Tools

Simple inheritance question.

 
 
JustSomeGuy
Guest
Posts: n/a
 
      07-13-2005
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?


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-13-2005
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
 
Reply With Quote
 
 
 
 
Ian
Guest
Posts: n/a
 
      07-13-2005
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
 
Reply With Quote
 
JustSomeGuy
Guest
Posts: n/a
 
      07-17-2005

"Ian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments