how make cast from ifstream to istream in MS Visual C++

Discussion in 'C++' started by ragi, Sep 7, 2004.

  1. ragi

    ragi Guest

    Short version of my program:

    ifstream File;

    File.open("test.txt");
    if(!File.good()) return;

    Func(File); <--cannot convert parametr 1 from 'std::ifstream' to
    'std::istream'


    and declaration is:

    bool Func(std::istream);


    What should I do to solve these problem ?
     
    ragi, Sep 7, 2004
    #1
    1. Advertising

  2. "ragi" <> wrote in message
    news:chk57q$2tec$...
    > Short version of my program:
    >
    > ifstream File;
    >
    > File.open("test.txt");
    > if(!File.good()) return;
    >
    > Func(File); <--cannot convert parametr 1 from 'std::ifstream' to
    > 'std::istream'
    >
    >
    > and declaration is:
    >
    > bool Func(std::istream);
    >
    >
    > What should I do to solve these problem ?


    bool Func(std::istream&);

    Always pass streams by reference (or pointer) they cannot be copied.

    john
     
    John Harrison, Sep 7, 2004
    #2
    1. Advertising

  3. John Harrison wrote:
    > "ragi" <> wrote in message
    > news:chk57q$2tec$...
    >
    >>Short version of my program:
    >>
    >>ifstream File;
    >>
    >>File.open("test.txt");
    >>if(!File.good()) return;
    >>
    >>Func(File); <--cannot convert parametr 1 from 'std::ifstream' to
    >>'std::istream'
    >>
    >>
    >>and declaration is:
    >>
    >>bool Func(std::istream);
    >>
    >>
    >>What should I do to solve these problem ?

    >
    >
    > bool Func(std::istream&);
    >
    > Always pass streams by reference (or pointer) they cannot be copied.
    >
    > john
    >
    >


    If a function is guaranteed to not to modify the passed variables then
    they should be passed as const reference. Personally I rarely pass
    anything by value since it's usually faster to pass by (const)
    reference - especially with classes. I only use by value if the original
    variable shouldn't be changed and the function would change it if it's
    passed by reference (or pointer, though personally I try to avoid them
    at all cost :).

    - Jonne Lehtinen
     
    Jonne Lehtinen, Sep 7, 2004
    #3
  4. "Jonne Lehtinen" <> wrote in message
    news:chkfts$1niv$...
    | John Harrison wrote:
    | > "ragi" <> wrote in message
    | > news:chk57q$2tec$...
    | >
    | >>Short version of my program:
    | >>
    | >>ifstream File;
    | >>
    | >>File.open("test.txt");
    | >>if(!File.good()) return;
    | >>
    | >>Func(File); <--cannot convert parametr 1 from 'std::ifstream' to
    | >>'std::istream'
    | >>
    | >>
    | >>and declaration is:
    | >>
    | >>bool Func(std::istream);
    | >>
    | >>
    | >>What should I do to solve these problem ?
    | >
    | >
    | > bool Func(std::istream&);
    | >
    | > Always pass streams by reference (or pointer) they cannot be copied.
    | >
    | > john
    | >
    | >
    |
    | If a function is guaranteed to not to modify the passed variables then
    | they should be passed as const reference. Personally I rarely pass
    | anything by value since it's usually faster to pass by (const)
    | reference - especially with classes. I only use by value if the original
    | variable shouldn't be changed and the function would change it if it's
    | passed by reference (or pointer, though personally I try to avoid them
    | at all cost :).

    In general, you are right, but in this instance
    you are wrong.

    You cannot declare the istream as an 'const' parameter.

    Cheers.
    Chris Val
     
    Chris \( Val \), Sep 7, 2004
    #4
  5. ragi

    Mike Wahler Guest

    "Chris ( Val )" <> wrote in message
    news:...
    >
    > "Jonne Lehtinen" <> wrote in message
    > news:chkfts$1niv$...
    > | John Harrison wrote:
    > | > "ragi" <> wrote in message
    > | > news:chk57q$2tec$...
    > | >
    > | >>Short version of my program:
    > | >>
    > | >>ifstream File;
    > | >>
    > | >>File.open("test.txt");
    > | >>if(!File.good()) return;
    > | >>
    > | >>Func(File); <--cannot convert parametr 1 from 'std::ifstream' to
    > | >>'std::istream'
    > | >>
    > | >>
    > | >>and declaration is:
    > | >>
    > | >>bool Func(std::istream);
    > | >>
    > | >>
    > | >>What should I do to solve these problem ?
    > | >
    > | >
    > | > bool Func(std::istream&);
    > | >
    > | > Always pass streams by reference (or pointer) they cannot be copied.
    > | >
    > | > john
    > | >
    > | >
    > |
    > | If a function is guaranteed to not to modify the passed variables then
    > | they should be passed as const reference. Personally I rarely pass
    > | anything by value since it's usually faster to pass by (const)
    > | reference - especially with classes. I only use by value if the original
    > | variable shouldn't be changed and the function would change it if it's
    > | passed by reference (or pointer, though personally I try to avoid them
    > | at all cost :).
    >
    > In general, you are right, but in this instance
    > you are wrong.
    >
    > You cannot declare the istream as an 'const' parameter.


    Well, you can, but that makes the stream of limited utility
    (i.e. "no fishing in that stream!" :) ). However one can
    still call its const member functions (e.g. 'good()', 'eof()',
    etc.)

    -Mike
     
    Mike Wahler, Sep 7, 2004
    #5
  6. ragi

    Old Wolf Guest

    Jonne Lehtinen <> wrote:
    > John Harrison wrote:
    > > "ragi" <> wrote:
    > > >bool Func(std::istream);

    > >
    > > bool Func(std::istream&);
    > >
    > > Always pass streams by reference (or pointer) they cannot be copied.

    >
    > If a function is guaranteed to not to modify the passed variables then
    > they should be passed as const reference. Personally I rarely pass
    > anything by value since it's usually faster to pass by (const)
    > reference - especially with classes. I only use by value if the original
    > variable shouldn't be changed and the function would change it if it's
    > passed by reference (or pointer, though personally I try to avoid them
    > at all cost :).


    It is difficult to use a stream by const reference -- the operations
    of reading and writing both require the stream to be modified.
    It would be rare to pass a stream to a function where the function
    did not modify the stream.
     
    Old Wolf, Sep 7, 2004
    #6
  7. "Mike Wahler" <> wrote in message
    news:VHl%c.9666$w%...
    |
    | "Chris ( Val )" <> wrote in message
    | news:...
    | >
    | > "Jonne Lehtinen" <> wrote in message
    | > news:chkfts$1niv$...
    | > | John Harrison wrote:
    | > | > "ragi" <> wrote in message
    | > | > news:chk57q$2tec$...

    [snip]

    Hello Mike, hope you're well :)

    | > | > bool Func(std::istream&);
    | > | >
    | > | > Always pass streams by reference (or pointer) they cannot be copied.
    | > | >
    | > | > john
    | > | >
    | > | >
    | > |
    | > | If a function is guaranteed to not to modify the passed variables then
    | > | they should be passed as const reference. Personally I rarely pass
    | > | anything by value since it's usually faster to pass by (const)
    | > | reference - especially with classes. I only use by value if the original
    | > | variable shouldn't be changed and the function would change it if it's
    | > | passed by reference (or pointer, though personally I try to avoid them
    | > | at all cost :).
    | >
    | > In general, you are right, but in this instance
    | > you are wrong.
    | >
    | > You cannot declare the istream as an 'const' parameter.
    |
    | Well, you can, but that makes the stream of limited utility
    | (i.e. "no fishing in that stream!" :) ). However one can
    | still call its const member functions (e.g. 'good()', 'eof()',
    | etc.)

    You're right, though I should have qualified exactly
    what I meant.

    I meant that even reading of the stream required
    an mutable operation to take effect, therefore
    declaring the stream as 'const' was not the right
    thing to recommend, in this instance.

    Cheers.
    Chris Val
     
    Chris \( Val \), Sep 8, 2004
    #7
  8. Old Wolf wrote:
    > Jonne Lehtinen <> wrote:
    >
    >>John Harrison wrote:
    >>
    >>>"ragi" <> wrote:
    >>>
    >>>>bool Func(std::istream);
    >>>
    >>>bool Func(std::istream&);
    >>>
    >>>Always pass streams by reference (or pointer) they cannot be copied.

    >>
    >>If a function is guaranteed to not to modify the passed variables then
    >>they should be passed as const reference. Personally I rarely pass
    >>anything by value since it's usually faster to pass by (const)
    >>reference - especially with classes. I only use by value if the original
    >>variable shouldn't be changed and the function would change it if it's
    >>passed by reference (or pointer, though personally I try to avoid them
    >>at all cost :).

    >
    >
    > It is difficult to use a stream by const reference -- the operations
    > of reading and writing both require the stream to be modified.
    > It would be rare to pass a stream to a function where the function
    > did not modify the stream.


    Okay, true :)
    Maybe I was generalizing a bit too much...

    - Jonne Lehtinen
     
    Jonne Lehtinen, Sep 9, 2004
    #8
    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. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,080
    Dan Pop
    Feb 10, 2004
  2. EvilRix
    Replies:
    8
    Views:
    653
    Martin Dickopp
    Feb 14, 2004
  3. Replies:
    9
    Views:
    2,536
  4. xmllmx
    Replies:
    5
    Views:
    592
    Jorgen Grahn
    Jun 15, 2010
  5. Bubba
    Replies:
    1
    Views:
    273
    Jorgen Grahn
    Apr 16, 2011
Loading...

Share This Page