~sircmpwn/public-inbox

1

Opinions on class-like structs in C

Details
Message ID
<CKHCWBNQQPBH.2R1X8J1CMGNHP@unicorn-x220.lan>
DKIM signature
pass
Download raw message
Hello,

Since I am not a very experienced programmer, I'd love to hear some
inputs about treating a struct like an object/class in C since I am
learning about it currently and would like to have some ideas about why
I should or should not do it.

What I mean exactly is having "member functions" inside structs using
function pointers, and using overlaid structs to create derivative
"objects" that extend the basic one.

To give a concrete example, I would like to implement some widgets for a
terminal interface library (libtickit). For this, it makes sense to have
a basic template of things that each widget has and does. For example
each widget has a create() and destroy() function, as well as a render()
function, that are all supposed to behave the same way between all the
widgets so that they can be nested flexibly. They all have a parent
widget and some may have one or more child widgets. For all the things
that are the same across all widgets, it would be good to have an
uniform interface, which seems like it would be easiest to do by making
a basic "widget" struct that has a pointer to its parent, the create(),
destroy() and render() functions and whatever else there may be that can
be the same between all widgets. An individual widget implementation
would then be its own struct having the "widget" struct as its first
member and binding its functions etc to the appropriate field inside the
"widget" struct.

Does this seem like a sensible design? What would be some downsides of
using structs in this way, or is this something common? I'd love to hear
your thoughts and learn. Just superficially, it seemed a bit odd to take
this object-oriented approach in a language that has no concept of an
object, and I can imagine some criticisms about the transparency of
struct fields being function pointers, but it is hard for me to evaluate
the pros and cons at my current state of knowledge.

Thanks in advance,
Edin
Details
Message ID
<Yp4s1DvnlmGdPyRK@gmail.com>
In-Reply-To
<CKHCWBNQQPBH.2R1X8J1CMGNHP@unicorn-x220.lan> (view parent)
DKIM signature
pass
Download raw message
> Since I am not a very experienced programmer, I'd love to hear some
> inputs about treating a struct like an object/class in C since I am
> learning about it currently and would like to have some ideas about
> why I should or should not do it.

Some inputs from another not very experienced programmer:
object-oriented paradigm (OOP) is just point of view on
writing/structuring the code. Considering

	struct foo bar = ...;

and understanding foo as a class and bar as an object is perfectly fine.

> What I mean exactly is having "member functions" inside structs using
> function pointer:

You may have OOP without member functions. I liked reading about it in
the Practical Common Lisp book:

	https://gigamonkeys.com/book/object-reorientation-generic-functions.html

> using overlaid structs to create derivative "objects" that extend the
> basic one.

I think extending by composition fits better to C style:

	struct Keyboard {
		unsigned number_of_keys;
		enum { EN, DE } layout;
	};
	struct Monitor {
		double width;
		double height;
	};
	struct Notebook {
		struct Monitory display;
		struct Keyboard keyboard;
	};

	struct Notebook nb;
	nb.display.width = ...;
	nb.keyboard.layout = ...;
Reply to thread Export thread (mbox)