OOP in Pure C

preview_player
Показать описание
Enable CC for Twitch Chat

Support:
- BTC: bc1qj820dmeazpeq5pjn89mlh9lhws7ghs9v34x9v9
Рекомендации по теме
Комментарии
Автор

I read the book clean Architecture. It has a great section near the beginning about programming paradigms where the author shows how you can implement all the OOP behavior in C. Very fascinating

demolazer
Автор

BREAKING NEWS: c programmer discovers dynamic dispatch in the lord's year 2024

miroaja
Автор

@30:36 I started learning C from reading the manual for LCC-win32 IDE as a PDF on an Palm PDA while on night shift. It even explained how to do win32 api stuff from scratch.

tripplefives
Автор

Very nice! Reminds me of an article on Quora on "Object-Oriented Programming in C: A Deep Dive" by Khanno Mikhail:
"...there are actually THREE ways to accomplish OOP in C (aptly named):

Open style.
Pointer style.
and GTK style (GObject style).

There are also three ways to apply function/method polymorphism in C as well:

Struct style.
Disjoint style.
and Interface style ..."

GaryChike
Автор

absolutely agree with your phrase "you can do anything in C"

juanmamani
Автор

A very interesting idea and quite elegant implementation! It's rather amazing that hot reloading can work so well in a typed and compiled language like C that doesn't have much features. I wish modern languages put some thought on making this a feature because it is so convenient for creating web servers, gui's and other highly interactive and visual applications.

PhthaloJohnson
Автор

Это замечательно! С удовольствием посмотрел на всю эту дичь. На удивление это крайне просто работает. Никогда не пытался имплементить что-то такое в своей жизни, максимум все заканчивалось указателями на функции, по причине ненадобности. Но какой же классный видос

zetroks
Автор

I've been waiting for this for a loooong time😭

Do_It_Stupidly
Автор

It only makes sense to define the Task struct's function members to accept Task itself so it looks similar to `self` in Python and besides you can call other task's functions from a Task's function

neshkeev
Автор

are the captions the live chat?? that's cool af

Jack_in_the
Автор

Tsoding, you don't actually need a dynamic array for vtable, or even register it at runtime, just store it in global library plain array variable and define it inplace. This way "dynamicness" is achieved through loading the lib itself, you just recompile the lib itself and vtable gets reloaded, then you just resolve it like you already do with function symbols. This way you can add vtable entries, but can't reorder or delete stuff from that just like in your implementation. I can provide an example via PR but unfortunately can't find public repo of panim, please consider making it public so people like me can f around with it)

asakhar
Автор

Wouldn't be a C video without a segfault 10 minutes in

hkejhkm
Автор

Looking at old codebases I've seen many cases of OOP in C:
- Sun Pixrect is a low level library for drawing on the framebuffer, and for each framebuffer there is a different implementation. At the creation of a Pixrect object, it is dispatched with the function table to manipulate it
- Still from Sun, X/NeWS has a graphics library called Shapes, that replaced the CScript (the PostScript graphic layer) + Pixrect combo, and it has plenty of macros to declare classes in C and dispatch all the methods. This package handles shapes, paths and framebuffers
- At CMU, a preprocessor called "Class" was made for the Andrew project. It enriched C with Object-Oriented specific syntax (similar but different from both C++ and Objective C). I guess this was used for the Andrew Toolkit and other applications of the suite

DVRC
Автор

For the tag implementation, you can make the tag an enum, as well as make the data a union of pointers to the types that inherit it, this allows the vtable to be off of the heap as you now know the size in compile time, as well as you can use designated initializers to have a more well defined offset of all of the pointers for your vtable.

shakkar
Автор

Where is the book, Zozin? Where is it? You'd never give it to an ordinary citizen!

john.darksoul
Автор

Honestly seeing classes and virtual methods implemented in C was really freaking cool

StevenMartinGuitar
Автор

Thank you! For inspiration and laughs! Proposal: maybe animals poop in various places. Abstract the toilet?

kaotiskhund
Автор

Why did i click this video. Obviously i'm not gonna watch a 2 hour video at 12:57pm, also i have to wake up at 7am for school.

SeasonalMike
Автор

Now your bottleneck is the compiler being slow lol

SaidMetiche-qyhb
Автор

The boilerplate in C for the animal example could have been a bit smaller if you would have stuck to what C++ does a bit closer. In C++ it would inline the dog and cat data into the same allocation as for animal, meaning Animal effectively becomes a "Animal_Methods**" with some extra data to the right of it. Dog and Cat would also need the vtable pointer in the struct definitions, but this could be mitigated via the use of the butterfly technique in the utility methods. Initialization code for the tags could also be simplified if you used the C++ approach.

bbq