Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy constructor in itcl

153 views
Skip to first unread message

amit....@gmail.com

unread,
Mar 28, 2006, 11:57:16 PM3/28/06
to
Hi,
I have a problem here in which I need to copy an object and then
configure it.
suppose I have "object1" I need to create a separate object "object2"
which copies all attributes of object1 but is a separate entity.
Similar to a copy constructor in C++.
I then want to tweak some of the vairables/values of the newly created
object.

Is there some way of implementing this in Itcl.

-Amit

amit....@gmail.com

unread,
Mar 29, 2006, 3:51:21 AM3/29/06
to
Okay I did it and here it is
------------------------------------------------------------------------------
itcl::class visInfo {
constructor {args} {
if { [catch {
if { [$args isa visInfo] } {
#Copy Constructor here
configure -name "[$args cget -name]"
configure -x "[$args cget -x]"
configure -y "[$args cget -y ]"
configure -fontSize "[$args cget -fontSize]"
configure -zorder "[$args cget -zorder]"
}
} err] } {
eval configure $args
}
}
public variable name ""
public variable x ""
public variable y
public variable fontSize ""
public variable zorder ""
}
--------------------------------------------------------------
let me know if there is a cleaner implementation

Googie

unread,
Mar 29, 2006, 7:24:40 AM3/29/06
to
Writting value-copying by hand for each variable in class may be nasty.
It can be automated, so you can add/remove/modify variables in class and
copy-constructor will take care of everything.

------------------------------------------------------------------------------
itcl::class visInfo {
constructor {args} {
if { [catch {
if { [$args isa visInfo] } {
#Copy Constructor here

foreach vName [$this info variable] {
set realName [string range $vName [expr {[string last "::"
$vName]+2}] end]
configure -$realName [$args cget -$realName]


}
}
} err] } {
eval configure $args
}
}
public variable name ""
public variable x ""
public variable y
public variable fontSize ""
public variable zorder ""
}
--------------------------------------------------------------

But note, that it's (nearly sure) slower method, becouse interpreter
needs to inspect and process some data before copying value and your
method gives ready to use instructions - what to copy, where from and
where to.

--
Pozdrawiam! (Greetings!)
Googie

Eckhard Lehmann

unread,
Apr 1, 2006, 2:00:01 PM4/1/06
to

amit....@gmail.com wrote:

> --------------------------------------------------------------
> let me know if there is a cleaner implementation

In addition to what Googie said already, you could ommit the "catch",
and use Itcl's introspection capabilities more intensively:

itcl::class visInfo {
constructor {args} {

if {[llength $args] == 1 && [$args isa visInfo]} {
foreach v [$args info variable] {
set [namespace tail $v] [$args info variable $v -value]
}
} else {
eval configure $args
}
}

public {
variable name ""
...
}
}

This works not only for public variables, but also for private and
protected ones. But note that it does not make deep copies of member
objects - it just copies string references...


Eckhard

0 new messages