[flagged]
Polymorphism in C is trivial: You use a vtable - a struct of method pointers. Same approach most languages with "simple" method resolution mechanisms, like e.g C++, uses under the hood. You don't need macros. You either call via the vtable, or you write a wrapper per interface per message. E.g. struct class { void (*vtable[NUM_SLOTS]); }; struct object { struct class * class; }; typedef struct object object; const in…
OOP in C
21–30 of 149 posts
Re: OOP in C
#22Earlier quoted context omitted.
Hi. For background, I have been writing professional OOP code in ANSI C. Yes, Structures in C can be used as classes. And yes, this includes polymorphism. My solution was to put a callback in the structure that would point to the implementation that was polymorphic. This allowed me to pass the struct around and then call the specific implementation at the right time. When you write OOP in C you do not need to make it…
please post an example of what such a callback would look like
Re: OOP in C
#23A whole book on this topic was already written in 1993 by Axel-Tobias Schreiner. It seems to be freely available nowadays: https://www.cs.rit.edu/~ats/books/ooc.pdf (edit: link updated to point to author's website)
Never truly finished it, but it works well :)
Re: OOP in C
#24[flagged]
Polymorphism in C is trivial: You use a vtable - a struct of method pointers. Same approach most languages with "simple" method resolution mechanisms, like e.g C++, uses under the hood. You don't need macros. You either call via the vtable, or you write a wrapper per interface per message. E.g. struct class { void (*vtable[NUM_SLOTS]); }; struct object { struct class * class; }; typedef struct object object; const in…
I know, thus the /s for sarcasms.
> Such wrappers are fairly trivial to generate if you don't want to handwrite them.
It's fairly irrelevant to my point, which is that C structs aren't equivalent to Java classes in anyway.
Of course you can implement OO is C, I've done enough GObject in my life to know how horrible it is to use in practice.
Re: OOP in C
#25Re: OOP in C
#26[flagged]
Polymorphism in C is trivial: You use a vtable - a struct of method pointers. Same approach most languages with "simple" method resolution mechanisms, like e.g C++, uses under the hood. You don't need macros. You either call via the vtable, or you write a wrapper per interface per message. E.g. struct class { void (*vtable[NUM_SLOTS]); }; struct object { struct class * class; }; typedef struct object object; const in…
Re: OOP in C
#27Earlier quoted context omitted.
Polymorphism in C is trivial: You use a vtable - a struct of method pointers. Same approach most languages with "simple" method resolution mechanisms, like e.g C++, uses under the hood. You don't need macros. You either call via the vtable, or you write a wrapper per interface per message. E.g. struct class { void (*vtable[NUM_SLOTS]); }; struct object { struct class * class; }; typedef struct object object; const in…
what does an "object" look like? how do i derive from it?
Re: OOP in C
#28Earlier quoted context omitted.
Polymorphism in C is trivial: You use a vtable - a struct of method pointers. Same approach most languages with "simple" method resolution mechanisms, like e.g C++, uses under the hood. You don't need macros. You either call via the vtable, or you write a wrapper per interface per message. E.g. struct class { void (*vtable[NUM_SLOTS]); }; struct object { struct class * class; }; typedef struct object object; const in…
what does an "object" look like? how do i derive from it?
Re: OOP in C
#29Earlier quoted context omitted.
Polymorphism in C is trivial: You use a vtable - a struct of method pointers. Same approach most languages with "simple" method resolution mechanisms, like e.g C++, uses under the hood. You don't need macros. You either call via the vtable, or you write a wrapper per interface per message. E.g. struct class { void (*vtable[NUM_SLOTS]); }; struct object { struct class * class; }; typedef struct object object; const in…
> You don't need macros. You either call via the vtable, or you write a wrapper per interface per message. I know, thus the /s for sarcasms. > Such wrappers are fairly trivial to generate if you don't want to handwrite them. It's fairly irrelevant to my point, which is that C structs aren't equivalent to Java classes in anyway. Of course you can implement OO is C, I've done enough GObject in my life to know how horri…
Re: OOP in C
#30Earlier quoted context omitted.
Polymorphism in C is trivial: You use a vtable - a struct of method pointers. Same approach most languages with "simple" method resolution mechanisms, like e.g C++, uses under the hood. You don't need macros. You either call via the vtable, or you write a wrapper per interface per message. E.g. struct class { void (*vtable[NUM_SLOTS]); }; struct object { struct class * class; }; typedef struct object object; const in…
Nothing in your example looks or reads as trivial . That is a gross perversion of a simple procedural language.
From a user point of view you end up doing things like:
string * str = string_new();
int len = string_length(str);
So it looks just like most other typical C code using prefixes per interface and passing pointers to structs around.In practice, like in C++, you'd tend to only actually use the vtable approach for those things which you actually need to be able to override based on type, which tends to be rare.
Another approach is to use messages instead, and a call a generic message handler. E.g. "ob->call(MSG_TYPE, ... arguments)" and just have a pointer to a message handler function as the first pointer in your objects. It's more flexible, but slower.