Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

override my anus

Name: Anonymous 2011-10-28 11:28

How do I override functions used in structs in c?

Function pointers?

Name: Anonymous 2011-10-28 11:34

you don't have a fucking clue what you're doing

Name: Anonymous 2011-10-28 11:37

Teach me your ways >>2

Name: FrozenVoid 2011-10-28 12:02

"Functions used in structs" have addresses.
You can call the original or call something else.
Overwriting function pointers is useless for performance, const function pointer tables with separate storage for all pointers should be used.

Name: Anonymous 2011-10-28 13:58

>>4
Can you give me an example of this please and thanks

Name: Anonymous 2011-10-28 14:01

what are you trying yo hack>?

Name: Anonymous 2011-10-28 14:02

and what system?

Name: Anonymous 2011-10-28 14:05

>>6
Trying to port java code that involves alot of inheritance and function overriding to c

Name: Anonymous 2011-10-28 14:10

hmm its 2011 by now your favourite ide
should have a lang conversion by now

Name: Anonymous 2011-10-28 14:13

Emacs doesn't do it.. >>9

Name: Anonymous 2011-10-28 14:18

>>8
http://en.wikipedia.org/wiki/Virtual_function
C++ may be easier, but C++ can be translated to C anyway.

Name: Anonymous 2011-10-28 14:21

>>11
Sepples is shit

Name: Anonymous 2011-10-28 18:09

>>4
And what happens if this struct is loaded up into some CPU register you stupud fucker? I thought so. Go fuck another tree you illiterate nigger.

Name: Anonymous 2011-10-28 21:05

>>5

void (*anusify[])(void) = {hard, soft, smooth, …, rough};

void perfromanus(const int anus_index)
{
    /* Call the function specified by jump_index */
    anusify[anus_index]();
}



this is how function overriding is performed in higher languages, also you can just make it a switch statement

Name: Anonymous 2011-10-29 0:46

here's a way kinda like seeples.




Interface EntityAble {
  int getHealth();
  void recieveDamage(int damage);
  void attack(EntityAble target);
};


struct entity_interface {
  int (*get_health)(struct entity_interface** self);
  void (*recieve_damage)(struct entity_interface** self, int damage);
  void (*attack)(struct entity_interface** self, entity_interface* target);
  void (*rest)(struct entity_interface** self);
};

struct entity_base {
  struct entity_interface* v_table;
  int health;
};

int entity_base_get_health(struct entity_interface** self) {
  return ((struct entity_base_class*)self)->health;
}

void entity_base_recieve_damage(struct entity_interface** self, int damage) {
  ((struct entity_base_class*)self)->health -= damage;
}

struct wizard {
  struct entity_base;
  int mana;
  double magic_damage_multiplyer;
};

int wizard_calculate_amount_of_mana_to_use(struct wizard* self, struct entity_interface** target) {
  int other_health = target->get_health(target);
  int ideal_amount_of_mana = (int)((double)other_health/self->magic_damage_multiplyer);
  int maximum_amount_of_mana_to_use = self->mana/2;
  int mana_to_use = ideal_amount_of_mana > maximum_amount_of_mana ? maximum_amount_of_mana : ideal_amount_of_mana;
  return mana_to_use;
}

void wizard_attack(struct entity_interface** self, struct entity_interface** target) {
  struct wizard* self_wizard = (struct wizard*)self;
  int mana_to_use = wizard_calculate_amount_of_mana_to_use(self, target);
  int damage_to_deal = (int)((double)mana_to_use*self_wizard->magic_damage_multiplyer);
  self_wizard->mana -= mana_to_use;
  target->recieve_damage(target, damage_to_deal);
}

void wizard_rest(struct entity_interface** self) {
  struct wizard* self_wizard = (struct wizard*)self;
  self_wizard->entity_base.health++;
  self_wizard->mana += 7;
}

struct wolf_class {
  struct entity_interface super;
  int bite_cost;  // bite_cost > scratch_cost
  int scratch_cost;
};

struct wolf {
  struct entity_base_class entity_base_class;
  int stamina;
  int bite_damage;
  int scratch_damage;
  double stamina_damage_absorbtion_factor;
};

void wolf_recieve_damage(struct entity_interface** self, int damage) {
  struct wolf* self_wolf = (struct wolf*)self;
  int stamina_reduction = (int)((double)damage*stamina_damage_absorbtion_factor);
  int health_reduction = damage - stamina_reduction;
  self_wolf->stamina -= stamina_reduction;
  entity_base_recieve_damage(self, health_reduction);
}

void wolf_attack(struct entity_interface** self, struct entity_interface** other) {
  struct wolf* self_wolf = (struct wolf*)self;
  int attack_damage;
  int attack_cost;
  int other_health = (*other)->get_health(other);
  if(self_wolf->stamina < (struct wolf_class*)(self_wolf->entity_base.v_table)->scratch_cost) {
    // You don't have enough stamina to do an attack.
    return;
  }
  if(other_health < self_wolf->scratch_damage ||
     self_wolf->stamina < (struct wolf_class*)(self_wolf->entity_base.v_table)->bite_cost  ||
     (random() % 6 == 0)) {
    attack_damage = self->wolf->scratch_damage;
    attack_cost = (struct wolf_class*)(self->entity_base.v_table)->scratch_cost;
  } else {
    attack_damage = self->wolf->bite_damage;
    attack_cost = (struct wolf_class*)(self->entity_base.v_table)->bite_cost;
  }
  self_wolf->stamina -= attack_cost;
  (*other)->recieve_damage(other, attack_damage);
}

