Getting and Setting Variables

In the .NET Framework it is possible to use variables as functions; in the sense that when you call the variable is performs a function and when you set a variable value a different function is performed.

An example (in C#) of this follows:

1
2
3
4
5
6
7
8
9
10
11
12
int _MyVar = 0;
int MyVar
{
   get
   {
      return _MyVar;
   }
   set
   {
      _MyVar = value;
   }
}


My question to you is can this be done without the .NET Framework in C++?

Thanks in advance.
in c++ "_MyVar" and "MyVar" would be two different declarators...

u could encapsulate this in an structure(struct / class)... as far as i know^^
They are two different declarations here too.
What I am asking is, is it possible to tell when a variable is 'called' (in a sense) and when the value is changed?
Not directly without modifying the environment...

Google around "c++ property classes" for information on how to do the same sort of thing in C++. You should be aware, however, that it is just better to use getter/setters from the get-go in your classes:
1
2
3
4
5
6
7
8
class Foo
  {
  public:
    int getBar() const { return m_bar; }
    void setBar( int bar ) { m_bar = bar; }
  private:
    int m_bar;
  };

You can report that the value was changed/read from within the function that is called.
Hope this helps.
Last edited on
Yeah, I did think of that but I was hoping I wouldn't have to use that method.
It sounds like there isn't anyother way really, but Thank You for your help.
Topic archived. No new replies allowed.