[Up: Using C++ objects with Tcl]
[Previous: The C++ Domain] [Next: Dynamic Loading]
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.
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.
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''.
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]