Timeline for How do I implement Dependency Inversion in C?
Current License: CC BY-SA 4.0
11 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 8, 2022 at 12:53 | comment | added | amon | @AndyLin This technique doesn't require that you use headers. But you can put the interface into its own header. The interface header would be included by the “dependency” and “context” files, which could be separate compilation units. The main (composition) part will have to combine all parts, though. | |
Jan 8, 2022 at 12:06 | comment | added | Andy Lin | @amon Can you supplement which part goes to header file and which part goes to source file in third code section? Also, can you point out which header file(s) should be included in main.c? | |
May 25, 2020 at 17:06 | comment | added | MrBit | Thank you for your help guys. I'm new to OOP and I'm struggling to understand those concepts. | |
May 25, 2020 at 17:04 | vote | accept | MrBit | ||
May 25, 2020 at 16:55 | comment | added | amon | Thanks @Erik, I fixed that mistake. Yes you're right that there are different ways to do vtables. I tried to illustrate that data + vtable belong together, although in practice you'd either embed the full vtable or a vtable pointer in the object (like a typical C++ or Java implementation), or detach the vtable from the object and store it alongside the data pointer (as in typical Go, Rust, Haskell implementations). In C, it seems somewhat common to abstract only over behaviour but not data layout: using a vtable, but no void pointer to the data. | |
May 25, 2020 at 16:46 | history | edited | amon | CC BY-SA 4.0 |
fixed minor bugs
|
May 25, 2020 at 16:18 | comment | added | Erik Eidt |
FWIW, I don't see this as a "classic vtable", as each interface object carries an entire vtable copy along with a pointer to the data. I would expect the other way around: the data to carry a pointer a vtable (that could be shared by all members of the same concrete class), mimicking class Dependency implements Interface
|
|
May 25, 2020 at 16:11 | comment | added | Erik Eidt |
I think you want return ((Dependency*)interface->data)->concreteStuff; in getConcreteStuff .
|
|
May 25, 2020 at 13:41 | comment | added | amon | @MrBit Header files primarily offer modularization/encapsulation. You can declare functions and structs in the header, but define them in a separate compilation unit. In particular, static functions are private. That's a weak kind of dependency inversion where all compilation units depend on declarations in the headers. But dependency inversion is more concerned with how dependencies are fulfilled/linked/injected. In the OOP examples there's some piece of code that selects concrete dependencies combines the objects. In the preprocessor variants, this decision is moved to compiler flags. | |
May 25, 2020 at 12:44 | comment | added | MrBit | Thanks for the answer. I just read here stackoverflow.com/a/26205828/3829694 that header files can be used as interfaces in C. What's the difference between that and your answer? | |
May 25, 2020 at 11:47 | history | answered | amon | CC BY-SA 4.0 |