Live data from Hacker News

Object-oriented design patterns in C and kernel development

oshub.org

221–224 of 224 posts

Re: Object-oriented design patterns in C and kernel development

#221

Earlier quoted context omitted.

> while OP use void pointers OP doesn't use void pointers, he uses void. He writes about functions having no arguments and returning nothing for the same reason other blog posts name functions foo and bar. > OP uses this vtable as a form of indirection to implement runtime method swapping and polymorphism The kernel uses vtables to implement polymorphism, it doesn't store the vtable in the object to save space. If th…

> If there is no polymorphism, you don't use a vtable at all, that's saving even more space. Not true. You use a vtable even if there is no polymorphism, in cases where you want to have objects store pointers to their methods (OO interface), but don't want each instance to have pointers to all methods. I was referring to this article, which the OP links in his post https://lwn.net/Articles/444910/

When do objects need to have function pointers? If the function never changes per object, then you would just directly call the function. When do you use function pointers? When the caller only knows the type of struct foo_ops, but not the actual called function. That's polymorphism.

Re: Object-oriented design patterns in C and kernel development

#222

Earlier quoted context omitted.

"Orthogonal" vtables are essentially traits/typeclasses.

What I really like about C is that it supports these sophisticated concepts without having explicit support for them. It just naturally emerges from the core concepts. This is what makes it feel like it just doesn't restrict the programmer much. Maybe it's a bit due to its evolution. It started with a language that should have all features every possible, that was to complicated to be implemented at the time. Then it…

PS, as a can't edit it anymore: To those who don't know, I was talking about CPL -> BCPL -> B -> New B -> K&R C -> C99.

Re: Object-oriented design patterns in C and kernel development

#223

Earlier quoted context omitted.

> If there is no polymorphism, you don't use a vtable at all, that's saving even more space. Not true. You use a vtable even if there is no polymorphism, in cases where you want to have objects store pointers to their methods (OO interface), but don't want each instance to have pointers to all methods. I was referring to this article, which the OP links in his post https://lwn.net/Articles/444910/

When do objects need to have function pointers? If the function never changes per object, then you would just directly call the function. When do you use function pointers? When the caller only knows the type of struct foo_ops, but not the actual called function. That's polymorphism.

> When do objects need to have function pointers?

You can redefine the methods at runtime. If for example I have a logger that logs on the local drive and another one that logs on a networked drive I can swap the method pointer. This is not polymorphism since the object does have to change. I can also load the function from a shared library that is unknown during compilation and use that as part of the objects methods.

Re: Object-oriented design patterns in C and kernel development

#224

Earlier quoted context omitted.

When do objects need to have function pointers? If the function never changes per object, then you would just directly call the function. When do you use function pointers? When the caller only knows the type of struct foo_ops, but not the actual called function. That's polymorphism.

> When do objects need to have function pointers? You can redefine the methods at runtime. If for example I have a logger that logs on the local drive and another one that logs on a networked drive I can swap the method pointer. This is not polymorphism since the object does have to change. I can also load the function from a shared library that is unknown during compilation and use that as part of the objects method…

I would call that polymorphism, since the object type the caller sees, still stays the same, i.e. the vtable itself or some containing struct/class. So as far as the caller is concerned, the object he has access to behaves differently.
Post reply on HN