void wolf_rest(struct entity_interface** self) {
  struct wolf* self_wolf = (struct wolf*)self;
  self_wolf->entity_base.health += 4;
  self_wolf->stamina += 11;
}

static struct entity_interface wizard_class = {
  entity_base_get_health,
  entity_base_recieve_damage,
  wizard_attack,
  wizard_rest
};

static struct wolf_class wolf_class {
  {entity_base_get_health,
   wolf_recieve_damage,
   wolf_attack},
  10, // scratch cost
  20  // bite cost
};

void wizard_init(struct wizard* self, int health, int mana, double magic_damage_multiplyer) {
  self->entity_base.v_table = &wizard_class;
  self->entity_base.health = health;
  self->mana = mana;
  self->magic_damage_multiplyer = magic_damage_multiplyer;
}

void wolf_init(struct wolf* self, int health, int stamina, int scratch_damage, int bite_damage) {
  self->entity_base.v_table = (struct entity_interface*)&wolf_class;
  self->entity_base.health = health;
  self->stamina = stamina;
  self->scratch_damage = scratch_damage;
  self->bite_damage = bite_damage;
}

// A generic function taking an argument that implements
// the interface..
int is_alive(struct entity_interface** self) {
  return (*self)->get_health(self) > 0;
}

void main() {
  struct wizard wizard;
  struct wolf wolf;
  wizard_init(&wizard, 100, 50, .7);
  wolf_init(&wolf, 100, 200, 10, 18);
  for(;;) {
    if(!is_alive((struct entity_interface**)&wizard)) {
      printf("the wizard died.\n");
      break;
    }
//    wizard_attack(&wizard, &wolf); // This call would avoid the v table look up overhead.
    (*((struct entity_interface**)&wizard))->attack(&wizard, &wolf); // Using the v table.
    if(!is_alive((struct entity_interface**)&wolf)) {
      printf("the wolf died.\n");
      break;
    }
    (*((struct entity_interface**)&wolf))->attack((struct entity_interface**)&wolf, (struct entity_interface**)&wizard);
  }
}


If you have a compiler that is old and doesn't do type checking on pointers, like the enterprise borland compiler, then you can omit all of those casts, or perhaps make macros for them.

Name: Anonymous 2011-10-29 0:47

>>15

sorry about that bit of java at the beginning. Please ignore it...

Name: Anonymous 2011-10-29 0:58

>>15
good lord all that shit just to do function overriding

Name: Anonymous 2011-10-29 1:00

>>15
c++ is shit, use lisp

Name: Anonymous 2011-10-29 1:09

>>17
 There is also some java style inheritance.

Name: Anonymous 2011-10-29 4:24

>>15

why go through all this trouble when you can just use C++ classes?
(Everything else can stay in ANSI-C, but C++ classes do exactly what you did with much less verbosity)

Name: Anonymous 2011-10-29 5:14

>>20

for science.

Name: Anonymous 2011-10-29 7:05

for dubs

Name: Anonymous 2011-10-29 13:07

>>20
C is bloat

Name: FrozenVoid 2011-10-29 13:28

>>23
Some implementation of C. C itself is very compact(see Tiny C Compiler)

Name: Anonymous 2011-10-29 13:29

>>24
500 pages and still underspecified.

Name: Anonymous 2011-10-29 15:28

>>25

underspecified, with intention

Name: Anonymous 2011-10-29 15:31

sorry, I meant to say:

underspecified, with intention

Name: Anonymous 2011-10-29 15:50

>>26-27
What? Speak English, damn you

Name: Anonymous 2011-10-29 16:07

>>Anus over-ridden
Surprize buttsecks: begins

Name: Anonymous 2011-10-29 17:46

>>26-27
Does it really take 500+ pages to underspecify a language?

Name: Anonymous 2011-10-29 19:38

>>30

Ya.

But really though, C is like the lowest common denominator for different architectures. All implementations have to adhere to the spec to stay portable, and a stricter spec would make the implementation of the compiler difficult for certain, more obscure architectures. Well, it wouldn't be that difficult for the compiler, but if the hardware didn't produce results that adhered to the spec for things like signed integer overflow, then checks in compiled binary would need to be inserted to make sure that the overflow behaved according to teh spec, which would induce needless runtime cost on strange computers that use something other than two's compliment for signed numbers.

Name: Anonymous 2011-10-29 20:00

>>31
C is the lowest common denominator for different architectures designed to run C. C would likely have tons of problems running on certain, more obscure architectures.

Name: Anonymous 2011-10-29 22:54

>>32
Well it can be a bit of a pain on Harvard architectures, but then again, Harvard architecture are a bit of a pain in general.

Don't change these.
Name: Email:
Entire Thread Thread List