Skip to content

paulogarithm/cclass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Classes in C, litterrally

The REAL code is in v2

let me ask you something, whats the difference between C structs and C++ classes ?

"🤓 axually it's JUST that the structs are public by default in C++, that's the only diff..."

shut your butch ass up, the point i wanted to make is that structs/classes in C++ auto-integrates this, which refers to the object itself.

the only thing is that it's impossible in C, since function pointer in a struct is just a reference to a function, and since functions are "global" to the file, there is no way to actually know what is your object, beside giving it as an argument.

Or so i thought...

Behold, the c classes...

    counter_t *x = newobjectof(counter, 3); // you create your own object
    x->increment(3); // you can do this, and it will INCREMENT ???!!!!
    printf("%d\n", x->number); // 6

you may wonder, "ok whats in your counter_t class then"

here is the definition:

typedef struct {
    int number;
    void(*increment)(int n);
} counter_t;

"wait... that's it ? how do you define your method then, it should be hell"

nope, just as simple as that (it will change soon)

CCL_METHOD(void, counter_t, increment, int n) {
    this->n += n;
} CCL_METHODFOOT(increment)

how is it possible ? is it magic ?

How does it work

todo: explain

About

Litterally, classes in C !

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors