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. Advertisements

  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. Advertisements

  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. Advertisements

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,365
    Martijn van Steenbergen
    Dec 21, 2003
  2. knocte

    Simple inheritance question

    knocte, Jul 17, 2004, in forum: Java
    Replies:
    3
    Views:
    503
    Adam Maass
    Jul 19, 2004
  3. knocte

    Simple inheritance question

    knocte, Jul 17, 2004, in forum: Java
    Replies:
    0
    Views:
    395
    knocte
    Jul 17, 2004
  4. Michael
    Replies:
    6
    Views:
    387
    Michael
    Mar 10, 2007
  5. Thomas
    Replies:
    6
    Views:
    813
    Patricia Shanahan
    Jul 12, 2007
  6. Numeron

    simple inheritance question

    Numeron, Nov 27, 2007, in forum: C++
    Replies:
    6
    Views:
    413
    Tadeusz B. Kopec
    Nov 28, 2007
  7. crea
    Replies:
    11
    Views:
    499
    MikeWhy
    Apr 5, 2011
  8. crea
    Replies:
    17
    Views:
    491
Loading...