Why a function is not an object? How does it differ?
To understand this, let's move from bottom to top in terms of abstractions involved. So, you have your address space through which you can define the state of the memory and we have to remember that fundamentally it's all about this state you operate on.
Okay, let's move a bit higher in terms of abstractions. I am not taking about any abstractions imposed by a programming language yet (like object, array, etc.) but simply as a layman I want to keep a record of a portion of the memory, lets call it Ab1
and another one called Ab2
.
Both have a state fundamentally but I intend to manipulate/make use of the state differently.
Differently...Why and How?
Why ?
Because of my requirements (to perform addition of 2 numbers and store the result back, for example). I will be using use Ab1
as a long usage state and Ab2
as relatively shorter usage state. So, I will create a state for Ab1
(with the 2 numbers to add) and then use this state to populate some of state of Ab2
(copy them temporarily) and perform further manipulation of Ab2
(add them) and save a portion of resultant Ab2
to Ab1
(the added result). Post that Ab2
becomes useless and we reset its state.
How?
I am going to need some management of both the portions to keep track of what words to pick from Ab1
and copy to Ab2
and so on. At this point I realize that I can make it work to perform some simple operations but something serious shall require a laid out specification for managing this memory.
So, I look for such management specification and it turns out there exists a variety of these specifications (with some having built-in memory model, others provide flexibility to manage the memory yourself) with a better design. In-fact because they(without even dictating how to manage the memory directly) have successfully defined the encapsulation for this long lived storage and rules for how and when this can be created and destroyed.
The same goes for Ab2
but the way they present it makes me feel like this is much different from Ab1
. And indeed, it turns out to be. They use a stack for state manipulation of Ab2
and reserve memory from heap for Ab1
. Ab2
dies after a while.(after finished executing).
Also, the way you define what to do with Ab2
is done through yet another storage portion called Ab2_Code
and specification for Ab1
involves similarly Ab1_Code
I would say, this is fantastic! I get so much convenience that allows me to solve so many problems.
Now, I am still looking from a layman's perspective so I don't feel surprised really having gone through the thought process of it all but if you question things top-down, things can get a bit difficult to put into perspective.(I suspect that's what happened in your case)
BTW, I forgot to mention that Ab1
is called an object officially and Ab2
a function stack while Ab1_Code
is the class definition and Ab2_Code
is the function definition code.
And it is because of these differences imposed by the PL, you find that they are so different.(your question)
Note: Don't take my representation of Ab1
/Object
as a long storage abstraction as a rule or a concrete thing - it was from layman perspective. The programming language provides much more flexibility in terms of managing lifecycle of an object. So, object may be deployed like Ab1
but it can be much more.
And does this have any relation with the functors (function objects)?
Note that the first part answer is valid for many programming languages in general(including C++), this part has to do specifically with C++ (whose spec you quoted). So you have pointer to a function, you can have a pointer to an object too. Its just another programming construct that C++ defines. Notice that this is about having a pointer to the Ab1
, Ab2
to manipulate them rather than having another distinct abstraction to act upon.
You can read about its definition, usage here:
C++ Functors - and their uses