LeelaSwamy
asked on
Objects
1) Do member functions occupy space in objects?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yes, and only if they are virtual.
The answer is not simple, because the question is not simple.
There is no consensus on objects implementation in C++, just common aspects and more or less differences between various compilers. However, we can consider that intimate details of relevance for C++ programmers are common to the most implementations, and the differences are important just for the restraint circles of compiler developers. (Compiler development is a quite definite specialty in programing).
The code of a member function appears just in a single place in a program, related to the class object (the object which is employed by the program as a template for creating instance objects of the class). Each instance object has access to the same piece of code, and the this pointer has some importance in this mechanism. It is suggested a very limited space expense for each instance object, regardless of the number of member functions.
For virtual member functions there is also no code duplication at all. There is a virtual methods table (VMT or vtable) within the class objects, and every instance object has just a special pointer (vpointer) to the virtual methods table.
Good luck !
There is no consensus on objects implementation in C++, just common aspects and more or less differences between various compilers. However, we can consider that intimate details of relevance for C++ programmers are common to the most implementations, and the differences are important just for the restraint circles of compiler developers. (Compiler development is a quite definite specialty in programing).
The code of a member function appears just in a single place in a program, related to the class object (the object which is employed by the program as a template for creating instance objects of the class). Each instance object has access to the same piece of code, and the this pointer has some importance in this mechanism. It is suggested a very limited space expense for each instance object, regardless of the number of member functions.
For virtual member functions there is also no code duplication at all. There is a virtual methods table (VMT or vtable) within the class objects, and every instance object has just a special pointer (vpointer) to the virtual methods table.
Good luck !
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: AlexFM {http:#9770363}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: AlexFM {http:#9770363}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
functions are part of the class not the variable. same thing with static members of the class except for one difference. static members can be accessed without actually creating an object but non-static functions have to be called using concrete objects.
so the answer is NO.