By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Difference between operator and function

P: n/a
What is the difference between an operator and a function in C++?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Oct 29 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* Alex Vinokur:
What is the difference between an operator and a function in C++?
An operator is a special kind of function, with a non-alphanumeric name,
an operator symbol, which can be invoked using prefix, infix or postfix
notation, e.g.

-x // prefix unary minus '-'
a < b // infix less than '<'
functioid( 3.14 ) // Postfix function call operator '()'

To define an operator you use the word 'operator' followed by the
operator symbol.

E.g.,

bool operator<( Thing const& a, Thing const& b )
{
return a.isLessThan( b );
}

There are restrictions on defining some operators.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 29 '06 #2

P: n/a
On Sun, 29 Oct 2006 18:02:36 +0100, "Alf P. Steinbach"
<al***@start.nowrote in comp.lang.c++:
* Alex Vinokur:
What is the difference between an operator and a function in C++?

An operator is a special kind of function, with a non-alphanumeric name,
an operator symbol, which can be invoked using prefix, infix or postfix
notation, e.g.
The concept of "special kind of function" is a very strange one, and
certainly not supported by any wording in the C++ language.

The notion that operators must have a non-alphanumeric name is quite
easily refuted by the following operators:

- sizeof
- const_cast
- static_cast
- dynamic_cast
- reinterpret_cast

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 30 '06 #3

P: n/a
On 29 Oct 2006 08:50:48 -0800, "Alex Vinokur"
<al****@users.sourceforge.netwrote in comp.lang.c++:
What is the difference between an operator and a function in C++?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
I read, and seriously disagree with, Alf's reply.

Here is the difference as I see it:

Operators are defined as operators by the language. Some operators
are overloadable, others are not. Most operators do not create
sequence points (although when overloaded, the overloaded versions are
functions and do impose sequence points). The number of operands on
which an operator operates is fixed and immutable.

Other than the fact that functions are overloadable, none of the above
applies to them.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 30 '06 #4

P: n/a
* Jack Klein:
On Sun, 29 Oct 2006 18:02:36 +0100, "Alf P. Steinbach"
<al***@start.nowrote in comp.lang.c++:
>* Alex Vinokur:
>>What is the difference between an operator and a function in C++?
An operator is a special kind of function, with a non-alphanumeric name,
an operator symbol, which can be invoked using prefix, infix or postfix
notation, e.g.

The concept of "special kind of function" is a very strange one,
No, it's not a strange concept, it's the basic idea.

The standard calls it an "operator function" (some operator functions
can be user-defined).

and
certainly not supported by any wording in the C++ language.
Yes, the standard does not AFAIK define the general concept of
operators. AFAIK there's not even a list of operators, except if a
table with punctuation symbols thrown in is OK. The reader is simply
supposed to already have a working notion of what operators are.

The notion that operators must have a non-alphanumeric name is quite
easily refuted by the following operators:

- sizeof
- const_cast
- static_cast
- dynamic_cast
- reinterpret_cast
In addition to those there's 'new', 'delete', 'and', 'and_eq', 'bitand',
'bitor', 'compl', 'not', 'not_eq', 'or', 'or_eq', 'xor' and 'xor_eq'.

So clearly an operator mustn't necessarily have a non-alphanumeric name.

But operators /usually/ are non-alphanumeric (those I listed here,
except the two first, are in practice not used, and those you listed
cannot be overridden, so these alphanumeric operators are very special
cases), and since they usually are, they /can/ be non-alphanumeric.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 30 '06 #5

P: n/a
"Jack Klein" <ja*******@spamcop.netwrote in message news:nf********************************@4ax.com...
[snip]
Most operators do not create sequence points (although when overloaded, the overloaded versions are functions and do impose
sequence points).
[snip]

What does "sequence points" mean?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Oct 30 '06 #6

P: n/a
Alex Vinokur wrote:
"Jack Klein" <ja*******@spamcop.netwrote in message
news:nf********************************@4ax.com... [snip]
>Most operators do not create sequence points (although when
overloaded, the overloaded versions are functions and do impose
sequence points).
[snip]

What does "sequence points" mean?
www.Google.com and make it a habit.
Oct 30 '06 #7

P: n/a
Alex Vinokur <al****@users.sourceforge.netwrote:
"Jack Klein" <ja*******@spamcop.netwrote in message news:nf********************************@4ax.com...
[snip]
>Most operators do not create sequence points (although when overloaded, the overloaded versions are functions and do impose
sequence points).
[snip]

What does "sequence points" mean?
Since this is something that C++ inherited from C:
http://www.c-faq.com/expr/seqpoints.html

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Oct 30 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.