How do u do OOP with C?

How do u do OOP with C?

>inb4 use C++
kys pajeets.

Attached: tumblr_inline_oq5lufj1Ek1rpf0sr_540.png (256x256, 57K)

Other urls found in this thread:

jonisalonen.com/2012/calling-c-from-java-is-easy/
ooc-lang.org/
developer.gnome.org/gobject/stable/pr01.html
libcello.org/
dlang.org/spec/betterc.html
twitter.com/SFWRedditImages

you don't

jonisalonen.com/2012/calling-c-from-java-is-easy/

"OOP" means different things to different people. What do you mean when you say OOP? What specific features do you want?

Basically you have structs full of function pointers.

why would you want OOP? if you need variant use enum+union combo

use c++

kys

What's the point?
Why use a language for something it was not designed for?

Structs, Unions, and function pointers. But OOP is a terrible paradigm so why would you even want to attempt to use it.

because I want a C with classes and basic OOP?

>just use C++
kys nigger.

I'm a fellow C fag and I think this is essentially not doable without ruining the simplicity and elegance that can be achieved by C. Try out functional style programming instead (it isn't really possible to do true functional programming in C)

ooc-lang.org/

Seems no longer actively developed.

Seems cool but dead project

implement vtables in your structs for runtime polymorphisme

Emulate it with function pointers and you can implement your own virtual table so you can do inheritance then you can write your own malloc that calls init functions after allocating memory and then you can add clean up functions to atexit().

But first you have to find a mirror and look into your eyes and say "I'm not stupid"

Imagine being this retarded, just for a second - don't want anyone to hurt themselves.

this desu

Attached: ops.jpg (565x313, 41K)

#define :: __module__

developer.gnome.org/gobject/stable/pr01.html

Works great until you decide you need inheritance; then it gets fucky. I've never seen a class constructor idiom I really liked. And just forget multiple inheritance unless you're OK with basically implementing a non-mangled subset the Itanium C++ ABI by hand.

Look at Lua c library. High quality, and shows how to do it.
>tl;dr use structs

Seconding this.

struct A_vtable
{
void(*destroy)(struct A*);
};
struct A_base
{
int a;
};
struct A
{
struct A_vtable* vtable;
struct A_base a;
}
struct B_vtable
{
struct A_vtable a;
void(*set_x)(struct B*);
};
struct B_base
{
struct B_vtable* vtable;
struct b_base* b;
};

void A_init(struct A*);
void A_delete(struct A*);
void B_init(struct B*);
void B_delete(struct B*);

meant to write
>struct B_base b;

The only relevant OOP is structs+functions
Everything else is pajeetery

struct{
};

You're welcome.

Why do you think c++ was created tard? There's no oop in c

>"OOP" means different things to different people.

Maybe, but OOP is clearly defined.


You cannot achieve full OO but you can try. It will look a bit like Rust I think - error checking and all.

What is the definition of OOP?

>why would you even want to attempt to use it.

Scalability. Programs aren't 64k anymore.

Everything is an object.

Encapsulation, polymorphism, inheritance, etc.

Looks like vomit.

Then use FP.

You can do all of those with a forward declared struct pointer. I guess C is OOP.

I guess you're right.

Attached: obamalaughing.jpg (990x660, 87K)

>Ghetto-rigged complicated solutions

You might as well say assembly is OOP.

>I want a car, but without wheels.
fuck off, retard

Attached: 1532023802859.jpg (440x528, 19K)

I'm glad you see my point. Either OOP refers to a specific narrow set of semantics or its meaning is so broad and general that it can be applied to any kind of programming.
So I ask again, what's the definition of OOP?

>not possible to do functional programming in C
That’s retarded. You can write an entire functional language in C. It might not be easy but you certainly can.

Use objective-C. Pure C with OOP extensions. Gcc compiles it.

You can't even make a property private in C. Stop baiting.

Like I said, use a forward declared struct pointer. You don't really know C, do you?

>it isn't really possible to do true functional programming in C

True, but you can go a long way with bare function pointers. I've never understood why people are so quick to group functions into classes; it's actually quite nice to juggle function pointers around and only toss them in a struct later if you decide it makes sense. Compare to other OO languages that want you to create a bogus "functor" class with one method in it to get the same thing.

>I've never understood why people are so quick to group functions into classes

Because some functions are intended to used only one way. In C++ and similar languages, you don't have to add reference parameter to the function that's intended to be used on only one type of object. It's unnecessary overhead.

Forward declared struct pointers work for private data but not the rest. vtables for public methods have to be visible, just like any other function declaration. Not a big deal until you need inheritance - then it gets nuts. (Quick, how do you access stuff from the parent class? Multiple parent classes?)

Declaring a whole class and limiting yourself to one data type is a lot to ask just to save typing a parameter for "this". Not all OO languages even bother with implicit "this". Not saying it's not nice to have when you need it, but sometimes you just don't.

begone aplel nigger

The C standard is pretty helpful here. The standard guarantees that a pointer to a struct is equivalent to a pointer to its first element, and also that two different structs are equivalent when accessing common members of an initial sequence of elements.
The upshot of this is that you can get access to vtables and single inheritance just by casting pointers.

Unchecked casting really really sucks and there are (non conforming) compilers that don't respect the common initial subseqence rule. But yeah, if you are OK manually checking all the casts when you change the class heirarchy, you can do it.

Make some macros or inline functions if you don't like doing the casting yourself.

But whatever, that's all just details. The point is that OOP does not mean encapsulation and polymorphism, it means a particular semantics for achieving those things. These hacks aren't really OOP because they're just a poor way of approximating those semantics.

That's not even OOP.

Making super() an inline function is a great idea actually. If C had overloading it would be almost elegant... (inb4 _Generic; it's ugly as hell)

Look at glib.

What do you even use function pointers for if you don't do OOP?

libcello.org/

Any kind of procedure polymorphism. Polymorphism is not a strictly OOP thing.

>I want a C with classes and basic OOP
dlang.org/spec/betterc.html

and throw it into bin immediatelly

yes

>How do u do OOP with C?

You can't.

C has too much freedom and OOP is all about restrictions, safety and abstraction.

go back 20 years and reinvent c-with-classes

Take your usual struct and add function pointers to act as methods.
If you want shit like Inheritance, polymorphism and other fancy OOP stuff, you can either learn how those features are implemented in c++, or actually use c++.

Your inability to understand basic language design theory is super harmful to your potential as a programmer.

>Take your usual struct and add function pointers to act as methods.

3479837 unnecessary pointers and self-references.

explicit is better than implicit
your compiler will do it any way

Unnecessary back references are pervasive in real OOP too. It's symptomatic of a bad data model.

Struct represents the object, use function pointers as methods. Use cpp/java tho.

str = "herp derp"
len = str.len(str)

That's redundant, not explicit. Your argument is invalid.

>Everything is an object.
Only in pure OO languages. Not defining OOP.

Hiding things isn't fundamental to OOP.

Please stop this, it's embarrassing.

So use a macro to invoke methods.

C++

fpbp

You probably won't like my suggestion, but the closest thing to a real "C with classes" is Free Pascal.

Enums & magic numbers.
Maybe unions.