TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Neil Heller
from: Pascal Schmidt
date: 2004-04-18 13:51:16
subject: Function Pointers

Hi Neil! :-)

 NH> Does anybody still use function pointers?
I use them when I write some traversal function for a database where the
action that is to be taken for each db entry varies. I make the traversal
function take a pointer to a function that is called for each entry. This
approach nicely seperates the database traversal from processing of the
entries.

 NH> Is there anything that function pointers can do that other construct 
 NH> cannot do?
Simulating classes and objects in plain C. An abstract class is simply a
struct with embedded function pointers, which are then filled out in the
objects. Obviously there needs to be explicit code to fill out the pointers
for a new object, which would be generated by the compiler in a real
object-oriented language.

This technique is uses extensively in the Linux kernel. For example, each
filesystem has to provide a filled out "struct file_operations",
which contains pointers to the open(), write(), read(), etc. functions for
the filesystem. The same is true of character, block, and network devices.
This way, the generic routines in the kernel don't have to know what kind
of device they are working with, they just use the function pointer and
automagically end up using the right driver.

Subclassing works by inlining the old class' struct definition into the new
one. I don't think this is used in the Linux kernel, though.

Ciao
Pascal

--- Msged/LNX 6.1.1
* Origin: rot13: tr '[A-Za-z]' '[N-ZA-Mn-za-m]' (1:153/401.2)
SEEN-BY: 633/267 270
@PATH: 153/401 307 140/1 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.