* Copying a class copies the member functions?
@ 2007-04-08 15:20 Shriramana Sharma
2007-04-13 17:21 ` Glynn Clements
0 siblings, 1 reply; 2+ messages in thread
From: Shriramana Sharma @ 2007-04-08 15:20 UTC (permalink / raw)
To: linux-c-programming
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.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Copying a class copies the member functions?
2007-04-08 15:20 Copying a class copies the member functions? Shriramana Sharma
@ 2007-04-13 17:21 ` Glynn Clements
0 siblings, 0 replies; 2+ messages in thread
From: Glynn Clements @ 2007-04-13 17:21 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: linux-c-programming
Shriramana Sharma wrote:
> 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?
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.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-04-13 17:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-08 15:20 Copying a class copies the member functions? Shriramana Sharma
2007-04-13 17:21 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).