Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 606587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:16:12+00:00 2026-05-13T17:16:12+00:00

Suppose the following code declarations: itcl::class ObjectA { private variable m_ownedObject private variable m_someVariable

  • 0

Suppose the following code declarations:

itcl::class ObjectA {
    private variable m_ownedObject
    private variable m_someVariable

   constructor {} \
   {
        set m_ownedObject [ObjectA #auto]
   }

   protected method SetSomeVariable {newVal} {
       set m_someVariable $newVal
   }

   public method SomeMethod{} {
       $m_ownedObject SetSomeVariable 5
   }
}

This is the only way I know how to modify m_someVariable from within SomeMethod on m_ownedObject. In other languages (say C/C++/C#/Java to name a few), I’m pretty sure I could just say something like:

m_ownedObject.m_someVariable = 5

Is there a way to do something like this in tcl, or do I always need to create protected getters and setters? Hopefully this is reasonably clear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:16:13+00:00Added an answer on May 13, 2026 at 5:16 pm

    You cannot directly do what you’re asking for in itcl. However, this being Tcl, you can work around that, and directly set the member variable from anywhere. I use a helper routine called memv which you pass an instance and a variable name, and it returns a “reference” to that variable.

    This obviously bypasses the private/protected mechanisms that Itcl set up, so you’re violating abstractions using them. It’s your call whether you want to use it. I find it invaluable for debugging, but don’t it in production code.

    The example usage is:

    set [memv m_ownedObject m_someVariable] 5
    

    The code for memv is:

    proc memv {obj varname} {
      # have to look up the variable, which might be in a base class
      # so do 'info variable' to get that, and the full name is the 3rd element
    
      # next two lines handle pulling apart an array
      set aindex ""
      regexp -- {^(.+)\((.+)\)$} $varname ignore varname aindex
    
      set var [lindex [$obj info variable $varname] 2]
    
      if {$aindex == ""} {
        return [list @itcl $obj $var]
      } else {
        return [list @itcl $obj $var\($aindex\)]
      }
    }
    

    Similarly, I have a helper routine named memv which allows you to call any method (including private and protected methods). It’s usage is similar

    [memf m_ownedObject SetSomeVariable] 5
    

    And it’s code is:

    proc memf {obj fcnname} {
      set f [$obj info function $fcnname]
      if {[llength $f] != 5} {
        error "expected '$obj info function $fcnname' to return something like 'private proc ::namespace::name args {...}' but got: $f"
      }
      set fullname [lindex [$obj info function $fcnname] 2]
      set namespace [namespace qualifiers $fullname]
      set function [namespace tail $fullname]
      return [itcl::code -namespace $namespace $obj $function]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose we have following code defined in tester.py class Tester( object ): def method(
let's suppose to have following code: <body style=overflow: hidden> <div class=wall> <button>New</button> <div class=prova>
Suppose I have following code: public class CBase: AbstractC,IRenderable { //code here } public
Suppose the following template definition (the code is meaningless): template<class X, class Y> bool
Suppose you have this following code in HTML - <div id=pattern> <span class=a>Content</span> </div>
Suppose I have the following code: class a { public: virtual void do_a() =
Suppose I have the following hierarchy: class A { public: A() private: int aa;
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.