P
Paul
I also generally think of a function as a sequence of opcodes in memory(
function definition), however that is not the *only* way I can think of a
function.
I am also able to think of a function as a sub-process, a runtime entity.
Most of the people here who argue with me seem to lack this ability.
--That is perfectly valid to think about function invocations as part of
--process. However on one hand an object is not that process or part of
--it (so how can such invocation be part of object?) and on other hand
--same function may be invoked hundreds of times during program run from
--different parts of process (so function feels not a single thing but
--whole cloud of invocations by that view).
But an object is part of the calling process when we invoke a member
functions.
One way to demonstrate this is to consider the difference between the
following two functions:
obj.membfunction();
ordinaryfunction(p_obj);
An ordinary function can be recursive with each recursion invoked with a
different object parameter:
void ordinaryfunction(T* p_obj){
call overwrite(p_obj ); /*asm subroutine to overwrite the object with a
new one in exactly the same region of memory. Dunno if it can be done in C++
*/
ordinaryfunction(p_obj); /*a new object for each recursion*/
}
The same cannot be done with a member function because the function belongs
to the object:
void memberfunction(){
call overwrite();
this->memberfunction() /*'this' is not modified but it points to a
different object */
/*ptr_to_thisfunction() Using a pointer to the opcode makes no
difference*/
}
The rules of C++ disallow member functions(instances of functions) to be
invoked with a different object thus .. the instance of the function belongs
to the object on which it was invoked.
Assuming you agree with above it follows that:
The sequence of execution for a member function is not always the same as
that of an ordinary function, for no other reason that the fact that the
function(or function invokation, if you need this directive) is a member of
an object.
My explanation is based on thiscall convention as explained here :
"4.thiscall: This convention is mainly used in C++ member functions and
parameters are pushed from right to left. this pointer is stored in ECX
register instead of pushing into a stack. Callee will clean up the stack.
this pointer is passed in ECX register."
ref: http://www.yashks.com/2011/01/stack-internals/
Ty for reading
Paul.