access static private members of a class

Nov 13, 2010 at 11:10pm
So I have a class :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
///header 
class C {
private:
   static int i;
public:
   void set_i(int j);
};
///cpp
int C::i=0;
void C::set_i(int j){i=j;}
///main
//...
int N=atoi(argv[1]);
//here I want to access C::i *before* creating a C-object (so I can't use set_i yet) 

Possible ?

Thanks :)
Last edited on Nov 13, 2010 at 11:11pm
Nov 13, 2010 at 11:55pm
Yes, you don't need objects to access static members, but you need to be in a friend class/function if it's private
if method set_i only modifies the static member should be static too
Nov 14, 2010 at 9:37am
So I need to define either a static (friendly to C) function outside everything or a static function inside another (friendly to C) class ?
EDIT : or just this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
///header 
class C {
private:
   static int i;
public:
   static void set_i(int j);
};
///cpp
int C::i=0;
void C::set_i(int j){i=j;}
///main
//...
int N=atoi(argv[1]);
C::set_i(N);
//simple as that ?! 
Last edited on Nov 14, 2010 at 9:50am
Nov 14, 2010 at 10:40am
The code above should work
Nov 14, 2010 at 10:59am
Thanks :)
Topic archived. No new replies allowed.