In January of 2004, a poster to comp.lang.tcl asked which of the [tcl] [extension]s providing [object orientation] allowed one to define class members which were other objects. Several people submitted answers - and some of the answers provided demonstrated how the different extensions might do just that. Here's some of the examples provided: ---- [Snit] snit::type C2 { variable this variable that constructor {args} { set this [C2 %AUTO%] set that [C2 %AUTO%] } destructor { catch {$this destroy} catch {$that destroy} } method doThis {} { $this doSomething } method doThat {} { $that doSomething } } [Peter Lewerin]: shouldn't it be like this: snit::type C1 { option -partof method doSomething {} { puts "[$self cget -partof]'s $self: born to do it" } } snit::type C2 { delegate method doThis to this as doSomething delegate method doThat to that as doSomething constructor {args} { install this using C1 %AUTO% -partof $self install that using C1 %AUTO% -partof $self $self configurelist $args } destructor { catch {this destroy} catch {that destroy} } } [incr Tcl] itcl::class C1 { } itcl::class C2 { variable m1 variable m2 constructor {} { set m1 [C1 #auto] set m2 [C1 #auto] } destructor { itcl::delete object $m1 $m2 } } [stooop] class C2 { proc C2 {this} { set ($this,o1) [new C1] set ($this,o2) [new C1] } proc ~C2 {this} { delete $($this,o1) $($this,o2) } class C1 { proc C1 {this} {} proc ~C1 {this} {} } } delete [new C2] [XOTcl] There are some posibilities '''Example 1''' (by using instance variables) Class C1 Class C2 C2 instproc init {} { my instvar o1 o2 set o1 [C1 new] set o2 [C1 new] } C1 instproc useO1 {} { [my set o1] info class } C1 instproc useO2 {} { my instvar o2 $o2 info class } C2 instproc destroy {} { my instvar o1 o2 $o1 destroy $o2 destroy next } '''Example 2''' (by using nested objects and parameters. Does not need to destroy explicit the objects) Subobjects are better to implement aggregation or membership (part of, has a) Class C1 Class C2 -parameter {o1 o2} C2 instproc init {} { my o1 [C1 new -childof [self]] my o2 [C1 new -childof [self]] } '''Example 3''' (by using named subobjects. Does not need to destroy explicit the objects) Quite similar to C++ object members Class C1 Class C2 C2 instproc init {} { C1 create [self]::o1 C1 create [self]::o2 } C2 intporc useO1 {} { puts "class of o1 [[self]::o1 info class]" } ---- [Category Object Orientation] | [Category Example]