473,666 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ostringstream and write()

Using ostringstream the buffer gets increased to allow data to be
appended when needed at least when you call <<. I was under the
impression that write() also did but I am having problems that make me
doubt that. Can I depend on the behavior of something like this?

ostringstream os;

os.write("This is a test.", strlen("This is a test."));
cout << os.str() << endl;

If so my problem is obviously caused by something else...

Jan 30 '06 #1
10 2374

ro**********@gm ail.com wrote:
Using ostringstream the buffer gets increased to allow data to be
appended when needed at least when you call <<. I was under the
impression that write() also did but I am having problems that make me
doubt that. Can I depend on the behavior of something like this?

ostringstream os;

os.write("This is a test.", strlen("This is a test."));
cout << os.str() << endl;

If so my problem is obviously caused by something else...


I realized what the problem was. I was using iostream as a polymorphic
entity and it isn't. Code above would have looked more like this
logically invalid code:

ostringstream os;
ostream * ptr = &os;

ptr->write("This is a test.", strlen("This is a test."));

This of course does not work because templates don't have virtual
functions and streams are templates. I was being stupid again.

Jan 30 '06 #2
ro**********@gm ail.com wrote:
ro**********@gm ail.com wrote:
Using ostringstream the buffer gets increased to allow data to be
appended when needed at least when you call <<. I was under the
impression that write() also did but I am having problems that make me
doubt that. Can I depend on the behavior of something like this?

ostringstream os;

os.write("This is a test.", strlen("This is a test."));
cout << os.str() << endl;

If so my problem is obviously caused by something else...
I realized what the problem was. I was using iostream as a polymorphic
entity and it isn't.


How did you get that idea? iostreams are polymorphic.
Code above would have looked more like this logically invalid code:

ostringstream os;
ostream * ptr = &os;

ptr->write("This is a test.", strlen("This is a test."));

This of course does not work because templates don't have virtual
functions and streams are templates.


Again, this is a false assumption. What makes you think that a class
template cannot have virtual functions?

Jan 31 '06 #3

Rolf Magnus wrote:
ro**********@gm ail.com wrote:
ro**********@gm ail.com wrote:
Using ostringstream the buffer gets increased to allow data to be
appended when needed at least when you call <<. I was under the
impression that write() also did but I am having problems that make me
doubt that. Can I depend on the behavior of something like this?

ostringstream os;

os.write("This is a test.", strlen("This is a test."));
cout << os.str() << endl;

If so my problem is obviously caused by something else...
I realized what the problem was. I was using iostream as a polymorphic
entity and it isn't.


How did you get that idea? iostreams are polymorphic.


In order for a class to be polymorphic it has to have virtual
functions. The iostreams in the std lib do not. They are therefore
not polymorphic.

If you doubt this then just do what I did: Call write() on a
stringstream from a pointer to type iostream and see what happens.
Code above would have looked more like this logically invalid code:

ostringstream os;
ostream * ptr = &os;

ptr->write("This is a test.", strlen("This is a test."));

This of course does not work because templates don't have virtual
functions and streams are templates.


Again, this is a false assumption. What makes you think that a class
template cannot have virtual functions?


It was either in the book "Generic Programming" or "Template
Metaprogramming " that I first read this; I believe in the former as it
is part of the reasoning behind policies. Tests with my compiler
generate errors. There is also a discussion on comp.lang.c++.m oderated
entitled "Why a template function cannot be a virtual function?". I
would paste a link but copy paste has decided not to work and I will
probably now have to reboot my computer. You should be able to find it
yourself with the title.

Jan 31 '06 #4
> Code above would have looked more like this
logically invalid code:

ostringstream os;
ostream * ptr = &os;

ptr->write("This is a test.", strlen("This is a test."));

This of course does not work because templates don't have virtual
functions and streams are templates. I was being stupid again.


No, that code is absolutely fine. Your problem must lie somewhere else.

Jan 31 '06 #5
In message <11************ ********@g43g20 00cwa.googlegro ups.com>,
ro**********@gm ail.com writes

Rolf Magnus wrote:
ro**********@gm ail.com wrote:
> ro**********@gm ail.com wrote:
>> Using ostringstream the buffer gets increased to allow data to be
>> appended when needed at least when you call <<. I was under the
>> impression that write() also did but I am having problems that make me
>> doubt that. Can I depend on the behavior of something like this?
>>
>> ostringstream os;
>>
>> os.write("This is a test.", strlen("This is a test."));
>> cout << os.str() << endl;
>>
>> If so my problem is obviously caused by something else...
>
> I realized what the problem was. I was using iostream as a polymorphic
> entity and it isn't.
How did you get that idea? iostreams are polymorphic.


In order for a class to be polymorphic it has to have virtual
functions.


In order for a class to be polymorphic all it needs is to have derived
classes. It's only to make RTTI and dynamic_cast work that it has to
have virtual functions.
The iostreams in the std lib do not. They are therefore
not polymorphic.

