首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取Itcl类成员变量的引用?

如何获取Itcl类成员变量的引用?
EN

Stack Overflow用户
提问于 2011-08-05 12:57:21
回答 2查看 1.6K关注 0票数 0

假设我有以下结构:

代码语言:javascript
复制
package require Itcl


itcl::class AAA {

private variable m_list {}

constructor {} {
    fill m_list list
}

}

如何获取m_list上的引用以便编写

代码语言:javascript
复制
foreach elem $reference {.......} 

考虑到列表真的很大,我不想复制它!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-05 13:05:40

Tcl变量使用写入时复制语义。您可以安全地传递一个值,将多个变量赋给它,而不必担心它会占用更多的内存空间。

例如

代码语言:javascript
复制
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语句之前,只有一个列表副本,所有三个变量都指向它。

票数 4
EN

Stack Overflow用户

发布于 2011-08-05 13:20:45

下面是如何获取类成员的引用:

代码语言:javascript
复制
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]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6951626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档