Static Member variable

How do you initialize a static variable when it's a struct?? i can't make it work...

1
2
3
4
5
6
7
8
9
10
11
struct sommething
{
     int a;
     int b;
};

class Myclass
{
public:
     static something c1;
};


Thanks!!
You misspelled something in your struct, maybe that's your problem?
You can always just use a constructor.
Otherwise try it normally:
 
something Myclass::c1.a = 3;
Last edited on
I haven't tested, but I don't think you can do it that way. You'd have to initialize the whole struct.

One way is to use the {braces} approach:

 
something Myclass::c1 = {3,5};  // a=3, b=5 


Another way would be to give something a constructor.
:o Disch I tried that and it worked. That's pretty cool I didn't know that :)
One way is to use the {braces} approach:


Awesome, thank you!!
could you also just type

1
2
int a = 0;
int b = 0;


to initialize the integers for A and B?
If you wanted to initialize a and b separately you'd have to do it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct something
{
     int a;
     int b;
};

class Myclass
{
public:
     static something c1;
};

Myclass::c1.a = 0;
Myclass::c1.b = 0;


EDIT: Scratch that. That doesn't work.
Last edited on
Topic archived. No new replies allowed.