#include<cstdint>#include<cstdio>#include<new>/* * A function to take two pointers and: * * create a new pointer to union of their types * * assign it the value of 'foo' * * delete the original pointers * * reassign them to both point to the union*/template<typenameF,typenameB>boolmembind(F*&foo,B*&bar){if(sizeof(foo)!=sizeof(bar))returnfalse;Ftemp=(*foo);deletefoo;deletebar;unionuni_t{Ffoo;Bbar;};uni_t*result=newuni_t;foo=&(result->foo);bar=&(result->bar);new(foo)F(temp);returntrue;}/* * Creating a pointer to store a hex colour value * and a struct rgba value then binding them together*/intmain(){structPixel{uint8_ta;uint8_tb;uint8_tg;uint8_tr;};uint32_t*colour=newuint32_t(0xFF00FFFF);Pixel*pixel=newPixel{0};printf("[%p]: %0x\n[%p]: %d %d %d %d\n",colour,(*colour),pixel,pixel->r,pixel->g,pixel->b,pixel->a);printf("\nCalling membind..\n\n");if(membind(colour,pixel)){printf("[%p]: %0x\n[%p]: %d %d %d %d\n",colour,(*colour),pixel,pixel->r,pixel->g,pixel->b,pixel->a);}return0;}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.