set/get methode

how is that called whats the corect syntax ?
a link would by great

1
2
3
4
5
6
7
8
9
10
11
12
class C
{
private:
	int _x
public:
	int X(int x) __set {_x = x;}
	int X()      __get {return _x;}
}

C *a = new C();
a.X = 1;
You can name them whatever you want, though getXX and setXX (where XX represents the variable name) are among the more commonly used names.

They're otherwise just like any other member function:
1
2
3
4
5
6
7
8
class myClass
{
    private:
        int foo;
    public:
        int getFooOrWhateverOtherFunctionNameYouChoose() const { return foo; }
        void setFooOrPickSomeOtherNameThatMakesSenseOrBananas(int x) { foo = x; } // Preferably not bananas
};

http://www.cplusplus.com/doc/tutorial/classes/
Last edited on
its not what i am looking for

i wana have this

C *a = new C();
a.X = 1;

and not

C *a = new C();
a.X(1);


note that X is a function (of some kind thats what i dont know) and not a variable
found it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct S {
   int i;
   void putprop(int j) { 
      i = j;
   }

   int getprop() {
      return i;
   }

   __declspec(property(get = getprop, put = putprop)) int the_prop;
};

int main() {
   S s;
   s.the_prop = 5;
   return s.the_prop;
}
Last edited on
Hmm, well, that appears to be Microsoft-specific, so don't expect that to work on other compilers.
Topic archived. No new replies allowed.