0

私が次の構造を持っているとしましょう:

package require Itcl


itcl::class AAA {

private variable m_list {}

constructor {} {
    fill m_list list
}

}

書くためにm_listの参照を取得する方法

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

そのリストは本当に大きいので、コピーしたくないと考えてください。

4

2 に答える 2

4

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

上記のコードは、2つの巨大なリストになります。1つはxが任意のyを指す元のデータを持ち、もう1つはzだけが指す123の追加要素を持ちます。lappendステートメントの前は、リストのコピーは1つだけで、3つの変数すべてがそれを指していました。

于 2011-08-05T05:05:39.657 に答える
1

クラスのメンバーに関する参照を取得する方法は次のとおりです。

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]
于 2011-08-05T05:20:45.117 に答える