Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how make cast from ifstream to istream in MS Visual C++

Reply
Thread Tools

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

 
 
ragi
Guest
Posts: n/a
 
      09-07-2004
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 ?
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-07-2004

"ragi" <(E-Mail Removed)> wrote in message
news:chk57q$2tec$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Jonne Lehtinen
Guest
Posts: n/a
 
      09-07-2004
John Harrison wrote:
> "ragi" <(E-Mail Removed)> wrote in message
> news:chk57q$2tec$(E-Mail Removed)...
>
>>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
 
Reply With Quote
 
Chris \( Val \)
Guest
Posts: n/a
 
      09-07-2004

"Jonne Lehtinen" <(E-Mail Removed)> wrote in message
news:chkfts$1niv$(E-Mail Removed)...
| John Harrison wrote:
| > "ragi" <(E-Mail Removed)> wrote in message
| > news:chk57q$2tec$(E-Mail Removed)...
| >
| >>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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-07-2004

"Chris ( Val )" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Jonne Lehtinen" <(E-Mail Removed)> wrote in message
> news:chkfts$1niv$(E-Mail Removed)...
> | John Harrison wrote:
> | > "ragi" <(E-Mail Removed)> wrote in message
> | > news:chk57q$2tec$(E-Mail Removed)...
> | >
> | >>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


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      09-07-2004
Jonne Lehtinen <(E-Mail Removed)> wrote:
> John Harrison wrote:
> > "ragi" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Chris \( Val \)
Guest
Posts: n/a
 
      09-08-2004

"Mike Wahler" <(E-Mail Removed)> wrote in message
news:VHl%c.9666$w%(E-Mail Removed) ink.net...
|
| "Chris ( Val )" <(E-Mail Removed)> wrote in message
| news:(E-Mail Removed)...
| >
| > "Jonne Lehtinen" <(E-Mail Removed)> wrote in message
| > news:chkfts$1niv$(E-Mail Removed)...
| > | John Harrison wrote:
| > | > "ragi" <(E-Mail Removed)> wrote in message
| > | > news:chk57q$2tec$(E-Mail Removed)...

[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





 
Reply With Quote
 
Jonne Lehtinen
Guest
Posts: n/a
 
      09-09-2004
Old Wolf wrote:
> Jonne Lehtinen <(E-Mail Removed)> wrote:
>
>>John Harrison wrote:
>>
>>>"ragi" <(E-Mail Removed)> 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
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Homemade STL - how to make "specific" constructor and istream>> operator Bubba C++ 1 04-15-2011 11:21 PM
Does the C++ standard define the global function of " istream&operator >>(istream& in, string& str); "? xmllmx C++ 5 06-15-2010 11:57 AM
newbie declare ifstream, istream, whatever stream question... sandwich_eater@hotmail.com C++ 9 08-05-2005 01:53 AM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM



Advertisments