daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Feb 12th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4.  
  5.   typedef struct A A;
  6.   struct A {
  7.     /*some members*/
  8.   };
  9.  
  10. #ifdef __cplusplus
  11. }
  12. #endif
  13.    
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17.  
  18.   typedef struct B B;
  19.   struct B {
  20.     A parent; // <---- public inheritance
  21.     /*some members*/
  22.   };
  23.  
  24. #ifdef __cplusplus
  25. }
  26. #endif
  27.    
  28. A* static_cast_A(B* b) {
  29.   return &(b->parent);
  30. }
  31. B* static_cast_B(A* a) {
  32.   B* const b = 0;
  33.   unsigned const ptrdiff = (unsigned)((void*)(&(b->parent)));
  34.   return (B*)(((void*)a)-ptrdiff);
  35. }
  36.    
  37. #include <stddef.h>
  38.  
  39. B* static_cast_B(A* a) {
  40.   return (B*)((char*)a - offsetof(B, parent));
  41. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top