NAME
class - Define a class
SYNOPSIS
class namespace ?parent_namespace parent_namespace ...? definition
DESCRIPTION
-
The class command starts a definition (or redefinition) of a class.
-
namespace
-
The namespace in which the class will reside. If no namespace qualifiers
are present, it becomes a child namespace of the namespace where the class
command was run. Also created is an alias for the object command
by the same name. For example, if I name the class ::my::sampleclass, within
::my there will be a command called sampleclass which dealiases to object
::my::sampleclass
-
parent_namespace
-
A class namespace from which to inherit members. There can be any
number of parents. If no namespace qualifiers are present, the class
is searched for as follows;
-
class' parent namespace (peer context)
-
global namespace (global context)
-
(future versions will support the calling context - where the class command
was run from)
-
definition
-
#
-
A comment is ok inside a class definition
-
private:
-
Any member declared after this tag is of private access
-
protected:
-
Any member declared after this tag is of protected access
-
public:
-
Any member declared after this tag is of public access
-
private
-
The member declared on the same line as this tag is of private access
-
protected
-
The member declared on the same line as this tag is of protected access
-
public
-
The member declared on the same line as this tag is of public access
-
static
-
The member declared on the same line as this tag is of static scope
-
virtual
-
The proc declared on the same line as this tag is virtual
-
types
-
variable name ?default_value?
-
array name ?default_value?
-
proc name ?argument_list body?
-
constructor ?argument_list body?
-
destructor ?body?
-
object class_namespace name
-
class_namespace name
-
reference name
-
class name ?parent_namespace parent_namespace ...? definition
-
notes
-
array's default_value should always be in pairs
-
proc/constructor/destructor can be defined outside of the class
definition and can be deferred until the moment before a first call by
an object.
-
class_namespace below object is the same as using object
-
class is a subclass and this is usually done when only objects of
the parent class will instanciate objects of the subclass
-
constructor and destructor are actually named like C++ (so
you can also use proc to define them). In the example below, one
could have used proc myclass and proc ~myclass. In the case of the
destructor, do not attempt to use any arguments, this will generate errors.
Note that these member names are reserved for the constructor/destructors,
even if you don't intend to define any yourself. There is a default
constructor defined for you if you do not define one yourself. It
takes a single argument which is a dereference of an object and sends this
to the copy procedure.
-
-
example:
-
class myclass {
-
private:
-
static variable myclasscount 0
-
variable pvtScalar {Default value}
-
constructor {} {
-
incr myclasscount
-
}
-
destructor {
-
incr myclasscount -1
-
}
-
public:
-
proc getvalue {} {
-
return $pvtScalar
-
}
-
proc setvalue {val} {
-
return [set pvtScalar $val]
-
}
-
proc getcount {} {
-
return $myclasscount
-
}
-
proc externexample
-
}
-
proc myclass::externexample {} {
-
# We still have access to members!
-
puts $pvtScalar
-
return {An example of an externally defined member procedure}
-
}
-