Live data from Hacker News

OOP in C

staff.washington.edu

21–30 of 149 posts

Re: OOP in C

#21
post #16

[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…

what does an "object" look like? how do i derive from it?

Re: OOP in C

#22
post #13
post #9

Earlier 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

One example: https://www.codementor.io/@michaelsafyan/object-oriented-pro...

Re: OOP in C

#23

A 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)

Years ago, I started an implementation of that book: https://github.com/linkdd/ooduck

Never truly finished it, but it works well :)

Re: OOP in C

#24
post #16

[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…

> 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 horrible it is to use in practice.

Re: OOP in C

#26
post #16

[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…

Nothing in your example looks or reads as trivial. That is a gross perversion of a simple procedural language.

Re: OOP in C

#27
post #21
post #16

Earlier 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?

Any struct. Only requirement would be that the first element can be casted to a point to a class object containing the vtable. I'ved edited to fix the casting so it actually compiles, and to use a void * in the argument list, as is what you'd actually usually do.

Re: OOP in C

#28
post #21
post #16

Earlier 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?

generic base type struct

Re: OOP in C

#29
post #16

Earlier 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…

They only way they aren't is that the class pointer is hidden from the user.

Re: OOP in C

#30
post #16

Earlier 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.

Trivial to a C developer then. This has been typical of C code for literally decades. The only thing that sucks there is writing wrappers, but it's easy to generate them.

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.

Post reply on HN