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

发布于 2024-11-28 01:39:37 字数 271 浏览 2 评论 0原文

假设我有以下结构:

package require Itcl


itcl::class AAA {

private variable m_list {}

constructor {} {
    fill m_list list
}

}

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

foreach elem $reference {.......} 

考虑到列表确实很大,我不想复制它!

Say I have the following structure:

package require Itcl


itcl::class AAA {

private variable m_list {}

constructor {} {
    fill m_list list
}

}

How to get a reference on the m_list in order to write

foreach elem $reference {.......} 

Consider that list is really big and I don't want to copy it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画骨成沙 2024-12-05 01:39:37

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 语句之前,只有一份列表副本,并且所有三个变量都指向它。

Tcl variables use copy-on-write semantics. You can safely pass a value around, assigning multiple variables to it, without worrying about it taking up more space in memory.

For example

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

The above code will result in two giant lists, one with the original data that both x any y point at, and one with the extra element of 123 that only z points to. Prior to the lappend statement, there was only one copy of the list and all three variables pointed at it.

江湖正好 2024-12-05 01:39:37

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

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]

Here is how to get a reference on the member of a class:

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文