![]() ![]() It's not type safe, since type safety isn't a primary concern of this pattern.The code behind linux, cpython implementation, and many other large scale C projects implement OOP through vtables - a pattern that is pretty common amongst the C community.Īt the same time, if you've used this pattern - you may also know of its deficiencies. Thankfully, polymorphism in C is nothing new - there are many articles, repositories, and projects in general explaining, and using a Virtual Method Table (aka vtable) based OOP polymorphism pattern. Chances are- you're somewhat frustrated at the lack of native polymorphism support. ![]() Maybe you want to have generic containers (i.e a linked list of generic elements), or maybe you want to have a function that takes in a polymorphic type, or anything in between. If you've written a fair amount of C, chances are- you've been struck by the desire to have some sort of polymorphism like the higher level languages do. You can view an implementation and usage of this pattern in c-iterators, where I implement lazy, type safe, rust-like iterators. This article describes an extensible and practical design pattern you can use to achieve functional polymorphism in pure, standard C99 (or above). Note: An extended and updated version of this demonstration can be found at typeclass-interface-pattern. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |