TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

OOP in C

185 点作者 philonoist超过 2 年前

20 条评论

johndoe0815超过 2 年前
A whole book on this topic was already written in 1993 by Axel-Tobias Schreiner. It seems to be freely available nowadays:<p><a href="https:&#x2F;&#x2F;www.cs.rit.edu&#x2F;~ats&#x2F;books&#x2F;ooc.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.rit.edu&#x2F;~ats&#x2F;books&#x2F;ooc.pdf</a><p>(edit: link updated to point to author&#x27;s website)
评论 #34298138 未加载
评论 #34299232 未加载
评论 #34298231 未加载
评论 #34300131 未加载
评论 #34297444 未加载
评论 #34300714 未加载
antirez超过 2 年前
C required the simplest form of OOP, but who standardizes it decided to deny the feature to the language: the ability to have callable structure methods (function pointers) with explicit &quot;self&quot; pointer. Not even constructors&#x2F;destructors. After all it&#x27;s C, and you can write list-&gt;init() and list-&gt;free(). This simple form to bind data and the functions operating on such data would make many codebases better.
评论 #34298976 未加载
评论 #34298665 未加载
评论 #34298327 未加载
评论 #34301307 未加载
carapace超过 2 年前
Or you could implement COLA?<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;COLA_(software_architecture)" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;COLA_(software_architecture)</a><p>Huh, that page was deleted in Dec 22. &quot;concern was: Old research project. Unable to locate any details. VPRI institute is dead. Been on the cat:nn list since March 2009. No new updates.&quot;<p>Goddamned deletionist activist WP editors tearing down the human knowledge base.<p>(If you don&#x27;t know VPRI is (was?) Alan Kay&#x27;s research org. I think it&#x27;s a bit notable and important.)<p>ANYWAY<p>From the search result (in DDG) snippet I can get the first two sentences of the deleted page:<p>&gt; &quot;COLA&quot; stands for &quot;Combined Object Lambda Architecture&quot;. [1] A COLA is a self-describing language in two parts, an object system which is implemented in terms of objects, and a functional language to describe the computation to perform. [2]<p>It&#x27;s a very simple system that gives you the basis for both OOP and Lisp-like semantics. It&#x27;s fun!<p>You can see it here: <a href="https:&#x2F;&#x2F;piumarta.com&#x2F;software&#x2F;cola&#x2F;" rel="nofollow">https:&#x2F;&#x2F;piumarta.com&#x2F;software&#x2F;cola&#x2F;</a><p>Or check out the VPRI reports, etc.:<p><a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20220819075633&#x2F;https:&#x2F;&#x2F;www.vpri.org&#x2F;writings.php" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20220819075633&#x2F;https:&#x2F;&#x2F;www.vpri....</a><p>Ironically their website appears to be down at the moment.
posharma超过 2 年前
While I commend this effort I should say that seriously, just write C++ if possible. No matter what you do memory management is going to be your biggest enemy (I’m ignoring the aesthetic aspects like macros, function pointers, type safety, etc). Without RAII it’s just not worth it. Just choose the simpler portions of C++ and keep safe.
评论 #34302156 未加载
评论 #34302143 未加载
评论 #34302713 未加载
hgs3超过 2 年前
There is a lightweight object oriented extension to C called Objective-C [1] that unfortunately never gained much traction outside the NeXT&#x2F;Apple ecosystem. There is also Cello [2].<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Objective-C" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Objective-C</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;orangeduck&#x2F;Cello">https:&#x2F;&#x2F;github.com&#x2F;orangeduck&#x2F;Cello</a>
michaelcampbell超过 2 年前
I was surprised no one here mentioned cfront. My first job out of college I would look at the generated C code from my C++ source to see how it would convert some of the the C++ things into C, since I couldn&#x27;t figure out how to do it myself.
Congeec超过 2 年前
The famous htop actually employs OOP in C. <a href="https:&#x2F;&#x2F;github.com&#x2F;htop-dev&#x2F;htop&#x2F;blob&#x2F;650cf0f13bf667270d0a6a4612437af264667585&#x2F;AffinityPanel.c#L32-L57">https:&#x2F;&#x2F;github.com&#x2F;htop-dev&#x2F;htop&#x2F;blob&#x2F;650cf0f13bf667270d0a6a...</a>
评论 #34303526 未加载
ChrisMarshallNY超过 2 年前
I was doing OOP in C, back in the early ‘90s. Lotta work, but turned out great. Some of the software that I wrote back then, was still in use, 25 years later (a camera software SDK).<p>It had to be done a certain way, because stack conventions were all over the place, back then.
评论 #34302558 未加载
_0w8t超过 2 年前
I recently needed to write C code to interface with a COM library on Windows. While Microsoft fully supports it, the result is very foreign compared with typical C code.<p>Surely in C one does use tables of function pointers to implement some aspects of OOP. However, a good library limits it and rather exposes some structs instead of requiring multiple virtual methods calls to archive a similar effect.
评论 #34300970 未加载
bullen超过 2 年前
I think arrays of 64 byte structures is the most optimal way to architect your multicore &quot;on the same memory&quot; code.<p>Preferably using int&#x2F;float which should be atomic on X86 and hopefully on modern ARM too, don&#x27;t know about RISC-V yet.
anthomtb超过 2 年前
Strongswan, the open source IPSec VPN software (<a href="https:&#x2F;&#x2F;www.strongswan.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.strongswan.org&#x2F;</a>) is written in object oriented C. Ref: <a href="https:&#x2F;&#x2F;docs.strongswan.org&#x2F;docs&#x2F;5.9&#x2F;devs&#x2F;objectOrientedC.html" rel="nofollow">https:&#x2F;&#x2F;docs.strongswan.org&#x2F;docs&#x2F;5.9&#x2F;devs&#x2F;objectOrientedC.ht...</a><p>I have not looked through the source myself but thought this might be an interesting reference.
评论 #34299758 未加载
WalterBright超过 2 年前
I&#x27;ve seen OOP inheritance in C before. It involves a table of pointers to functions, and a derived &quot;class&quot; would just point to a different table.<p>It works, but all the machinery has to be handled manually. It&#x27;s very, very easy to make a mistake. The question is, why do this? Just use C++ as &quot;C with classes&quot; and you&#x27;ll be much better off.
评论 #34308952 未加载
评论 #34303373 未加载
redgrange超过 2 年前
I see so many uses of the term ADT that I can&#x27;t really give anyone a confident definition
评论 #34299258 未加载
评论 #34300271 未加载
zabzonk超过 2 年前
all that memory map stuff is completely out of whack, along with much else
评论 #34297743 未加载
znpy超过 2 年前
Beside the topic or the content, I love the typography choices of this.
kasajian超过 2 年前
Similar to: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=34297450" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=34297450</a>
ryanianian超过 2 年前
TLDR: Put data members in a struct. Write a function per &quot;method&quot; and pass in the `this` pointer. Simple and effective.<p>BUT: inheritance. C++ has subclasses (`class Manager : Employee`) and virtuals &#x2F; vtable lookup on pointer access (`employee-&gt;name()` which calls `Manager::name` or `Employee::name` based on type).<p>What is the typical equivalent in C? Hand-rolled vtables? Or is there a general paradigm that helps to avoid the need for it in C?<p>I ask as a seasoned C++ and Java developer looking to improve some C code which uses this pattern in the extreme and needs some refactoring. My intuition is composition rather than inheritance, although isA is more intuitive than hasA for this code-base.
评论 #34301602 未加载
评论 #34300683 未加载
bni超过 2 年前
ADTs was a major part of the C course I took in 1998
评论 #34298132 未加载
mpardalos超过 2 年前
Unrelated to the content, but this is basically a perfect webpage.
dboreham超过 2 年前
Ok, but this is how pretty much all large programs were designed in <i>assembler</i>. C has the data structures commonly used in assembler. C++ (originally) removes some boilerplate and adds some comple-time checking.
评论 #34300429 未加载