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

R

ragi

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 ?
 
J

John Harrison

ragi said:
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
 
J

Jonne Lehtinen

John said:
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
 
C

Chris \( Val \)

| John Harrison wrote:
| > | >
| >>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
 
M

Mike Wahler

Chris ( Val ) said:
| John Harrison wrote:
| > | >
| >>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
 
O

Old Wolf

Jonne Lehtinen said:
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.
 
C

Chris \( Val \)

|
| | >
| > | > | John Harrison wrote:
| > | >
[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
 
J

Jonne Lehtinen

Old said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top