By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 ?
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"ragi" <ra*******@interia.pl> wrote in message
news:ch***********@mamut.aster.pl...
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
Jul 22 '05 #2

P: n/a
John Harrison wrote:
"ragi" <ra*******@interia.pl> wrote in message
news:ch***********@mamut.aster.pl...
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
Jul 22 '05 #3

P: n/a

"Jonne Lehtinen" <jo************@tut.fi.invalid> wrote in message
news:ch***********@news.cc.tut.fi...
| John Harrison wrote:
| > "ragi" <ra*******@interia.pl> wrote in message
| > news:ch***********@mamut.aster.pl...
| >
| >>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
Jul 22 '05 #4

P: n/a

"Chris ( Val )" <ch******@bigpond.com.au> wrote in message
news:2q************@uni-berlin.de...

"Jonne Lehtinen" <jo************@tut.fi.invalid> wrote in message
news:ch***********@news.cc.tut.fi...
| John Harrison wrote:
| > "ragi" <ra*******@interia.pl> wrote in message
| > news:ch***********@mamut.aster.pl...
| >
| >>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
Jul 22 '05 #5

P: n/a
Jonne Lehtinen <jo************@tut.fi.invalid> wrote:
John Harrison wrote:
"ragi" <ra*******@interia.pl> 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.
Jul 22 '05 #6

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:VH*****************@newsread1.news.pas.earthl ink.net...
|
| "Chris ( Val )" <ch******@bigpond.com.au> wrote in message
| news:2q************@uni-berlin.de...
| >
| > "Jonne Lehtinen" <jo************@tut.fi.invalid> wrote in message
| > news:ch***********@news.cc.tut.fi...
| > | John Harrison wrote:
| > | > "ragi" <ra*******@interia.pl> wrote in message
| > | > news:ch***********@mamut.aster.pl...

[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

Jul 22 '05 #7

P: n/a
Old Wolf wrote:
Jonne Lehtinen <jo************@tut.fi.invalid> wrote:
John Harrison wrote:
"ragi" <ra*******@interia.pl> 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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.