假设我有以下结构:
package require Itcl
itcl::class AAA {
private variable m_list {}
constructor {} {
fill m_list list
}
}如何获取m_list上的引用以便编写
foreach elem $reference {.......} 考虑到列表真的很大,我不想复制它!
发布于 2011-08-05 13:05:40
Tcl变量使用写入时复制语义。您可以安全地传递一个值,将多个变量赋给它,而不必担心它会占用更多的内存空间。
例如
set x {some list} ;# there is one copy of the list, one variable pointing at it
set y $x ;# there is one copy of the list, two variables pointing at it
set z $y ;# there is one copy of the list, three variables pointing at it
lappend z 123 ;# there are two copies of the list
;# x and y pointing at one
;# z pointing at the other
;# which is different from the first via an extra 123 at the end上面的代码将产生两个巨大的列表,一个包含x和y都指向的原始数据,另一个包含只有z指向的额外元素123。在lappend语句之前,只有一个列表副本,所有三个变量都指向它。
发布于 2011-08-05 13:20:45
下面是如何获取类成员的引用:
package require Itcl
itcl::class AAA {
public variable m_var 5
public method getRef {} {
return [itcl::scope m_var]
}
}
AAA a
puts [a cget -m_var]
set [a getRef] 10
puts [a cget -m_var]https://stackoverflow.com/questions/6951626
复制相似问题