Wrapping C++ objects in Tcl

Other solutions exist to do this, but this is a simple templatized header file that you can embed in anything. It's much much simpler than having a relatively huge library like SWIG to take advantage of Tcl in C++.

Define your C++ class that you want to expose to Tcl like this:
class TCLCPPCLASS(foo) {
public:
        int method(int oc, Tcl_Obj *ov[]) {
                //
                // the body of your method goes here 
                //
                return TCL_OK;
        }

        TCLCPPOBJCONSTRUCTOR(foo) {
                RegisterMethod("method", &foo::method);
                //
                // your object's constructor goes here
                //
                // int oc, Tcl_Obj *const ov[] are available
                // also the protected methods get_type() and get_name() and
                // get_interp() exist here
                //
        }
};
Then in main(), do something like this below or
allocate it globally and keep the object around.
TclCPPObjFactory<foo> objcreator(interp, "FOO");
Now in Tcl, you can do the following and it'll call the C++ class foo's
constructor and then it's method() with 3 Tcl_Obj* args: 1 2 3.
set obj [::FOO::new]
$obj method 1 2 3
You can also do this and the constructor will get called with 3 args: 1 2 3.
set obj [::FOO::new 1 2 3]


Download the header file here