0

itcl::scopeの指定されたメンバ変数の完全な名前を返します$this

itcl::scope同じクラスの別のオブジェクト ( ではなく)を呼び出すにはどうすればよい$thisですか?

これが回避策です。

itcl::class dummy {
    variable m_data

    method function { other } {
        if { [itcl::is object -class dummy $other] } {
            error "Invalid argument."
        }

        set name "@itcl $other [$other info variable m_data -name]"
        # OR
        set name [lreplace [itcl::scope m_data] 1 1 $other]

        puts "==== this is the name of m_data of of object $other: $name"
    }
}

しかし、控えめに言っても、これは醜いです。

必要なものを返す必要があると思います$other info variable m_data -nameが、オブジェクトのコンテキストが省略されているだけです。

4

1 に答える 1

0

その情報を取得する良い方法はありませんし、作成するのも必ずしも良いことではありません。通常、オブジェクトの変数は、そのオブジェクトのインターフェイスの一部ではなく、そのオブジェクトの実装の一部であると見なす方が適切です。そのため、外部コードはそのように内部を突っ込むべきではありません。オブジェクトの外部の何か (クラスの他のメンバーを含む) が変数にアクセスすることが重要な場合は、変数への参照を返すメソッドを記述します (で生成されますitcl::scope)。

itcl::class dummy {
    variable m_data

    protected method dataRef {} { itcl::scope m_data }

    method function { other } {
        if { [itcl::is object -class dummy $other] } {
            error "Invalid argument."
        }

        set name [$other dataRef]

        puts "==== this is the name of m_data of of object $other: $name"
    }
}
于 2012-01-09T22:54:54.730 に答える