5 ms·
A whole book on this topic was already written in 1993 by Axel-Tobias Schreiner. It seems to be freely available nowadays: https://www.cs.rit.edu/~ats/books/oo
by johndoe0815 4y ago
A whole book on this topic was already written in 1993 by Axel-Tobias Schreiner. It seems to be freely available nowadays:
https://www.cs.rit.edu/~ats/books/ooc.pdf https://www.cs.rit.edu/~ats/books/ooc.pdf
(edit: link updated to point to author's website)
- askvictor 4y agoI remember having a book on the shelf through the 90s entitled 'Object Oriented Programming in Macro Assembler'
- friendlyHornet 4y agoI started reading rhis last year in my free time, and following along coding myself. I only read a few chapters then got sidetracked, but man I learnt so much stuff from reading just a bit i already knew all the C concepts he was using but it never occurred to me to use them in that way. The book is very well-written and easy to understand, and also very approachable even for someone who is not so experienced with C, like myself I want to start over someday
- deleted 4y ago[deleted]
- linkdd 4y agoYears ago, I started an implementation of that book: https://github.com/linkdd/ooduck https://github.com/linkdd/ooduck Never truly finished it, but it works well :)
- pmontra 4y agoThat was how I was doing OOP in C in the late 80s and early 90s. I found many of those concepts both in Python and in JavaScript. After all they were born around that time. Python because of the insistence of declaring that explicit self argument, the pointer to the struct for the object. JavaScript because of the prototype based OO. You can change the meaning of any field in an OO C struct if you know how to handle it later.
- attractivechaos 4y agoThe webpage in the title is pretty good. It explains most things you need to achieve OOP in C. This PDF however pushes C for what it is not intended for, which leads to inefficient and obscure code. For example, the first example in the PDF implements a Set. Everything is "void". This loses typing and will make code hard to read and maintain. The way inheritance is mimicked is hideous and inefficient. The PDF also heavily uses function pointers, which can be slow (because they aren't easily inlined) and waste memory (because each pointer costs 8 bytes on x64). In all, most large C projects are doing OOP at some level. However, C is not C++ and is not intended for providing all OOP features like inheritance. This book should probably be titled "Wrong ways to OOP in C". PS: The book does have some interesting ideas, but those are the "clever" things you will have to unlearn later. The author provides the source code for the book [1]. Have a look at the string implementation in Chapter Two. That is the most arcane and inefficient string implementation I have seen. [1] https://www.cs.rit.edu/~ats/books/ooc-14.01.03.tar.gz https://www.cs.rit.edu/~ats/books/ooc-14.01.03.tar.gz
- sfpotter 4y agoThis book is a really fun read. You’ll learn a lot and get some interesting ideas. Don’t listen to prescriptions from strangers on HN, how about? Myself included. :-)
- quelsolaar 4y agoI have not read the book. > However, C is not C++ and is not intended for providing all OOP features like inheritance. There are parts of the ISO C standard specifically included to allow for inheritance. Consider: typedef struct{ int x; }struct_a; typedef struct{ struct_a inherited; int y; }struct_b; void function(struct_a *s) { s->x = 0; } Here struct_b inharates struct_a. You can call the function with either a pointer to struct_a or struct_b (either by casing or void pointer), because the standard specifically states that there can be no padding before the first member of a structure, so that the first member of a structure should have the same pointer as the structure itself. This part of the spec was written with the use case of inheritance in mind. (there is some people who don't read the spec that way, but this is the intention) Sorry for being nitpicky... but that's pretty much what we do in the wg14....
- tpoacher 4y agoI wasn't too keen on this book. I read it hoping it would be more like the article in this post. But I felt it was more a case of someone re-inventing c++ with macros.
- linkdd 4y agoYou mean "C with classes" ?
- KingLancelot 4y ago[dead]
- tpoacher 4y agonope, it's this book: https://www.goodreads.com/book/show/30040029-object-oriented-programming-with-ansi-c https://www.goodreads.com/book/show/30040029-object-oriented... i was basically reiterating my comment from there.