static variables in classes

I am trying to make a class called "Arguments" which will handle all the arguments for my program.

There is a void function inside the class that will:
-take any arguement who's argv[0] = "-" or "/"
-evaluate the rest of the characters one by one, and adjust boolean values of flags as necessary.

Currently I have these boolean values defined private and static inside the Arguments definition. Now I'm not sure that was the best way to define them.
I wanted to avoid global variables. Since my program will completely process one argument before moving on to the next it seemed like using static makes sense.

My understanding of static variable, once assigned is that it CAN be changed however whatever it's value is, it is universal for all members of that class.

If I'm wrong there, I guess I'll have to try something else. However, if I'm right, I can't figure out how to set preliminary values for these flags.

Like I said, they are defined in the definition, but I need to predefine them as false. With other non-static variable I was able to do this in the constructor.

No matter what I try I can't seem to define these.

Thanks.
Are you sure that these variables should be static? If a constructor of an object need to initialize static variable when they are candidates to be non-static.

static variables are usually initialized independently of any object of the class. For example

1
2
3
4
5
6
struct A
{
   static bool flag;
};

bool A::flag;


In this example flag will be initialized by false.

Or you can reset values of static variables in a constructor of the class

1
2
3
4
5
6
7
struct A
{
   A() { flag = true; }
   static bool flag;
};

bool A::flag;

Last edited on
No I'm not totally sure. I'm just not sure what the alternatives are.

I guess I could make it global, or just have a class for holding flags.
Is there a typical way flags are usually handled?
Your understanding of static variables is correct.

You need to initialise the static variables in your cpp file

for example
 
int myclass::mystaticvar = 0;


without doing this the static variable is not created.

you can access the static variables from any member (including constructors) of the class.

sometimes you need to fully qualify the static variables name when accessing it
 
myclass::mystaticvar = 10;

(sometimes you don't need the full qualification - not sure about when)

I think your plans are OK

This class would not be good for multithreading - singlethreading OK
Last edited on
Topic archived. No new replies allowed.