TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Object-oriented techniques in C

113 pointsby sea6earover 9 years ago

15 comments

openasocketover 9 years ago
This seems to just seems to be replicating the functionality of C++ in C. The only reason he gives for not using C++ is lack of C++ compilers for embedded CPUs. I think a better approach for the long term would be to use a C++ -> C transpiler (as much as I hate that word).
评论 #10260961 未加载
评论 #10263193 未加载
评论 #10263461 未加载
kbensonover 9 years ago
Isn't there some way to emit C from C++? Wouldn't a capability like that and using C as a host language completely bypass the need for emulating OOP in C, by just letting you write C++? That seems like it would be far more manageable, and since it's compiled, not susceptible to some of the major downsides commonly associated with the technique (in JS).
评论 #10262959 未加载
评论 #10261601 未加载
nqzeroover 9 years ago
this approach is similar to the one that GObject uses, though stripped down a bit for embedded. i haven&#x27;t used it in years, but it worked great with GTK+. the only downside i remember is the overhead of the virtual function calls (in contrast, the jvm is able to factor out the virtual call in many cases at runtime)<p>one advantage of using something like this is it forces you to really think about what makes something OO and gives you a deeper appreciation of it. i&#x27;m a java programmer now, but to this day i still prefer object oriented C to C++
rbalsdonover 9 years ago
I&#x27;ve found that, in the embedded world at least, the overhead this adds (in terms of flash&#x2F;rom and ram) can be fatal. That said, there are some more implementations of this here if anyone is curious: <a href="https:&#x2F;&#x2F;github.com&#x2F;ryanbalsdon&#x2F;libr" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ryanbalsdon&#x2F;libr</a>
评论 #10263594 未加载
bluedinoover 9 years ago
EFnet&#x27;s infamous Zhivago (from #c) has a great article about OOP in C - <a href="http:&#x2F;&#x2F;www6.uniovi.es&#x2F;cscene&#x2F;CS1&#x2F;CS1-02.html" rel="nofollow">http:&#x2F;&#x2F;www6.uniovi.es&#x2F;cscene&#x2F;CS1&#x2F;CS1-02.html</a>
MichaelGGover 9 years ago
How badly does this impact optimizations? First you add an extra function pointer for any member, even if it&#x27;s not virtual. Then since you&#x27;re always calling via a function pointer -- do compilers notice when fps are assigned and never modified and do inlining and so on?<p>Also the first technique doesn&#x27;t feel like OO in any meaningful way. Defining a state object and passing it to functions is exactly what you&#x27;d do in say, a functional programming language, or even in regular old imperative code.
评论 #10261682 未加载
评论 #10262138 未加载
评论 #10263502 未加载
kasajianover 9 years ago
Also check out: Simply Object-Oriented C --&gt; <a href="http:&#x2F;&#x2F;www.codeproject.com&#x2F;Articles&#x2F;22139&#x2F;Simply-Object-Oriented-C" rel="nofollow">http:&#x2F;&#x2F;www.codeproject.com&#x2F;Articles&#x2F;22139&#x2F;Simply-Object-Orie...</a>
halayliover 9 years ago
Unmaintainable and extra complexity just to emulate a paradigm that C wasn&#x27;t designed for.
评论 #10260994 未加载
vaskebjornover 9 years ago
&quot;The danger in trying to force object-oriented concepts onto a C base is to get an inconsistent construction, impairing the software development process and the quality of the resulting products. A hybrid approach yields hybrid quality. This is why serious reservations may be voiced about the object-oriented extensions of C described in chapter 20. To benefit from object-oriented techniques, one must use a consistent framework, and overcome the limitations of languages such as Fortran or C which regardless of their other characteristics -- were designed for entirely different purposes.&quot; Bertrand Meyer, Object-oriented Software Construction, 1988.
giagabover 9 years ago
This interesting article reminded me of this post by Rob Pike from some time ago: <a href="https:&#x2F;&#x2F;plus.google.com&#x2F;+RobPikeTheHuman&#x2F;posts&#x2F;hoJdanihKwb" rel="nofollow">https:&#x2F;&#x2F;plus.google.com&#x2F;+RobPikeTheHuman&#x2F;posts&#x2F;hoJdanihKwb</a>.<p>While I&#x27;ve been a C++ programmer since my uni days, I think algorithms and data structures should come first, and I don&#x27;t like the idea of &quot;bending&quot; programs to make them fit nicely within the OO paradigm.
TickleSteveover 9 years ago
Do <i>NOT</i> use these techniques please!<p>I have experience of an early nineties project that was entirely structured like this and trust me, it ends up as a disaster.<p>C is not meant to be OO, consequently all the tooling in editors and the like do not understand the links between classes and methods.<p>What it ends up like is an impenetrable mess with all the mechanics of C++ exposed but being impossible to navigate.<p>again... please do <i>not</i> do this in any project... for the sake of any who will follow you in maintaining your code!
评论 #10261375 未加载
评论 #10260881 未加载
评论 #10261037 未加载
评论 #10261119 未加载
评论 #10261215 未加载
评论 #10261548 未加载
评论 #10260996 未加载
评论 #10262368 未加载
评论 #10260868 未加载
pmalyninover 9 years ago
A part a of series of articles on HN: How to %feature_from_c++% in C (when you could have used C++)
评论 #10261554 未加载
swahover 9 years ago
I don&#x27;t feel like the crc32 example is a good one. Maybe the game entities example would have been better...
评论 #10263642 未加载
blitiover 9 years ago
This is probably a stupid question, but here it goes:<p>Embedded platforms tend to have size limits. Would this increase the size of the source code? Seems a bit verbose to me.
评论 #10260936 未加载
评论 #10261513 未加载
mpweiherover 9 years ago
Hmm...those who don&#x27;t understand Objective-C are doomed to reinvent it, badly? (Yes, have used OO C techniques of different sorts and implemented ObjC compiler&#x2F;runtime).
评论 #10261464 未加载