OOP in C?
-
Hallo Leute,
ich hab mir mal ein paar Konzepte angeschaut, wie man in C Objekt-Orientiet Model entwickeln kann.
Außer einigen gemeinsamkeiten in den Konzeptne, macht es jeder bischen anders:)
Nun wollte ich wissen, welches C OO-Konzept ihr empfehlen würdet!!?
Grüße
-
C++.
-
Das ist auch der Grund warum es zu C++ kam.
-
Emuliere doch einfach C++.
Statt:
class File { private: int fd; public: File(const char* name); char read(); void write(char c); };
typedef struct File_tag { } File; File* File_open(const char* name); char File_read(File* file); void File_write(File* file, char c);
etc
-
Hehe oh c++ noch nie gehört^^
Neee.. ich hab nur nen C Compiler, und würde Objekt-Orientiert coden!:)
Ja Ethon, das is die reguläre vorgehensweise. Naja denke jeder macht bischcen sein eignes Ding:)
-
CDiscoverer schrieb:
Neee.. ich hab nur nen C Compiler, und würde Objekt-Orientiert coden!:)
Für welches System denn?
-
Die Frage ist, wie weit möchtest du gehen? Wenn du sogar noch Vererbung einsetzen willst, kannst du in etwa folgendes machen:
struct foo_type { void (*bar)(struct foo *); }; struct foo { struct foo_type *type; // Vtable int qux; // Daten }; struct foo make_foo() { static struct foo_type type = {&bar_impl_for_foo}; struct foo new_foo; new_foo.type = type; return new_foo; } void bar_impl_for_foo(struct foo *This) { printf("%i", This.qux); } int main() { struct foo bla = make_foo(); bla.qux = 42; bla.type.bar(&bla); }
So in etwa. Zusammenfassend: C++?
-
Hab nen PIC 30F2011 ! Der hat "glaub" nur nen C-Compiler. Zudem würd ich C bischen vertiefen würde gern mal nen Scheduler/Kernel Ansatz umsetzen.
Ja /rank/ mit dem Konzept mit dem Typ konzept hatte ich mir überlegt:
typedef struct MyClass { int size; void (*Ctor)(void* self,...); void (*Dtor) (void* self); } void MyClass_Ctor(...); void MyClass_Dtor(); static const MyClass MyClass_TYPE = { sizeof(Mylass), MyClass_Ctor, MyClass_Dtor }; void*p= calloc(1, MyClass_TYPE.size); MY_Class_TYPE.Ctor(p,...); ....
so ungefähr..
-
CDiscoverer schrieb:
Hab nen PIC 30F2011 ! Der hat "glaub" nur nen C-Compiler.
Wenn du den Compiler von Microchip direkt nimmst, das sollte ein gcc-port sein, und der sollte C++ auch können. Ansonsten ist das was Ethon gezeigt hat imho die gängige Variante von OOP in C. Alles "weitere" ist stark Brainfuck-verdächtig und sollte vermieden werden.
-
Ethon schrieb:
typedef struct File_tag { } File; File* File_open(const char* name); char File_read(File* file); void File_write(File* file, char c);
Was soll daran OOP sein?
Nichts. Ist nur ein Abklatsch von fopen/fread/fwrite/fclose/...
-
/rant/ schrieb:
Wenn du sogar noch Vererbung einsetzen willst, kannst du in etwa folgendes machen:
struct foo_type { void (*bar)(struct foo *); }; struct foo { struct foo_type *type; // Vtable int qux; // Daten }; struct foo make_foo() { static struct foo_type type = {&bar_impl_for_foo}; struct foo new_foo; new_foo.type = type; return new_foo; } ... int main() { struct foo bla = make_foo(); bla.qux = 42; bla.type.bar(&bla); }
Was soll daran Vererbung sein?