October 22, 2024
Chicago 12, Melborne City, USA
C#

Creating classes in C


is this how you guys would create a "class" in C? How about an interface? I’m guessing we just use function pointers for everything and we can change where the function pointers point to?

#ifndef CLASS
#define CLASS

void method1() { printf("method #1"); }
void method2() { printf("method #2"); }

typedef struct {
    void (*method1)(void);
    void (*method2)(void);
} Class;

Class class;

void class_setup()
{
    class.method1 = &method1;
    class.method2 = &method2;
}

#endif

int main()
{
    class_setup();

    class.method1();
    class.method2();

    return 0;
}



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video