C++ abuse: simple example
This example demonstrates the dangerous consequences of low-level memory manipulation in C++, where swapping an object’s vtable pointer with another object’s changes its runtime behavior. The code uses raw `void*` casts and pointer arithmetic in the `transmigrate` and `brain_damage` functions to overwrite the virtual function table entries of a `Dog` object with those of a `Cat`, causing the program to call the wrong virtual methods. The output sequence—showing `Dog::food()` initially, then `Cat::food()` after the swap, and finally undefined behavior after the vtable corruption—illustrates how such manual pointer juggling bypasses type safety and virtual dispatch. The example relies solely on standard C++ features like virtual functions, inheritance, and explicit pointer casts, with no external libraries.
AI-generated description