Connecting Tech Pros Worldwide Forums | Help | Site Map

ifstream as parameter

Gunnar
Guest
 
Posts: n/a
#1: Jul 22 '05
Hello.

The (stupid) code below does not compile with the message

In method `istream::istream(const istream &)':
/usr/lib/gcc-lib/i386-linux/2.95.4/../../../../include/g++-3/streambuf.h:128:
`ios::ios(const ios &)' is private
Silly.cpp:15: within this context

It's the function call to "foo" that does not work. My question is, what is
it exactly that makes the compiler (g++ 2.95.4) fail on this?
In the above mentioned file it says:

class ios : public _ios_fields {
ios& operator=(ios&); /* Not allowed! */
ios (const ios&); /* Not allowed! */

What does this mean? The assignment operator and CC is not allowed, but how
can I see that??

(It works fine if I use void foo(ifstream& fil) instead).

-------------------------------------
#include <fstream>
#include <iostream>
using namespace std;

void foo(ifstream fil){
fil.close();
}

int main(){
ifstream fil;
fil.open("hihi");
if (fil.is_open())
cout<<"File open"<<endl;
foo(fil);
if (fil.is_open())
cout<<"File open"<<endl;
}


Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 22 '05

re: ifstream as parameter


Gunnar wrote:
[color=blue]
> Hello.
>
> The (stupid) code below does not compile with the message
>
> In method `istream::istream(const istream &)':
> /usr/lib/gcc-lib/i386-linux/2.95.4/../../../../include/g++-3/streambuf.h:128:
> `ios::ios(const ios &)' is private
> Silly.cpp:15: within this context
>
> It's the function call to "foo" that does not work. My question is,
> what is it exactly that makes the compiler (g++ 2.95.4) fail on this?
> In the above mentioned file it says:
>
> class ios : public _ios_fields {
> ios& operator=(ios&); /* Not allowed! */
> ios (const ios&); /* Not allowed! */
>
> What does this mean?[/color]

Streams are not copyable.
[color=blue]
> The assignment operator and CC is not allowed, but how can I see
> that??[/color]

Ehm, you see it in form of a compiler error message if you try to copy a
stream.
[color=blue]
> (It works fine if I use void foo(ifstream& fil) instead).[/color]

If you pass an object by reference, it is not copied.

Gunnar
Guest
 
Posts: n/a
#3: Jul 22 '05

re: ifstream as parameter


>> class ios : public _ios_fields {[color=blue][color=green]
>> ios& operator=(ios&); /* Not allowed! */
>> ios (const ios&); /* Not allowed! */
>>
>> What does this mean?[/color]
> Streams are not copyable.[/color]
Well, yes, it says so, but what is it that makes it uncopyable?

How can I write my own class
class Foo{
int number;
};

and make it forbidden to copy?

Foo& (const Foo&);

seems ok to me, but perhaps I've just forgotten about CC's?
Buster
Guest
 
Posts: n/a
#4: Jul 22 '05

re: ifstream as parameter



"Gunnar" <gunix@comhem.se> wrote[color=blue][color=green][color=darkred]
> >> class ios : public _ios_fields {
> >> ios& operator=(ios&); /* Not allowed! */
> >> ios (const ios&); /* Not allowed! */
> >>
> >> What does this mean?[/color]
> > Streams are not copyable.[/color]
> Well, yes, it says so, but what is it that makes it uncopyable?
>
> How can I write my own class
> class Foo{
> int number;
> };
>
> and make it forbidden to copy?
>
> Foo& (const Foo&);
>
> seems ok to me, but perhaps I've just forgotten about CC's?[/color]

Make the copy constructor and assignment operator private (like in
the "ios" example - remember members are private by default if you
declare your class with "class"). If you write any constructors you
don't get the implicit default constructor, so make sure you write
at least one accessible constructor or you won't be able to create
any objects of this class.

Regards
Buster


Gunnar
Guest
 
Posts: n/a
#5: Jul 22 '05

re: ifstream as parameter


> Make the copy constructor and assignment operator private
Ah! That was a clever solution!
Thanks!
Closed Thread