Re: [dev] Object-Oriented C for interface safety?

From: <random832_AT_fastmail.us>
Date: Thu, 27 Nov 2014 14:07:47 -0500

On Thu, Nov 27, 2014, at 07:27, koneu wrote:
> Greetings.
>
> The two things that really make OO languages worthwhile in my opinion
> are polymorphism and inheritance. Doing polymorphism and data/code
> hiding in C is easy enough with const function pointers. You can just
> define public interfaces in their own header like
>
> struct interface {
> void * const this;
> int (* const get_foo)(void *this);
> void (* const set_foo)(void *this, int foo);
> char * (* const get_bar)(void *this);
> void (* const set_bar)(void *this, char *bar);
> };
>
> and implement them in "classes" like
>
> struct class {
> int foo;
> char *bar;
> };

In general when this is done in real life, you do it the other way
around, so you only need one copy of the interface structure per class.
Received on Thu Nov 27 2014 - 20:07:47 CET

This archive was generated by hypermail 2.3.0 : Thu Nov 27 2014 - 20:12:08 CET