20

Someone recently asked me the difference between a C++ standard operator (e.g. new,delete,sizeof) and function (e.g. tan,free, malloc). By "standard" I mean those provided by default by the compiler suite, and not user defined. Below were the answers I gave, though neither seemed satisfactory.

(1) An operator doesn't need any headers to be included to use it : E.g. you can have a call to new without including any headers. However, a function (say free() ) does need headers included, compulsorily.

(2) An operator is defined as such (ie as a class operator) somewhere in the standard headers. A function isn't.

Can you critique these answers and give me a better idea of the difference?

PKG
  • 579
  • 6
  • 17
  • is "delete" used in both sets intentionally? – Sev Jun 02 '10 at 06:18
  • See http://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new – nico Jun 02 '10 at 06:19
  • operators are functions, too, even if they have fancy syntax and some overloads are predefined in the language. I guess that what you want to ask is "the difference between c++ defined keywords and c++ functions" – paercebal Jun 02 '10 at 08:25
  • @paercebal: You could say that (some) operators can be _implemented_ by writing functions which follow a special naming scheme, but I wouldn't say "operators are functions". The infix operator `+` is just that: an infix operator. It might be _implemented_ by the function `operator+()` - or it might not (as in `42+1`). – sbi Jun 02 '10 at 13:44
  • 1
    @sbi : I guess it's less the implementation than the semantic idea that is important. There is not clear boundary between operator and function. But there is between a reserved keywords/symbols (like `new` and `+` and identifiers) : The reserved keywords/symbols are already used/known by the compiler, whereas the identifiers are declared/defined (possibly using reserved keywords/symbols), usually in headers (standards or not). – paercebal Jun 02 '10 at 16:48
  • @paercebal: We probably just have to agree to disagree then. – sbi Jun 02 '10 at 18:19

6 Answers6

20

Operators are keywords with a fixed syntax. Those which can be overloaded might vary a bit in syntax, but that's within the boundaries. The new operator is still spelled new, even when overloaded and the syntax of invoking it is always the same.

Function names are identifiers, which can be almost arbitrary. There's no syntactic reason you couldn't do away with malloc() and use

bool my_fancy_alloc(void*& memory, unsigned char size, bool zero_it);

instead. (Mark: There are other reasons, though. Like your fellow-workers' sanity.)

sbi
  • 219,715
  • 46
  • 258
  • 445
  • 1
    +1. I guess the question is wrong at the start (operators vs. functions?), but your answer highlighting the keywords and identifiers answers it all. – paercebal Jun 02 '10 at 08:23
7

An operator is compiled to a sequence of instructions by the compiler.

When the code calls a function, it has to jump to a separate piece of code

Siva Prakash
  • 4,626
  • 34
  • 26
4

(1) isn't strictly true. typeid is an operator, but its use requires that you include <typeinfo>.

My answer would simply be that operators are defined as such. :: doesn't necessarily need to be considered an operator, for instance, but the standard says it is, and so it is.

If you're looking for a more verbose answer, I would go on to mention that operators mostly do not look like functions. And those that do [sizeof and typeid] don't act as functions; their operands are not evaluated at runtime.

Dennis Zickefoose
  • 10,791
  • 3
  • 29
  • 38
  • Thanks. But what happens to pure C operators (such as +)? They can't be class operators, but they're operators because the C standard says so? – PKG Jun 02 '10 at 06:29
  • @user: I don't understand what you're asking. `operator +` can be overloaded for classes. – Dennis Zickefoose Jun 02 '10 at 06:35
2

An operator and function are conceptually same. Both of them take some values as input and return some as output. They only difference is there syntax.

2

Operators have special syntax, e.g. you can use new (or +) without putting arguments in brackets after the operator name.

Matt Curtis
  • 23,168
  • 8
  • 60
  • 63
0

to my understanding function name is a representation of goto operator, after jumping to a specific code location application can perform one to many unit of work, on the other hand an operator perform an actual unit of work.

sowrov
  • 1,018
  • 10
  • 16