Connecting Tech Pros Worldwide Help | Site Map

ifstream as parameter

  #1  
Old July 22nd, 2005, 07:47 AM
Gunnar
Guest
 
Posts: n/a
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, 07:47 AM
Rolf Magnus
Guest
 
Posts: n/a

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, 07:47 AM
Gunnar
Guest
 
Posts: n/a

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, 07:47 AM
Buster
Guest
 
Posts: n/a

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, 07:47 AM
Gunnar
Guest
 
Posts: n/a

re: ifstream as parameter


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
ifstream/string ctor Chris Forone answers 4 December 18th, 2007 09:15 AM
question about ifstream::read() supemoy@gmail.com answers 4 May 11th, 2007 05:25 PM
ifstream seeking toton answers 3 February 15th, 2007 05:45 PM
how to print a filename from ifstream? sam answers 10 July 23rd, 2005 05:02 AM