Hacker News new | ask | show | jobs
by mst 1754 days ago
To a great extent, the difference between "a data structure plus a dispatch table" and "an object" is primarily in how you squint.

It's certainly a form of polymorphism.

1 comments

It's a measurable difference. OO in C++ must use double indirect vtable method calls, whilst this OO in C uses only one indirection. It's measurably faster. Objects are a bit larger though, cloning is a bit more expensive, but method calls are much faster and much easier to cache.
I think you're misinterpreting GP's code. It is a vtable, hence the static const. There would be a pointer in each struct "instance" to this static vtable, thus the same double indirection would occcur as in C++.
Plus this was a discussion about conceptual models and "what counts as OO", not the specific details and optimisation possibilities of the implementation.
OP mimic'd C++ vtables. But in C you can do better and inline them. I mostly inline them for performance reasons.