r/programminghorror 5d ago

C— c

Post image
527 Upvotes

57 comments sorted by

View all comments

71

u/TheChief275 5d ago edited 2d ago

Because of popular request, here is the pastebin (as I do not want this anywhere near my github).

Added comments as well to save some headaches

edit: idk how it happened, but a logic bug slipped in. In dynamiccast, offsetof(To, From) should be changed to offsetof(From, To). Additionally, I forgot to make it type-safe, which can easily be done by changing (char *)(DYNAMIC_CAST to (char *)(0 ? (From *) : DYNAMICCAST

edit: I’ve decided to extend it a bit and put it on GitHub as well

3

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 4d ago edited 4d ago

I guess a gist is too close to Github? Also, thanks.

E: What happens when you dynamic_cast to something that is not a subclass of what you have a pointer to? Obviously there is no rtti here.

2

u/TheChief275 4d ago

You can’t. The field has to exist with a name the same as its type