<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Gobject on Siddharth Mishra</title><link>http://brightprogrammer.in/tags/gobject/</link><description>Recent content in Gobject on Siddharth Mishra</description><generator>Hugo</generator><language>en</language><lastBuildDate>Sun, 14 Jun 2026 21:53:10 -0700</lastBuildDate><atom:link href="http://brightprogrammer.in/tags/gobject/index.xml" rel="self" type="application/rss+xml"/><item><title>A C Of Polymorphism Without CPP</title><link>http://brightprogrammer.in/posts/a-c-of-polymorphism-with-cpp/</link><pubDate>Wed, 25 Mar 2026 00:00:00 +0000</pubDate><guid>http://brightprogrammer.in/posts/a-c-of-polymorphism-with-cpp/</guid><description>&lt;h1 id="introduction">Introduction&lt;/h1>
&lt;p>Polymorphism sounds like a C++ word. In C, it still exists, just without the sugar. You create a
base type, keep a table of function pointers, and pass that base pointer around.&lt;/p>
&lt;h1 id="a-small-example">A Small Example&lt;/h1>
&lt;p>Here’s an example. This is the base class.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-c" data-lang="c">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#007020;font-weight:bold">typedef&lt;/span> &lt;span style="color:#007020;font-weight:bold">struct&lt;/span> Animal Animal;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#007020;font-weight:bold">struct&lt;/span> Animal {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#902000">char&lt;/span>&lt;span style="color:#666">*&lt;/span> name;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#902000">void&lt;/span> (&lt;span style="color:#666">*&lt;/span>speak)(Animal&lt;span style="color:#666">*&lt;/span> self);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>};
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Now the child types.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-c" data-lang="c">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#007020;font-weight:bold">typedef&lt;/span> &lt;span style="color:#007020;font-weight:bold">struct&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Animal base;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>} Dog;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#007020;font-weight:bold">typedef&lt;/span> &lt;span style="color:#007020;font-weight:bold">struct&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Animal base;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>} Cat;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The polymorphic functions live in the child types. There is no default &lt;code>animal_speak()&lt;/code>, so this
behaves like a pure virtual method.&lt;/p></description></item></channel></rss>