If you doubt this then just do what I did: Call write() on a
stringstream from a pointer to type iostream and see what happens.


#include <iostream>
#include <ostream>
#include <sstream>

int main()
{
std::ostringstr eam s;
std::ostream * p = &s;
p->write("abcdefg ", 7);
std::cout << s.str() << '\n';
}

writes this for me:

abcdefg

I think your problem is elsewhere.

--
Richard Herring
Jan 31 '06 #6

Richard Herring wrote:
In message <11************ ********@g43g20 00cwa.googlegro ups.com>,
ro**********@gm ail.com writes
In order for a class to be polymorphic it has to have virtual
functions.


In order for a class to be polymorphic all it needs is to have derived
classes. It's only to make RTTI and dynamic_cast work that it has to
have virtual functions.


If you say so. I'll believe the language standard and common use of
the term though.
#include <iostream>
#include <ostream>
#include <sstream>

int main()
{
std::ostringstr eam s;
std::ostream * p = &s;
p->write("abcdefg ", 7);
std::cout << s.str() << '\n';
}

writes this for me:

abcdefg

I think your problem is elsewhere.


Yes it does. streambuf is polymorphic (its public interface calls
protected virtual members). Removing the abstract reference fixed my
problem and since streams are NOT polymorphic it tricked me. At this
point it doesn't matter...some change I made along the line fixed it
because the abstract reference now functions.

Jan 31 '06 #7
ro**********@gm ail.com wrote:
> ro**********@gm ail.com wrote:
>> Using ostringstream the buffer gets increased to allow data to be
>> appended when needed at least when you call <<. I was under the
>> impression that write() also did but I am having problems that make me
>> doubt that. Can I depend on the behavior of something like this?
>>
>> ostringstream os;
>>
>> os.write("This is a test.", strlen("This is a test."));
>> cout << os.str() << endl;
>>
>> If so my problem is obviously caused by something else...
>
> I realized what the problem was. I was using iostream as a polymorphic
> entity and it isn't.
How did you get that idea? iostreams are polymorphic.


In order for a class to be polymorphic it has to have virtual
functions.


Right.
The iostreams in the std lib do not.
Again, this is _not_ true. They do have virtual member functions.
They are therefore not polymorphic.

If you doubt this
I don't just doubt it. I know it's wrong.
then just do what I did: Call write() on a stringstream from a pointer to
type iostream and see what happens.
#include <iostream>

int main()
{
std::stringstre am stream;
std::iostream* p = &stream;
p->write("x\n", 2);
std::cout << stream.str();
}

The output of the above program on my computer is:

x

That's exactly what I expect.
Code above would have looked more like this logically invalid code:
>
> ostringstream os;
> ostream * ptr = &os;
>
> ptr->write("This is a test.", strlen("This is a test."));
>
> This of course does not work because templates don't have virtual
> functions and streams are templates.


Again, this is a false assumption. What makes you think that a class
template cannot have virtual functions?


It was either in the book "Generic Programming" or "Template
Metaprogramming " that I first read this; I believe in the former as it
is part of the reasoning behind policies. Tests with my compiler
generate errors.


How did you test? Try this:

#include <iostream>

template<typena me T>
class Base
{
public:
virtual void test()
{
std::cout << "This is Base<>::test()\ n";
}
};

template<typena me T>
class Derived : public Base<T>
{
public:
void test()
{
std::cout << "This is Derived<>::test ()\n";
}
};

int main()
{
Derived<char> d;
Base<char>* p = &d;
p->test();
}
There is also a discussion on comp.lang.c++.m oderated entitled "Why a
template function cannot be a virtual function?".


A function template cannot be virtual, but a normal member function of a
class template can.

Jan 31 '06 #8
ro**********@gm ail.com wrote:
> ro**********@gm ail.com wrote:
>> Using ostringstream the buffer gets increased to allow data to be
>> appended when needed at least when you call <<. I was under the
>> impression that write() also did but I am having problems that make me
>> doubt that. Can I depend on the behavior of something like this?
>>
>> ostringstream os;
>>
>> os.write("This is a test.", strlen("This is a test."));
>> cout << os.str() << endl;
>>
>> If so my problem is obviously caused by something else...
>
> I realized what the problem was. I was using iostream as a polymorphic
> entity and it isn't.
How did you get that idea? iostreams are polymorphic.


In order for a class to be polymorphic it has to have virtual
functions.


Right.
The iostreams in the std lib do not.
Ok, maybe only the streambufs, not the streams themselves. Not sure about
this.
They are therefore not polymorphic.

If you doubt this then just do what I did: Call write() on a stringstream
from a pointer to type iostream and see what happens.
#include <iostream>

int main()
{
std::stringstre am stream;
std::iostream* p = &stream;
p->write("x\n", 2);
std::cout << stream.str();
}

