Hacker News new | ask | show | jobs
by userbinator 2622 days ago
With the C++, there is also an optimisation which can be performed: put the method pointers in the object itself, instead of in a vtable. This is beneficial when there aren't so many objects nor methods, since it eliminates one indirection. Instead of obj->table.method() it turns into obj->method(). When I write "OOP" code in C, I usually do this instead of using a separate vtable.

As an aside, I noticed the word "klass", then looked up at the domain name and confirmed my suspicions. ;-)

2 comments

"klass" is a common convention in OOP code when referring to classes, because "class" and "Class" are often reserved already.
Python typically goes with `cls`, particularly in conjunction with the `@classmethod` decorator.
Python code uses both.
Um, not really. PEP8 explicitly says to use `cls`:

https://www.python.org/dev/peps/pep-0008/#function-and-metho...

Java seems to prefer `clazz`.
"klass" seems to somewhat outnumber "clazz" in OpenJDK, 1541 to 1021. Wonder if there's a reason they haven't settled on one.

https://github.com/openjdk/jdk/search?q=klass

https://github.com/openjdk/jdk/search?q=clazz

C# prefers `@class`
Doing this prevents a large class of important compiler optimizations. Pitching a DIY vtable as an optimization seems to require a lot of hard evidence.