r/c_language • u/VerterRobot • Jan 03 '26
Programming objects in C in style of Go
Hi there,
What do you folks think about this approach of programming objects in C? It supports non-virtual methods and private members.
goc.h
#ifndef GOC_H
#define GOC_H
#define fn(obj, fn, ...) (obj).fn(&(obj), ##__VA_ARGS__)
#endifwoman.h
#ifndef WOMAN_H
#define WOMAN_H
#include "goc.h"
struct woman;
typedef struct w {
struct woman *w;
void (* hug)(struct w *self);
void (* kiss)(struct w *self);
} woman;
woman new_woman(char *);
void free_woman(woman *);
#endifwoman.c
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "woman.h"
struct woman {
char *name;
bool hugged;
bool kissed;
};
void hug(woman *self) {
if (self->w == NULL) {
printf("No woman allocated!\n");
return;
}
printf("You're hugging %s\n", self->w->name);
if (self->w->hugged) {
printf("%s say: It hurts, get off me!\n", self->w->name);
return;
}
if (self->w->kissed) {
printf("%s say: Get off me, creep!\n", self->w->name);
return;
}
self->w->hugged = true;
printf("%s say: you're sweet!\n", self->w->name);
}
void kiss(woman *self) {
if (self->w == NULL) {
printf("No woman allocated!\n");
return;
}
printf("You're kissing %s\n", self->w->name);
if (!self->w->hugged) {
if (self->w->kissed) {
printf("%s is calling the police!\n", self->w->name);
return;
}
self->w->kissed = true;
printf("%s say: How dare you?\n", self->w->name);
return;
}
if (self->w->kissed) {
printf("%s say: Level up, boy!\n", self->w->name);
return;
}
self->w->kissed = true;
printf("%s say: Mwah!\n", self->w->name);
}
woman new_woman(char *name) {
struct woman *w = calloc(1, sizeof(struct woman));
w->name = name;
woman women = {
.w = w,
.hug = hug,
.kiss = kiss
};
return women;
}
void free_woman(woman *w) {
free(w->w);
w->w = NULL;
}main.c
#include <stdio.h>
#include "woman.h"
int main(void)
{
woman woman;
printf("\033[1mThe right workflow:\033[0m\n");
woman = new_woman("Alice");
fn(woman, hug);
fn(woman, kiss);
fn(woman, kiss);
free_woman(&woman);
putchar('\n');
printf("\033[1mThe wrong workflow:\033[0m\n");
woman = new_woman("Miranda");
fn(woman, kiss);
fn(woman, hug);
fn(woman, kiss);
free_woman(&woman);
putchar('\n');
printf("\033[1mThe dreaming workflow:\033[0m\n");
fn(woman, hug);
fn(woman, kiss);
fn(woman, kiss);
return 0;
}This program prints:
The right workflow:
You're hugging Alice
Alice say: you're sweet!
You're kissing Alice
Alice say: Mwah!
You're kissing Alice
Alice say: Level up, boy!
The wrong workflow:
You're kissing Miranda
Miranda say: How dare you?
You're hugging Miranda
Miranda say: Get off me, creep!
You're kissing Miranda
Miranda is calling the police!
The dreaming workflow:
No woman allocated!
No woman allocated!
No woman allocated!
0
Upvotes
4
u/tstanisl Jan 03 '26
It's one way to implement OOP in C. Though a bit more popular way is:
_init()but no_new()functionobjcontainer_of-like macros to implement typesafe single or multi inheritance6
u/Tabsels Jan 03 '26
If you want a serious response go find a less objectionable example.
1
u/[deleted] Jan 03 '26
Ok, so you force runtime dispatch for both non-virtualised functions (which otherwise are a direct call, but now require an additional pointer load unless the compiler manages to devirtualise), and your example requires a lot of passing around of expensive "interface" structs (
struct w, especially if the number of methods becomes large).The style is certainly peculiar, and perhaps if you need to use opaque objects in all places this might make sense. But as far as I understand it, you use opaque structs if you want to change the implementation without changing the interface (avoiding pointless recompilation). Does that hold here, given that whenever you change the
struct womanbacking type, you may also change the interfacestruct w, and so induce recompilation?