468,315 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

Quick question on operator overloading

I always understood that in C++, if I said

a + b

a.operator+(b) is called.

Now this makes sense with the operator<< when used in the following way

cout << 100; // converts to cout.operator<<(100);

but then how does one explain this one

cout << end; // endl(cout).

In what order does C++ look for operator functions? What all
functions does it search before giving up? In other words

when C++ sees

a+b

does it look for the existence of :

a.operator(b)
b(a)
a(b) ....???

or is the operator<< a special case?

Thanks,
--j

Aug 20 '06 #1
7 1631
John schrieb:
I always understood that in C++, if I said

[snipped]

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.

-mg

In what order does C++ look for operator functions? What all
functions does it search before giving up? In other words

when C++ sees

a+b

does it look for the existence of :

a.operator(b)
b(a)
a(b) ....???

or is the operator<< a special case?

Thanks,
--j
Aug 20 '06 #2

Sorry, but I do not understand.
Is endl a functor? Does the rule of
operator overloading change in case of functors?

Thanks,
--j
Markus Grueneis wrote:
John schrieb:
I always understood that in C++, if I said

[snipped]

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.

-mg

In what order does C++ look for operator functions? What all
functions does it search before giving up? In other words

when C++ sees

a+b

does it look for the existence of :

a.operator(b)
b(a)
a(b) ....???

or is the operator<< a special case?

Thanks,
--j
Aug 20 '06 #3
John posted:
Now this makes sense with the operator<< when used in the following way

cout << 100; // converts to cout.operator<<(100);

but then how does one explain this one

cout << end; // endl(cout).

I searched the Standard for "endl" and found:

template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);

I'm not sure exactly how it works, but if I had to guess, I'd say it might
be something like:

class OStream {
public:

OStream &operator<<(char const *const p)
{
/* Print text */

return *this;
}

OStream &operator<<(OStream &(&Func)(OStream&))
{
return Func(*this);
}
};

OStream &endl(OStream &os)
{
return os << "\n";
}

int main()
{
OStream cout;

cout << "Hello" << endl;
}

--

Frederick Gotham
Aug 20 '06 #4
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]
Aug 20 '06 #5
In article <11*********************@74g2000cwt.googlegroups.c om>,
John <we**********@yahoo.comwrote:
>I always understood that in C++, if I said

a + b

a.operator+(b) is called.

Now this makes sense with the operator<< when used in the following way

cout << 100; // converts to cout.operator<<(100);

but then how does one explain this one

cout << end; // endl(cout).
op<< (not all but the one you're talkign about) is overloaded with many
signatures, taking int, float, etc. One overload accepts an argumwnt
that has a type that is the same as endl's type. Check out the header file.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 20 '06 #6
Frederick Gotham schrieb:
I searched the Standard for "endl" and found:

template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
[...]
OStream &endl(OStream &os)
{
return os << "\n";
}
std::endl flushes the stream, like this:

ostream& endl(ostream& os)
{
os << '\n';
os.flush();
return os;
}

--
Thomas
Aug 20 '06 #7
"John" <we**********@yahoo.comwrote in message
news:11*********************@74g2000cwt.googlegrou ps.com...
>I always understood that in C++, if I said

a + b

a.operator+(b) is called.

Now this makes sense with the operator<< when used in the following way

cout << 100; // converts to cout.operator<<(100);

but then how does one explain this one

cout << end; // endl(cout).

In what order does C++ look for operator functions? What all
functions does it search before giving up? In other words

when C++ sees

a+b

does it look for the existence of :

a.operator(b)
b(a)
a(b) ....???

or is the operator<< a special case?

Thanks,
--j
Here is my compilers definition of std::endl:
template<class _Elem, class _Traitsinline
basic_ostream<_Elem, _Traits>&
__cdecl endl(basic_ostream<_Elem, _Traits>& _Ostr)
{ // insert newline and flush stream
_Ostr.put(_Ostr.widen('\n'));
_Ostr.flush();
return (_Ostr);
}


Aug 20 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Edward Diener | last post: by
2 posts views Thread by pmatos | last post: by
4 posts views Thread by vladimir | last post: by
67 posts views Thread by carlos | last post: by
3 posts views Thread by y-man | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.