4 ms·
Note that this is using interfaces (i.e. vtables, records of function pointers), not full object-orientation. Other OO features, like classes and inheritance, h
by munchler 1y ago
Note that this is using interfaces (i.e. vtables, records of function pointers), not full object-orientation. Other OO features, like classes and inheritance, have much more baggage, and are often not worth the associated pain.
- PhilipRoman 1y agoField inheritance is surprisingly natural in C, where a struct can be cast to it's first member.
- 1718627440 1y agoNote that you only need to cast for an upcast. To access the first member, you wouldn't need to cast. It would be nice though, if syntax like the following would be supported: struct A { int a; }; struct B { int b; struct A a; }; void foo (struct A * a) { struct B * b; &b->a = pa; } struct B b; foo (&b.a);
- PhilipRoman 1y agoYeah you're right, I meant the other way around. Also another loosely related idea is the container_of macro in Linux kernel.
- 1718627440 1y agoYeah, my idea is literally native type-safe support of container_of for assignment in the compiler.
- teo_zero 1y agoIn what scenario would this be useful? If foo() takes a struct A, it should be more generic and have no knowledge about the more specialized struct B.
- 1718627440 1y agoIn exact that same scenario, that you would cast to a subclass in another language, it's about language support for what for example the kernel does with container_of. Of course casting to a subclass isn't guaranteed to succeed always, but for example when you have actually declared it as the subclass elsewhere it's fine without checking for isinstance.
- 1718627440 1y agoWhat do you think inheritance is, if not composition of vtables? What do you think classes are, if not a composition of a vtable and scoped variables?
- munchler 1y agoThose "scoped variables" are the difference. Mutable state adds a great deal of complexity.
- 1718627440 1y agoAnd the style presented in the article uses vtables with "scoped variables". How do you conclude it's "not full object-orientation"?
- ryao 1y agovtables contain function pointers to functions that take “this” pointers. The author mentions struct file_operations as an example of a vtable. struct file_operations contains a pointer to a function that does not take “this” pointer. It is not even a vtable.
- 1718627440 1y agoI would still call it a vtable. Who assures you that every function of a class needs a pointer to the object instance? When you don't need it, you can just leave it of when you roll your own.
- ryao 1y agoStatic member functions are tied to the class while virtual member functions are tied to the object. If you throw the static member functions into a vtable, you will at best have a bug where the static member function from a different class can be called. Alternatively, you would have an undefined behavior where the other class in the hierarchy does not implement this function. There is a saying “If Your Only Tool Is a Hammer Then Every Problem Looks Like a Nail”. That is precisely what is happening here with the insistence to call what appears to be all structures of function pointers vtables. A vtable is something that follows a fairly well defined pattern for implementing inheritance. Not all things containing function pointers are vtables.
- 1718627440 1y ago> Static member functions are tied to the class while virtual member functions are tied to the object. That's nice, but the entire point is, that the caller doesn't know the type of the object, it only has a supertype. That's why you need dynamic dispatch here. Of course you can implement dynamic dispatch without function pointers, but it is done with function pointers here. If you don't want to name dynamic dispatch implemented with function pointers a vtable, OK, that's fine, but that's the definition I am familiar with.
- 1y ago