4 ms·
I've seen code that wrote directly to the vtable pointer (in c++). The code knew what it was doing was dangerous, but jeez. Always made me uncomfortable. FWIW
by yoklov 11y ago
I've seen code that wrote directly to the vtable pointer (in c++). The code knew what it was doing was dangerous, but jeez. Always made me uncomfortable.
FWIW I usually just use an enum. Easy to change on the fly, easy on the cache, easy to serialize, doesn't require indirection, ... Lots of benefits. Pisses off switch-phobic programmers, though.
- kazinator 11y agoChange class is simulated in C++ via the State Pattern and possibly others. Any pimpl variant can achieve an effective change class by allowing an object's internal representation to be mutated to a new representation. I once made a list/binary search tree library called Austin, purely in C. This library lets you dynamically change the type of an object from one kind of binary search tree to another. For instance from Red-Black to AVL or Splay. Or to a sorted linked list. There is a "vtable" in there which gets swizzled to point to different operations. During the swizzling, there are special operations which handle the needed reorganizations of the tree.