[linux-elitists] Nobody's favorite language? C++ and free software

Greg KH greg@kroah.com
Thu Mar 27 09:56:46 PST 2003

On Wed, Mar 26, 2003 at 10:15:07PM -0800, Nick Moffitt wrote:
> begin  Greg KH  quotation:
> > Except we are using nested structs a bit differently by not placing
> > them at the first element, but anywhere else.  Then those nested
> > structs are passed around, and subsystems cast backwards to the main
> > pointer of their "inherited" object.  Quite fun compiler abuse :)
> 	So why "anywhere else"?  I meant that the first element of
> struct foo is an instance of struct foo_parent, so that you can
> reasonably treat any struct foo as a foo OR a foo_parent (or
> foo_grandparent, if foo_parent followed the convention) and so forth. 

It was done this way so you can't treat the structure that way.  You
_must_ explicitly know the type of the object the structure is embedded
in, in order to use it.

Yes, this isn't as flexable, but it enforces a greater discipline on your
code, which in the kernel is a good thing.

For those curious, look at:
and then look at struct usb_device, struct pci_dev, or anyone else who
has a struct device embedded in them.  The to_usb_dev() macro is the
conversion from a struct device to a struct usb_device doing the
backwards pointer manipulation.

> 	The bonus is that this mechanism really *is* just plain C, and
> it's readily self-apparent to anyone who's used C for anything beyond
> a CS110 course.

I agree, that is a very nice way of implementing objects in c.

greg k-h

More information about the linux-elitists mailing list