U
usenetfunny
Toward the end of the page:
http://home.comcast.net/~fbui/OOC.html
http://home.comcast.net/~fbui/OOC.html
Toward the end of the page:
http://home.comcast.net/~fbui/OOC.html
Toward the end of the page:
http://home.comcast.net/~fbui/OOC.html
Chris M. Thomasson said:implementation file:
/* usb_drive.c */
#include "usb_drive.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
struct usb_drive {
struct device device;
/* whatever */
};
static int object_destroy(void*);
static int device_read(void*, void*, size_t);
static int device_write(void*, void const*, size_t);
static struct device_vtable const g_vtable[
{ /* object interface */
object_destroy
},
{ /* device interface */
device_read,
device_write
}
];
int usb_drive_create(
struct device** pself
) {
struct usb_drive* const self = malloc(sizeof(*self));
if (self) {
*pself = &self->device;
return 0;
}
return ENOMEM;
}
int object_destroy(void* const self_) {
struct usb_drive* const self = self_;
free(self);
printf("destroyed a usb_drive(%p)\n", self_);
return 0;
}
int device_read(void* const self_, void* buf, size_t size) {
struct usb_drive* const self = self_;
printf("read from a usb_drive(%p, %p, %lu)\n",
self_, buf, (unsigned long) size);
return 0;
}
int device_read(void* const self_, void* buf, size_t size) {
struct usb_drive* const self = self_;
printf("wrote to a usb_drive(%p, %p, %lu)\n",
self_, buf, (unsigned long) size);
return 0;
}
[...]
Chris M. Thomasson said:Toward the end of the page:
http://home.comcast.net/~fbui/OOC.html
Why are you actually putting interface function-pointers within the struct
itself? IMVHO, this can be a huge waste of space. A MUCH more space
efficient approach is to include a single pointer to a constant vtable.
Something like this, modulo bugs because I am quickly typing it directly
in the newsreader:
[...]
blargg said:C++ used a scheme like this before it had templates:
http://h30097.www3.hp.com/cplus/6026pro_genr.html
http://www.softwarepreservation.org...urce/incl-master/const-headers/generic.h/view
BTW, please use the message subject to summarize the body, not as the
first few words of the message body; the body should stand on its own.
Toward the end of the page:http://home.comcast.net/~fbui/OOC.html
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.