473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Difference between operator and function

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
7 6653
* 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
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
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
* 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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: learning_C++ | last post by:
Hi, I compiled some code. In the function friend ostream& operator<<(ostream& os, const complex c); I use the later argument complex c and complex& c. I can get the same values and there is no...
16
by: RS | last post by:
Hi, What is the difference between new() and malloc()? RS
12
by: z. f. | last post by:
what's the difference between using the VB.NET function IIF or the c# operator ? VB.NET: a = iif ( b=c, d, e) C#: a= (b==c)?d:e TIA, z.
6
by: fcvcnet | last post by:
Hi, I read the book C++ Primer, Fourth Edition By Stanley B. Lippman, Jos¨¦e Lajoie, Barbara E. Moo "If we define a class using the class keyword, then any members defined before the first...
9
by: waltbrad | last post by:
For an exercise I had to write a class that would do math operations on complex numbers. The main point of the exercise was overloading various operators. One of them was the conjugate operator...
6
by: mthread | last post by:
Hi, I am learning C++ and I have been told that an object can be created either by using calloc or new. If it is true, can some one tell me what is the difference b/w using these two function...
10
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
2
by: Peng Yu | last post by:
Hi, In the following code, the 'copy' member function works. But the '=' operator does not work. Can somebody let me know why a member function is different from an operator. Thanks, Peng ...
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.