开发者

ITCL - How to access associative array member inside a class?

开发者 https://www.devze.com 2022-12-09 15:49 出处:网络
How to access associative array member of a class inside the class itself? Itcl is modeled after C++, and in C++ we would write:

How to access associative array member of a class inside the class itself? Itcl is modeled after C++, and in C++ we would write:

SomeObject.SomePublicMember = ...

How to do the same in Itcl? Without providing accessor procedure for such an array. I've seen that for usual plain variables this can be ob开发者_开发技巧tained by using cget:

$this cget -PublicMemberVariableName

However the following construct doesn't work:

$this cget -AssociativeArrayName(NamedIndex)

Is this possible at all?


Alas, cget won't get what you want. The array element isn't passed all the way down to ItclGetInstanceVar (I'm not sure why).

You can use get/set and the like:

class myObject {
   public variable AssArray
   constructor {} {
      array set AssArray ""
   }
   method setArr { elem val } {
      set AssArray($elem) $val
   }
   method getArr { elem } {
      return $AssArray($elem)
   }
   method getFullArr {} {
      return [array names AssArray]
}
0

精彩评论

暂无评论...
验证码 换一张
取 消