473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Copying" iostreams

As we all know, iostreams cannot be assigned or copied. This gives me
a problem: suppose we have

class Foo {
std::istream in;
public:
Foo::Foo(std::istream& in_) : in(in_.rdbuf()) {}
};

This would not compile, as std::ios_base has a private copy constructor.

The question is how would we actually "copy" an iostream - that is, in
the sense that in and in_ would be extracting from the same source?

--
I am only a mirage.
Mar 6 '06 #1
4 2386
kelvSYC wrote:
As we all know, iostreams cannot be assigned or copied. This gives me
a problem: suppose we have

class Foo {
std::istream in;
public:
Foo::Foo(std::istream& in_) : in(in_.rdbuf()) {}
};

This would not compile, as std::ios_base has a private copy constructor.

The question is how would we actually "copy" an iostream - that is, in
the sense that in and in_ would be extracting from the same source?


Just use a reference:

class Foo {
std::istream& in;
public:
Foo::Foo(std::istream& in_) : in(in_) {}
};

Mathias
Mar 6 '06 #2
kelvSYC wrote:
As we all know, iostreams cannot be assigned or copied. This gives me
a problem: suppose we have

class Foo {
std::istream in;
public:
Foo::Foo(std::istream& in_) : in(in_.rdbuf()) {}
};

This would not compile, as std::ios_base has a private copy constructor.

The question is how would we actually "copy" an iostream - that is, in
the sense that in and in_ would be extracting from the same source?

Use a reference.

--
Ian Collins.
Mar 6 '06 #3
Mathias Waack wrote:
kelvSYC wrote:
As we all know, iostreams cannot be assigned or copied. This gives me
a problem: suppose we have

class Foo {
std::istream in;
public:
Foo::Foo(std::istream& in_) : in(in_.rdbuf()) {}
};

This would not compile, as std::ios_base has a private copy constructor.

The question is how would we actually "copy" an iostream - that is, in
the sense that in and in_ would be extracting from the same source?


Just use a reference:

class Foo {
std::istream& in;
public:
Foo::Foo(std::istream& in_) : in(in_) {}
};


However, that relies on the assumption that the stream lives at least as
long as the instance of the class that's using it.
An alternative might be to allocate the stream dynamically and use a shared
pointer to ensure the stream lives long enough.

Mar 6 '06 #4
kelvSYC wrote:
class Foo {
std::istream in;
public:
Foo::Foo(std::istream& in_) : in(in_.rdbuf()) {}
};

This would not compile,
Why not? It compiles fine for me and this is the way it should be
(well, I don't think that you are not allowed to fully qualify the
class name in the class definition, i.e. you should drop the "Foo::"
from the code). Of course, if you want to use 'Foo's copy
constructor and/or copy assignment, you need to define these
reasonably, too:

Foo(Foo const& f): in(f.in.rdbuf()) {}
Foo& operator= (Foo const& f) {
this->in.rdbuf(f.in.rdbuf());
return *this;
}
as std::ios_base has a private copy constructor.
You constructor does not need the copy constructor of 'std::ios_base'.
However, the copy constructor would, indeed, need this constructor.
The question is how would we actually "copy" an iostream - that is, in
the sense that in and in_ would be extracting from the same source?


The *real* question is rather, why would we want to have a stream
be a member of an object (well, there are a few examples of this
setup) or even more than one object? Except for some logging and
connection classes I never had the need to store a stream object
within an object and in all those cases there was only one client
of the stream. Typically, the association between a stream and an
object is rather short-lived and only exists when reading the object.
For this purpose, it does not make much sense to embed the stream
into an object.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
17622
by: x-herbert | last post by:
Hi, I have a small test to "compile" al litle script as a WMI-Tester. The script include a wmi-wrapper and "insert" the Win32-modeles. here the code: my "WMI-Tester.py" ----- import wmi
6
3933
by: Luc Saffre | last post by:
Hello, I had a strange problem when freezing (using either py2exe or McMillan installer) a script that imports reportlab (which imports PIL (which imports FixTk))). - Python 2.3.3c (also with...
8
2969
by: CoolPint | last post by:
Is there any way I can reduce the size of internal buffer to store characters by std::string? After having used a string object to store large strings, the object seems to retain the large...
3
4678
by: 21novembre | last post by:
Hi all, I made a question several days before to describe my strange trouble of mysqldump. But I still can't figour it out. Well, I just want to ask another question whether I could just backup...
81
7219
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
3
2967
by: Leo Nunez | last post by:
Hello! I need copy from structure "A" to "B" that contains "strings" in a one line code. Me problem like this : typedef struct tHeader{ char field1; char field2; char field3;
32
2158
by: Axel Bock | last post by:
Hi all, I am trying to get my head around what happens if I return a class object from a function. It seems C++ (MinGW) does not invoke the copy constructor if I do something like this: ...
1
2256
by: hartattack72 | last post by:
Hello. I am new to access, but have had great success in creating an application. My one user is requesting a function to save time in data entry... and I am stumped being the plebian that I am. ...
30
3785
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.