The output of the above program on my computer is:

x

That's exactly what I expect.
Code above would have looked more like this logically invalid code:
>
> ostringstream os;
> ostream * ptr = &os;
>
> ptr->write("This is a test.", strlen("This is a test."));
>
> This of course does not work because templates don't have virtual
> functions and streams are templates.


Again, this is a false assumption. What makes you think that a class
template cannot have virtual functions?


It was either in the book "Generic Programming" or "Template
Metaprogramming " that I first read this; I believe in the former as it
is part of the reasoning behind policies. Tests with my compiler
generate errors.


How did you test? Try this:

#include <iostream>

template<typena me T>
class Base
{
public:
virtual void test()
{
std::cout << "This is Base<>::test()\ n";
}
};

template<typena me T>
class Derived : public Base<T>
{
public:
void test()
{
std::cout << "This is Derived<>::test ()\n";
}
};

int main()
{
Derived<char> d;
Base<char>* p = &d;
p->test();
}
There is also a discussion on comp.lang.c++.m oderated entitled "Why a
template function cannot be a virtual function?".


A function template cannot be virtual, but a normal member function of a
class template can.

Jan 31 '06 #9
In message <11************ **********@f14g 2000cwb.googleg roups.com>,
ro**********@gm ail.com writes

Richard Herring wrote:
In message <11************ ********@g43g20 00cwa.googlegro ups.com>,
ro**********@gm ail.com writes

>In order for a class to be polymorphic it has to have virtual
>functions.


In order for a class to be polymorphic all it needs is to have derived
classes. It's only to make RTTI and dynamic_cast work that it has to
have virtual functions.


If you say so. I'll believe the language standard and common use of
the term though.


OK, I'll rephrase that. You are correct about the definition of
polymorphism, but the whole issue of polymorphism is a red herring and
what you were really talking about, access to a base class using a
pointer to a derived class, is not polymorphism, and doesn't need
virtual functions to exist:

4.10/3 An rvalue of type "pointer to cv D", where D is a class type,
can be converted to type "pointer to cv B", where B is a base class of
D. [...] The result of the conversion is a pointer to the base class
sub-object of the derived class object.
#include <iostream>
#include <ostream>
#include <sstream>

int main()
{
std::ostringstr eam s;
std::ostream * p = &s;
p->write("abcdefg ", 7);
std::cout << s.str() << '\n';
}

writes this for me:

abcdefg

I think your problem is elsewhere.


Yes it does. streambuf is polymorphic (its public interface calls
protected virtual members). Removing the abstract reference fixed my
problem and since streams are NOT polymorphic it tricked me. At this
point it doesn't matter...some change I made along the line fixed it
because the abstract reference now functions.


--
Richard Herring
Feb 1 '06 #10

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

Similar topics

4
6198
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ===========================================
6
3015
by: Eric Boutin | last post by:
Hi ! I have a strange problem with a std::ostringstream.. code : #include <sstream> /*...*/ std::ostringstream ss(); ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\" \"\"" << outfilename << "\"\""; //line 75
2
5674
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have seen commercial programs print output to the screen as well as to a log file. depending on the user and other situations, i might want to turn off one of the outputs or maybe even both outputs. so, i want a single line with operator << function...
3
5601
by: Mathieu Malaterre | last post by:
Hello, I am trying to write this simple code: std::ostringstream s; s << 1024; std::cout << s.str() << std::endl; s.str(""); // <- problem s << 512; std::cout << s.str() << std::endl;
3
11116
by: Kyle Kolander | last post by:
I posted this in response to a previous thread but have not gotten any replies back... Hopefully someone has an answer? From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears str("") does not clear the stream bits. It seems to me that in clearing the internal buffer, one would intend that the stream bits would also be reset (in a good state). Is there a reason this is not the defined behavior? Aside from a slight...
3
1396
by: Bob Altman | last post by:
Hi all, Why doesn't the following unmanaged C++ code work as expected: string s; ostringstream strm(s); // This stream should store results in s strm << 25; cout << s << endl; // s still contains an empty string cout << strm.str(); // but the stream internally contains "25"
15
1936
by: iu2 | last post by:
Hi all, can someone help me with this? I'm trying to shorthen some logging function in our project, i.e., instead of LogString("...); use a more convenient log() << "String here " << 12.33 << " and a number";
1
8178
by: schoedl | last post by:
Hello, we often compose strings via a ostringstream and then create a string from it. What is the rationale of not being able to use string in place of a ostringstream, so I could write string str; str << ... << ...; SomeAPI( str.c_str() );
13
2453
by: vsuresh.cs | last post by:
Hi, My c++ program looks likes this void api1() { cin << info; ostringstream ostr; ostr << info; api2(ostr.str().c_str()); // Getting string from ostreamstream and getting char* from string
0
8443
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7385
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6192
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
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 we have to send another system
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.