Advertisement
Guest User

class example

a guest
Feb 22nd, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4.  
  5. #define CLASS_METHOD                         \
  6.   register unsigned long rbp __asm__("rbp"); \
  7.   c_class *this = rbp + 16; // cfi_def_cfa_offset 16
  8.  
  9. typedef struct c_class
  10. {
  11.   int (*add)(void);
  12.   int (*sub)(void);
  13.  
  14.   int a;
  15.   int b;
  16.  
  17. } c_class;
  18.  
  19. int c_class_add(void)
  20. {
  21.   CLASS_METHOD
  22.  
  23.   return this->a + this->b;
  24. }
  25.  
  26. int c_class_sub(void)
  27. {
  28.   CLASS_METHOD
  29.  
  30.   return this->a - this->b;
  31. }
  32.  
  33. int main(void)
  34. {
  35.   c_class c =
  36.   {
  37.     .add = c_class_add,
  38.     .sub = c_class_sub,
  39.     .a   = 300,
  40.     .b   = 200
  41.   };
  42.  
  43.   printf("(expected 500) c.add() = %d\n", c.add());
  44.   printf("(expected 100) c.sub() = %d\n", c.sub());
  45.  
  46.   return EXIT_SUCCESS;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement