Re: struct data offsets



g3rc4n@xxxxxxxxx wrote:
i was playing around with the idea of getting some sort of virtual
function functionality out of c when playing about with pointers and i
was trying to do something like this

void say_hi();

struct foo{
unsigned offset;
int something;
double somethingelse;
void (*fun)();
};

struct bar{
unsigned function_offset;
void(*the_function)();

};

void init(struct foo* arg){
arg->offset = foo::fun;

foo::fun is a syntax error in C.

arg->fun = say_hi;
}

void init(struct bar* arg){

You cannot have two different functions with the same name in C.

arg->function_offset = bar::the_function;

bar::the_function is a syntax error in C.

arg->the_function = say_hi;

}
>
void fun(void* arg){
arg += (void*)(unsigned*)arg; // i know this doesn't work

If you know it doesn't work, at least try to explain what you're trying to do, since incrementing one void pointer by another void pointer is completely nonsensical.

((void(*)())arg)();

That makes my brain hurt. Function pointers are a very, very good reason to use typedefs.

}

int main(){
struct foo f;
init(&f);
fun(&f);
struct bar b;
init(&b);
fun(&b);
return 0;

}

so can you just read the offset of a structs data then mainpulate the
void* and do something with it?

What void*? The only (void *)s in your code is the argument to fun().



Here's what I think you're trying to do:

typedef void (*fptr)();

void foo_say_hi();
void bar_say_hi();

struct foo {
fptr fun;
};

void foo_init(struct foo *arg){
arg->fun = foo_say_hi; /* set base method in vtable */
}

struct foo {
fptr fun; /* copy all elements of parent struct at beginning */
int something; /* new elements in child struct */
double somethingelse;
};

void bar_init(struct bar *arg){
foo_init(arg); /* always chain to parent's constructor */
arg->fun = bar_say_hi; /* override base method in vtable */
}

int main(){
struct foo f;
foo_init(&f); /* call constructor */
f.fun(); /* calls foo_say_hi() */

struct bar b;
bar_init(&b); /* call constructor */
b.fun(); /* calls bar_say_hi() */

return 0;
}



--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Isaac Jaffe
.



Relevant Pages

  • Re: Function pointer to void function and int function
    ... struct foo f; ... ... If you call such a function through a `)' pointer, the compiler will not know it's supposed to generate the hidden `_value_ptr' argument nor the hidden `_returned_struct' object it should point to. ... that calls a function returning `int' and ignores the returned ... value is identical to the machinery that calls a `void' function. ...
    (comp.lang.c)
  • Re: void pointer arithmetic
    ... As for doing pointer arithmetic on the results: If all the pointers ... static int str_qcmp(const void *a, ... If casts are not no-ops, ... receives struct foo pointers, so it doesn't convert them from void* to ...
    (comp.lang.c)
  • Re: alignment/zero length arrays
    ... The lists contain a void *, ... struct foo *f; ... This sets pointer f to point to the data in your element. ...
    (comp.lang.c)
  • Re: Opaque pointers
    ... pete wrote: ... extern T List_list (void *x, ... In your header file, you'd have something along the lines of: ... The actual definition of 'struct foo' goes in the source file: ...
    (comp.lang.c)
  • Re: Need template work-around in VC6
    ... struct Int2Type ... void Baz(const Int2Type&) ... doesn't understand explicit template arguments on a function invocation. ...
    (microsoft.public.dotnet.languages.vc)