Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inheriting ostream

Reply
Thread Tools

inheriting ostream

 
 
enzo
Guest
Posts: n/a
 
      01-16-2004
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
 
Reply With Quote
 
 
 
 
Jacques Labuschagne
Guest
Posts: n/a
 
      01-16-2004
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;
}

 
Reply With Quote
 
 
 
 
Martijn Lievaart
Guest
Posts: n/a
 
      01-16-2004
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<typename 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

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      01-17-2004
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<typename 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheriting from ostream to customize standard output aaragon C++ 4 04-03-2011 12:39 PM
Strange compiler error w.r.t 'friend ostream& operator<< <>(ostream&,...)' abhay.burli@gmail.com C++ 2 02-26-2009 06:18 AM
Inheriting from std::ostream to override formatting. AJG C++ 1 06-29-2008 09:57 AM
ostream value output question Parity C++ 2 08-13-2003 10:50 PM
Line wrapping in ostream Alex Dribin C++ 2 08-06-2003 05:14 PM



Advertisments