[Next: Contents]

Using C++ objects with Tcl

Frank Pilhofer


The ``tclobj'' package allows you to access C++ classes and to operate C++ objects from the Tcl language. They can be created and deleted, class member functions can be called, and objects can be passed as parameters to other objects' member functions. Using dynamic loading, class definitions can be loaded into the interpreter at runtime.

Some code must be added to the class definition, but existing code does not need to be changed. Each class that you want to be visible from the Tcl level must be derived from a common base class, and two member functions must be overloaded to interface between the two worlds. With some carefully placed preprocessor directives, the new Tcl-dependent part of a class can be easily activated or disabled at compile time (for use outside of Tcl-based applications).

The implementation is pretty efficient. As long as an object is only used in the C++ domain and is not handed down to the Tcl level, the overhead is nil but for two variable initializations. Time is only consumed during the setup of a call from a Tcl procedure into a C++ object and its return due to argument checking and conversion.

This package does not claim to be perfect, as there are some deficiencies and open problems, but can certainly be called interesting.

Frank Pilhofer
Wed Mar 12 14:37:08 MET 1997