473,763 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inheriting ostream

hi all!

i need in my libraries a class to deal with messages.

I did:

class messenger
{
public:
messenger(const char* p, ostream& p_s) : m(p), m_stream(p_s)
{
}

string out()
{
m_s << m;
return m;
}

messenger&
operator << (const char* p)
{
m_s << p;
return *this;
}

private:
string m;
ostream& m_stream;
};
this class let me do:

int main()
{
messenger l(¨hallo world¨, cout);
l.out();
l << ¨hallo again¨;
return 1;
}

but i can't do:

l << endl;

i tried to change to :

class messenger : public ostream

but doesn't compile.

What am i doing wrong??

Thanks
Jul 22 '05 #1
3 2813
enzo wrote:

l << endl;


Because std::endl is a function, not a const char*. Just add this:

message& operator<< (ostream& (*f)(ostream&)) {
m_s << f;
return *this;
}

Jul 22 '05 #2
On Sat, 17 Jan 2004 00:25:27 +1300, Jacques Labuschagne wrote:
enzo wrote:

l << endl;


Because std::endl is a function, not a const char*. Just add this:

message& operator<< (ostream& (*f)(ostream&)) {
m_s << f;
return *this;
}


Even better, add:

template<typena me t>
message& operator<< (const T& t){
m_s << f;
return *this;
}

as the only operator<< and suddenly everything that can be written to an
ostream can me written to a message.

HTH,
M4

Jul 22 '05 #3
Martijn Lievaart wrote:
enzo wrote:

l << endl;


Because std::endl is a function, not a const char*. Just add this:

message& operator<< (ostream& (*f)(ostream&)) {
m_s << f;
return *this;
}


Even better, add:

template<typena me t>
message& operator<< (const T& t){
m_s << f;
return *this;
}

as the only operator<< and suddenly everything that can be written to an
ostream can me written to a message.
...


Not true. (And what Jacques said is not exactly correct either.)
'std::endl' is not a function, it is a function template. The compiler
won't be able to perform template argument deduction if you try to
output 'std::endl' with the above template 'operator<<'.

Jacques' solution will work because it provides compiler with enough
information to perform successful template argument deduction for
'std::endl'.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4

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

Similar topics

3
8266
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it at all possible?
3
2185
by: Christopher Benson-Manica | last post by:
This is starting to seem ridiculous to me :( #include <streambuf> #include <iostream> class TWFileStream : public std::streambuf { private: char cbuf;
4
3061
by: Thomas Matthews | last post by:
Hi, My objective is to add enable & disable functionality to the ofstream class. I want to have a log file where I can disable and enable output to it. When my first tests pass, I want to disable the annotations written to the log file, but enable the text from the newer test cases. I've searched the C++ newsgroups and the FAQ-Lite
2
2957
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
6
3254
by: silversurfer2025 | last post by:
Hello, I am currently trying to derive a class from ostream (which is giving output to my GUI), such that I can give my methods either std::cout or my own outputstream-class to be used as output stream. Unfortunately, I get a runtime error: "segmentation fault" each time I am using the overloaded << function but cannot find the mistakt.. Maybe somebody hwo is more of n expert than me (which shouldn't be too hard) knows what to change?
24
2957
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
6
1620
by: johnmmcparland | last post by:
Hi all, when I write a subclass, can I inherit its superclass' << operator? As an example say we have two classes, Person and Employee. class Person has the << operator; /**
6
4728
by: syang8 | last post by:
Any one can specify the problem of the following code? The compiling error is on the friend function. If the base class is not inherited from ostream, or I just remove the friend function from the base class, the code will compile. ------------------------------------------------------------------------- #include <iostream> template<class Tclass base; template<class Tstd::ostream& operator<< (std::ostream&, base<T>&);
1
3445
by: AJG | last post by:
Hi there. I am using a library called SOCI that has a method to set a stream which it uses to log SQL queries. The signature is as follows: void setLogStream(std::ostream *s); This works great when used with something like setLogStream(&std::cerr). However, I would like to add more context to the query logged. Specifically, I'd like to add the thread ID. So, instead of the query logged looking like:
0
9389
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
10003
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9828
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
6643
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5271
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...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3918
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
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2797
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.