Connecting Tech Pros Worldwide Help | Site Map

ifstream as parameter

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:47 AM
Gunnar
Guest
 
Posts: n/a
Default ifstream as parameter

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;
}


  #2  
Old July 22nd, 2005, 06:47 AM
Rolf Magnus
Guest
 
Posts: n/a
Default 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.

  #3  
Old July 22nd, 2005, 06:47 AM
Gunnar
Guest
 
Posts: n/a
Default 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?
  #4  
Old July 22nd, 2005, 06:47 AM
Buster
Guest
 
Posts: n/a
Default 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


  #5  
Old July 22nd, 2005, 06:47 AM
Gunnar
Guest
 
Posts: n/a
Default Re: ifstream as parameter

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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.