1

I'm writing a simple language compiler,currently it's support functional programming only.

I want to support OOP concept, but I don't know how exactly I can represent an object ? just where to start .

Edit:

I just want to how to represent a class data type,not how to inheritance,.. etc.

4
8

Simple objects are just a struct containing all the fields. Methods typically take a pointer to that struct as their first argument (like python does). This simple form of OOP is done all the time in C.

Inheritance makes things more complicated. You can add a pointer to the parent object from the child object, or include all the parent's fields. Virtual methods require a vtable as part of the struct, which is basically function pointers for every virtual method. You need to add code that handles polymorphic dispatch. Look at GObject for a good production-quality example of this kind of OOP in C.

3
  • Thank you,it's really useful answer. Do you know how C++/Java represent objects? or it's like Python does ? – CodeMan Jan 7 '15 at 17:55
  • On a fundamental level, they all look basically the same: a data structure containing object-specific fields and a vtable. Any differences are in higher abstraction levels. – Karl Bielefeldt Jan 7 '15 at 18:12
  • I've looked at the workings of 3 implementations of Java in my time, and they're pretty much exactly as described here. Objects have a pointer to a class descriptor of some kind, which contains a table of pointers to methods. Object Pascal implementations (e.g. Delphi) also follow this pattern. C++ is like this for simple cases, but multiple inheritance makes things get much more complex. – Jules Jan 7 '15 at 20:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.