473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 2360

ro**********@gmail.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**********@gmail.com wrote:
ro**********@gmail.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**********@gmail.com wrote:
ro**********@gmail.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++.moderated
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********************@g43g2000cwa.googlegroups.c om>,
ro**********@gmail.com writes

Rolf Magnus wrote:
ro**********@gmail.com wrote:
> ro**********@gmail.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::ostringstream 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********************@g43g2000cwa.googlegroups.c om>,
ro**********@gmail.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::ostringstream 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**********@gmail.com wrote:
> ro**********@gmail.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::stringstream 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<typename T>
class Base
{
public:
virtual void test()
{
std::cout << "This is Base<>::test()\n";
}
};

template<typename 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++.moderated 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**********@gmail.com wrote:
> ro**********@gmail.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::stringstream 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<typename T>
class Base
{
public:
virtual void test()
{
std::cout << "This is Base<>::test()\n";
}
};

template<typename 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++.moderated 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**********************@f14g2000cwb.googlegroups .com>,
ro**********@gmail.com writes

Richard Herring wrote:
In message <11********************@g43g2000cwa.googlegroups.c om>,
ro**********@gmail.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::ostringstream 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

Richard Herring wrote:
In message <11**********************@f14g2000cwb.googlegroups .com>,
ro**********@gmail.com writes

Richard Herring wrote:
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:


No, it is not polymorphism. Calls made to overriden members of
non-virtual functions result in the call being made to the parent type.
If the overridden members need to do such things as bounds checking on
an internal buffer before doing the stuff normally performed by the
function ... and that stuff is not done because the function called was
the one belonging to the parent ... then you have problems.

Such a design obviously has problems, which is why streams work off of
buffers that ARE polymorphic.

Feb 1 '06 #11

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

Similar topics

4
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...
6
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 << "\"\"...
2
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...
3
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
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...
3
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...
15
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 " <<...
1
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 ...
13
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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
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...

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.