John schrieb:
Sorry, but I do not understand.
Is endl a functor? Does the rule of
Yes.
operator overloading change in case of functors?
No, as I showed you, it works absolutly the same way.
>
Thanks,
--j
Please avoid top posting.
Markus Grueneis wrote:
>John schrieb:
[snipped old stuff]
>>but then how does one explain this one
cout << end; // endl(cout).
You can overload operator<< with functors.
For instance (assuming boost::function):
struct blub {
// ... data member
blub& operator<<(boost::function<void (blub&)fn)
{
fn(*this);
return *this;
}
};
You get the idea.
As Frederick Gotham wrote, endl is this:
template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
For the sake of easier redability, let's take:
ostream& endl (ostream& os)
So it has the type
ostream& (ostream&)
The first ostream& means the return value, the (ostream&) the argument
list of the functor.
Now, everything that has to be provided, either as a class member or an
ordinary function, is an overloaded operator<< taking the previously
shown types as argument.
Again, as Frederick wrote (modified by me to match the class names):
ostream& operator<<(ostream& (&Func)(ostream&))
{
return Func(*this);
}
This would be a member function of ostream matching the required types.
"ostream& (&Func)(ostream&)" identified the function argument named
"Func", which must have the type "ostream& (ostream&)". std::endl is
exactly such a function matching this type. So, here you are.
[snipped old stuff]