Re: Copying a class copies the member functions?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxx>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux