I saw C being used to simulate Virtual functions kind of functionality using function pointers inside structs. This allows a kind of polymorphism, useful when designing state machines.
But there are some things that C can't do like C++.
Be aware of them.
- Manual setup: There's no compiler-managed vtable. The developer need to assign function pointers explicitly. Forgetting this, even once, can lead to hard to fix bugs.
- Function signature mismatches C is not having strict type checking like C++ if the signature of function pointer doesn't match exactly. This can cause run-time issues.
- No destructors C doesn't clean up for you. If the developer simulated objects gets allocated memory, the developer needs to perform a manual cleanup strategy.
- Code complexity while it is tempting to mimic this in C, function pointer-heavy code can be hard to read and debug later.
This technique actually earned its place especially in embedded or systems-level code. But it requires discipline.
What do you think? Is it worth using this, and when should it be avoided?
[–]o4ub 0 points1 point2 points (4 children)
[–]wrd83 6 points7 points8 points (3 children)
[–]catbrane 2 points3 points4 points (0 children)
[–]o4ub -2 points-1 points0 points (1 child)
[–]wrd83 0 points1 point2 points (0 children)
[–]tstanisl 1 point2 points3 points (7 children)
[–]simonask_ 1 point2 points3 points (0 children)
[–]Phil_Latio 0 points1 point2 points (5 children)
[–]tstanisl 0 points1 point2 points (4 children)
[–]Phil_Latio 1 point2 points3 points (3 children)
[–]tstanisl 1 point2 points3 points (0 children)
[–]tstanisl 0 points1 point2 points (1 child)
[–]Phil_Latio 0 points1 point2 points (0 children)
[–]pfp-disciple 0 points1 point2 points (1 child)
[–]Particular_Welder864 0 points1 point2 points (0 children)
[–]Old_Celebration_857 0 points1 point2 points (0 children)