Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've seen OOP inheritance in C before. It involves a table of pointers to functions, and a derived "class" would just point to a different table.

It works, but all the machinery has to be handled manually. It's very, very easy to make a mistake. The question is, why do this? Just use C++ as "C with classes" and you'll be much better off.



The goal of this is to understand how OOP works under the hood, so you can adapt it to your own use case. You can do more strict OOP than C++ if you wanted to. However in most cases, all you need is basic OOP, namely writing your code with clean separation of functionality in different files with `static` keyword being similar to what `private` means in C++, and then using structs with typedef to define data. Function pointers and additional macros just add syntactic sugar, and are not necessary.

The main advantage of C++ isn't really OOP, its all the standard library features and smart pointers, but those are not always applicable (like when you want to manage memory as efficient as possible). So using C is often preferred.


the implementation in the article is basically what Golang is.

and the question: "so much manual/repetitive work, why do this"... is still valid.


Yup. Once you find yourself doing OOP in C, or doing metaprogramming with the C preprocessor, it's time to move to a more powerful language. You've outgrown C.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: