Re: True Inheritable Classes In C (was Generic Interface)

James C. Ahlstrom (jim@interet.com)
8 Mar 1995 16:55:49 -0500

In article <3j3vss$ii4@engr.orst.edu>,
Dianne Hackborn <hackbod@saurian.CS.ORST.EDU> wrote:
>
>Is there currently any way to create an object in C, which is an actual
>python "Class" object and can be used as a superclass in new Python [and C]
>objects? The application I am wanting to use this for is in a GUI -- I
>need to make a base "Widget" class in C, which can be subclasses in Python
>to do special interface handling and creating specialized classes.
>Basically the standard OO approach to GUIs.

This is what I am working on. I am trying to make the MFC class
hierarchy visible from Python so I can port some GUI code I have
running under Tcl/Tk to NT. I just got some code working which allows
Python to create C++ objects and inherit from the C++ hierarchy.
The method works without needing any new Python features.

First create the new C++ classes you need, and subclass them from
the GUI C++ class library you are using. For example, create a
C++ class CMyView subclassed from CView. In Python create a Python
class CView in a module and import it into your program. Your program
creates a class CMyView subclassed from CView (in Python). The two
classes Python CMyView and C++ CMyView are twins.

Next define all the member functions in Python CView that you care
about. These are equal to the corresponding functions in C++ CView.
The body is a call to an interface module my_module.cpp which calls
the corresponding function in C++ CMyView, which inherits fron C++
CView and the rest of the class hierarchy. You can override these
functions (members) in Python CMyView if you want.

The Python CMyView and C++ CMyView are twins, and a Python call on one
must convert to a C++ call on the other and vice versa. So set up
two dictionaries in C++ to convert Python object* to/from C++
CObject*. In a GUI environment using a black box class library like
MFC this is tricky. The only thing that works, and the thing that
makes the most sense, is to create the two dictionaries in the C++
class constructors, so you must arrange for the Python object*
to be available then.

You will find that you need to call Python member functions from
C++ to keep up the class hierarchy. Just use call_object as usual.
Since you are constantly interfacing C and C++, read the manual on
extern "C".

The above seems to work so far, but I am only creating documents
and views. Anyway it is something you can do right away without
waiting for the Ultimate Answer. Next I am adding buttons, and I
will no doubt learn more then.

BTW, "inherit from a C++ class hierarchy" comes up often and is
mentioned in the FAQ. And it is a very interesting issue. To
my mind there is no solution, and the problem is impossible. More
exactly, the "solution" is to write a compiler to convert C++
class declarations to the corresponding classes, and make my_module.cpp
too. The C++ class hierarchy is "immutable" in the Python sense, so
a hier.pyc seems a fair representation. Lacking a C++ class compiler,
the Python CView and my_module.cpp are written by hand.

If you want want code or further discussion, please post to this news
group, because I think this is of general interest. I read this group
faithfully. If I fail to respond, please e-mail, because I have some
evidence that some articles get dropped by my news system.

Jim Ahlstrom $clever