Why is a pointer not used for substract ?
In fact, the entire function name is a pointer in the corresponding scope. Here, in your code, when you define the substract() , add() function.
You also defined two variables named substract and add , the type of which is a pointer to a function: int (*)(int, int) . therefore, you can declare a new plus function pointer and assign add to it. All three variables are pointers.
Below is the assembly code generated by clang++ , which can give you a detailed explanation. I deleted all the inappropriate code. The function name is a little ugly to read, it is related to C ++ name mangling , you can just ignore the main characters and numbers to understand the name.
add() function:
.text .globl _Z3addii .align 16, 0x90 .type _Z3addii,@function _Z3addii: # @_Z3addii .cfi_startproc # BB#0: # %entry movl %edi, -4(%rsp) movl %esi, -8(%rsp) movl -4(%rsp), %esi addl -8(%rsp), %esi movl %esi, %eax ret .Ltmp6: .size _Z3addii, .Ltmp6-_Z3addii .cfi_endproc
substract function:
.globl _Z8subtractii .align 16, 0x90 .type _Z8subtractii,@function _Z8subtractii: # @_Z8subtractii .cfi_startproc # BB#0: # %entry movl %edi, -4(%rsp) movl %esi, -8(%rsp) movl -4(%rsp), %esi subl -8(%rsp), %esi movl %esi, %eax ret .Ltmp7: .size _Z8subtractii, .Ltmp7-_Z8subtractii .cfi_endproc
The label _Z3addii and _Z8subtractii gives the starting point of the two functions, which is also the address that points to the beginning of the function.
I added a few comments to the code to show how a function pointer that starts with ### works.
main function:
.globl main .align 16, 0x90 .type main,@function main:
In the above main function, we saw that the entire value of the function pointer has been moved to the rdx register. Here in the following functional function:
operation() function:
.globl _Z9operationiiPFiiiE .align 16, 0x90 .type _Z9operationiiPFiiiE,@function _Z9operationiiPFiiiE:
So, from the assembly it is clear that the variable substract , add , plus in your given code is all pointers and taken from the label of the starting point of the function.
What is the difference between the two methods?
Since plus , add and substract are the same type function pointers, as Lucian Grigore says, they are the same.
From the assembler code above, we can also find out that both methods are exactly the same method call without any differences.
Is this pointed to by C++ ?
C family languages
and some other derived languages, such as obj-c .
Java language
In Java, there is such a thing as function pointers, but a class that implemnts interface could achieve the same purpose as a function pointer.
For example: you can first define an interface:
interface StringFunction { int function(String param); }
and then define a function that can accept objects that implement the interface:
public void takingMethod(StringFunction sf) {
And then you can define different classes that implements interface StringFunction , and use it as parameter takingMethod()
Python
In Python, a function name is just a variable type, and you can use it directly, for example, in the following ways:
def plus_1(x): return x + 1 def minus_1(x): return x - 1 func_map = {'+' : plus_1, '-' : minus_1} func_map['+'](3) # returns plus_1(3) ==> 4 func_map['-'](3) # returns minus_1(3) ==> 2
ruby
Ruby also has several ways: Function pointer in Ruby?