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 7 1820
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<<(boos t::function<voi d (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
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<<(boos t::function<voi d (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
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<c harT,traits>& endl(basic_ostr eam<charT,trait s>& 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<<(cha r const *const p)
{
/* Print text */
return *this;
}
OStream &operator<<(OSt ream &(&Func)(OStrea m&))
{
return Func(*this);
}
};
OStream &endl(OStrea m &os)
{
return os << "\n";
}
int main()
{
OStream cout;
cout << "Hello" << endl;
}
--
Frederick Gotham
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<<(boos t::function<voi d (blub&)fn) { fn(*this); return *this; } };
You get the idea.
As Frederick Gotham wrote, endl is this:
template <class charT, class traits>
basic_ostream<c harT,traits>& endl(basic_ostr eam<charT,trait s>& 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<<(ostr eam& (&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]
In article <11************ *********@74g20 00cwt.googlegro ups.com>,
John <we**********@y ahoo.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?
Frederick Gotham schrieb:
I searched the Standard for "endl" and found:
template <class charT, class traits>
basic_ostream<c harT,traits>& endl(basic_ostr eam<charT,trait s>& os);
[...]
OStream &endl(OStrea m &os)
{
return os << "\n";
}
std::endl flushes the stream, like this:
ostream& endl(ostream& os)
{
os << '\n';
os.flush();
return os;
}
--
Thomas
"John" <we**********@y ahoo.comwrote in message
news:11******** *************@7 4g2000cwt.googl egroups.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_ostr eam<_Elem, _Traits>& _Ostr)
{ // insert newline and flush stream
_Ostr.put(_Ostr .widen('\n'));
_Ostr.flush();
return (_Ostr);
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Edward Diener |
last post by:
Is there a way to override the default processing of the assignment operator
for one's own __value types ? I realize I can program my own Assign method,
and provide that for end-users of my class, but I would like to use
internally my own = operator for some of my value types, so I can say "x =
y;" rather than "x.Assign(y);".
The op_Assign operator seems impossibly broken since it takes __value copies
of the two objects. Perhaps there is...
|
by: Pmb |
last post by:
I've been working on creating a Complex class for my own learning purpose
(learn through doing etc.). I'm once again puzzled about something. I can't
figure out how to overload the assignment operator.
Here's what I'm trying to do. I've defined class Complex as
class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
|
by: gorda |
last post by:
Hello,
I am playing around with operator overloading and inheritence,
specifically overloading the + operator in the base class and its
derived class.
The structure is simple: the base class has two int memebers "dataA",
"dataB". The derived class has an additional int member "dataC". I am
simply trying to overload the + operator so that 'adding' two objects
will sum up the corresponding int members.
|
by: pmatos |
last post by:
Hi all,
I'm overloading operator<< for a lot of classes. The question is about
style. I define in each class header the prototype of the overloading
as a friend. Now, where should I define the overloading of operator<<.
In the .cc of the respective class or in a file where I am overloading
operator<< for all classes?
Cheers,
|
by: vladimir |
last post by:
All,
I have seemingly quite known problem, namely I need to watch overloaded
in quick watch(by own vector) but I can not. It says overloaded
operator is not found. And that is claimed normal here (at least once).
But I have a project where at least for local class variables can be
watched. I see absolutely no difference in settings/options, and the
class is just copied from old project. Ans it is template class (if it
does matter).
| |
by: carlos |
last post by:
Curious:
Why wasnt a primitive exponentiation operator not added to C99?
And, are there requests to do so in the next std revision?
Justification for doing so:
C and C++ are increasingly used in low-level
numerical computations, replacing Fortran in newer projects.
Check, for example, sourceforge.net or freshmeat.net
But neither language offers a primitive exp operator.
|
by: karthik |
last post by:
The * operator behaves in 2 different ways. It is used as the value at
address operator as well as the multiplication operator. Does this mean
* is overloaded in c?
|
by: y-man |
last post by:
Hi,
I am trying to get an overloaded operator to work inside the class it
works on. The situation is something like this:
main.cc:
#include "object.hh"
#include "somefile.hh"
object obj, obj2 ;
|
by: Wayne Shu |
last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition).
When I read chapter 11 Operator Overloading, I have two questions.
1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,
operator =, operator, operator(), and operator-must be nonstatic
member function; this ensures that their first operands will be
lvalues". I know that these operators must be nonstatic member
functions, but why this ensure their first operands will...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |