Copying a class copies the member functions?
Apropos my recent questions regarding passing by reference etc the following question comes to mind:
When a class instance is created, does this also create a copy in memory (from the class prototype) of the member functions or only of the data members?
Regarding static member functions I realize there is no meaning to have an instance of the member function for each instance since the function does not relate to an instance of the class.
But even regarding non-static functions, I don't see any meaning in having an instance of the function for each member of the class, since all such functions would do basically the same thing, though with their parent instance only.
So what's the fact here? Is there any need for new copies of member functions with each new instance of a class being created? *Are* such new copies created by good compilers like GCC?
Thanks again for all your good and patient responses.
Shriramana Sharma.
Quoted 11 lines · Shriramana Sharma
Apropos my recent questions regarding passing by reference etc the following question comes to mind:
When a class instance is created, does this also create a copy in memory (from the class prototype) of the member functions or only of the data members?
Regarding static member functions I realize there is no meaning to have an instance of the member function for each instance since the function does not relate to an instance of the class.
Quoted 8 lines
But even regarding non-static functions, I don't see any meaning in having an instance of the function for each member of the class, since all such functions would do basically the same thing, though with their parent instance only.
So what's the fact here? Is there any need for new copies of member functions with each new instance of a class being created? *Are* such new copies created by good compilers like GCC?
Virtual methods are normally implemented via a table of function pointers which is shared by all instances of a given class.
So a C++ virtual method call such as e.g. obj.foo(arg) results in code equivalent to the C expression (*obj.vtable->foo)(&obj, arg).
Non-virtual methods are effectively just like C functions; the actual function is determined at compile time.
Signature · 1 line
Glynn Clements <[email protected]>