Simple inheritance question.

Discussion in 'C++' started by JustSomeGuy, Jul 13, 2005.

  1. JustSomeGuy

    JustSomeGuy Guest

    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?
     
    JustSomeGuy, Jul 13, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jul 13, 2005
    #2
    1. Advertising

  3. JustSomeGuy

    Ian Guest

    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
     
    Ian, Jul 13, 2005
    #3
  4. JustSomeGuy

    JustSomeGuy Guest

    "Ian" <> wrote in message
    news:...
    > 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?
     
    JustSomeGuy, Jul 17, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,144
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    827
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,041
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,904
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    777
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page