[Up: Using C++ objects with Tcl]
[Previous: The C++ Domain] [Next: Dynamic Loading]

The Tcl Domain

Basic Usage

Basically, only two new Tcl commands are provided, ``new'' and ``delete'', which you already know from C++ programming. As Tcl commands, they do just the same.

The first parameter to new is the name of a class, and the following arguments are treated as arguments to the constructor. As result, you receive a unique identifier. This identifier is actually a Tcl command which you can then call the object's member functions on. Looking at the definition of the test class from figure 1, the following sequence would be valid:

set x [new test]
$x set hello world
set y [new test $x]
$y get
delete $x $y

First, an empty object of type test is created using the default constructor. The identifier is stored in the variable ``x''. We then invoke the member function ``set'' of class test, setting the value of the object to type ``hello world''. Next, we create another object of type test, but this time using the copy constructor, with the first object as an argument. Invoking the ``get'' member function should therefore produce ``hello world''. At last, both objects are destroyed.

The delete function is particularly easy to understand. It takes any number of object identifiers as parameter and destroys them, calling their destructors.

A slightly different mechanism to create and destroy objects exists, too. This one is more reminiscent of automatic variables within a function, which is precisely their purpose.

test x
x set hello world
test y x
y get

This code fragment does much the same as the previous. However, we do not use the new operator. The first line creates an object of type test called ``x''. This is quite different from having a unique identifier created and having this identifier assigned to a variable of name ``x''. This is apparent in the second line, where we do not need to take the value of ``x'' to extract the identifier, because ``x'' itself is the identifier.

Another difference is that the two objects ``x'' and ``y'' that have been created cannot be destroyed with the delete operator. Instead, they are automatically destroyed when scope is lost, thus behaving like local variables.

Still, objects that have been created this way are different from local Tcl variables. Global variables are visible in a procedure unless they are shadowed with a local variable and can not be (and need not be) accessed with Tcl's global command.

You must also take care that you do not create objects with the same name as existing Tcl procedures, which would then become inaccessible.

Type Information

By deriving your classes from the Tcl_Object base class, the ``info'' member function is inherited on the Tcl level. Through this member, a Tcl program can query some information about the object.

object info id

Prints the object's type and a unique identifier (actually the numeric value of the this pointer).
object info dep

Returns a whitespace-separated list of the object's type and its (linear) derivation tree (the types of the object's base classes). Last in this list is always Tcl_Object.
object info ops

Returns a whitespace-separated list of member functions available for this class, including its base classes (which are prefixed with their type name, for example Tcl_Object::info). This list also includes ``new'', which is in fact no member function but the constructor.
object info sig

Prints all available signatures of all member functions (including the constructors). This list can get quite lengthy.
object info func

If func is a member function available for object, print all member functions of this name and their argument lists.

The disadvantage of the ``info'' member is that you need an existing object. As an alternative, you can also call the ``info'' operation on the class's type name (excluding ``info id'', because there is no object to have an identifier). Keep in mind that this is the same syntax as with the creation of ``local variables''.

Scope resolution

Like in C++, you can use the scope resolution operator on a member function in order to call a base class's implementation which would normally be shadowed by a derived class's members. For example, if a class overloads the ``info'' member, but you want to retrieve information about the object using the ``info'' implementation of Tcl_Object, you would use

object Tcl_Object::info id

[Previous: The C++ Domain] [Next: Dynamic Loading]
[Up: Using C++ objects with Tcl]

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