Click to See Complete Forum and Search --> : overriding manipulator endl
kakalake
February 22nd, 2003, 08:27 AM
How can i override the manipulator endl. I tried the following thing:
#include<iostream>
inline ostream& std::endl( ostream& os )
{
os << "\n\n\n" << flush;
return os;
}
void main(void)
{
cout << "hallo" << std::endl;
}
But it doesen´t work. Why? It should. I thing i have some problem with the namespace, maby.
Thanks in advance
mwilliamson
February 22nd, 2003, 08:47 AM
endl is defined as
template class<_Elem, _Tr>
basic_ostream<_Elem, _Tr>& endl(
basic_ostream<_Elem, _Tr>& _Ostr
);
so try defining your function the same. Also, I don't think its a good idea to try and use the std namespace too.
kakalake
February 22nd, 2003, 09:10 AM
I know that endl is definied this way. I already tried to redefine endl in this way, but it doesn´t work too. Now i used the following code without any success:
template <class _Elem, class _Tr>
inline basic_ostream<_Elem, _Tr>& endl( basic_ostream<_Elem, _Tr>& OutStream )
{
OutStream << "\n\n\n" << endl;
return OutStream;
}
It is no problem to define my own manipulator but when i try to redefine one it doesn´t work...
Thanks
PaulWendt
February 22nd, 2003, 10:13 AM
Why do you expect to be able to redefine a function that's global
to a namespace? Is that possible to do?
--Paul
kakalake
February 22nd, 2003, 11:34 AM
Hello!
I found something in my programming book. The author write that you can add!!! manipulators to the already existing ones. I thing it is not possible to change the behavior of the ex. manipulators. But if someone knows an way i would be very interessed in.
Thanks
PaulWendt
February 22nd, 2003, 12:45 PM
Well, overriding functions only works when you create a class
that is derived from another class. Each function will have its own
signature, however. In this manner, the compiler knows to insert
code to call that one particular function. In the event of virtual
functions, that decision is made at run-time. With "global"
functions [like manipulators] ... even if they're in a namespace
[like std] ... you can't just redefine the function because then
you will have two functions with the same signature. If you
supply endl, which version should the compiler call? Usually,
endl is going to be implemented entirely in a header file
somewhere in your std headers. If you REALLY need to change
the behavior of endl, you'd have to edit this header file. However,
this is clearly non-portable behavior.
Since you can add your own manipulators, there's no real need
to change endl's behavior. If you SOMETIMES want to output a
newline and flush the buffer and SOMETIMES you want other
behavior, you could have another manipulator that checks the
existance of a global boolean; if this boolean is true, it does the
customized behavior; otherwise, it could call endl.
If you let us know what you're really trying to do, we can help
you out more effectively :)
--Paul
Graham
February 22nd, 2003, 02:43 PM
I thought the standard says that you shouldn't do this sort of thing. You're not allowed to extend the std namespace (so, by implication, you shouldn't alter or overload anything defined in it).
To modify one of Paul's questions slightly: Why on earth would you want to override/overload std::endl? As Paul says, write your own manipulator - it's easy enough, Stroustrup has a whole section on it in TC++PL.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.