static member variables

Hello again , i have a question about static variables...
i have a class called circle and a class called cylinder( which inherirates circle). i think i've a good case for using statics's.....
I want to have a static variable in each class which denotes the largest radius in each class . i call it maxRadius

class circle
{ public :
static int maxRadius;
.....

class cylinder: public circle
{ public
static int maxRadius;

and within main()

{
circle::maxRadius =3;
cylinder::maxRadius = 5;
......

I've also seen somewhere that i should precede the above statements with int ??


Mmmm, why not just

1
2
3
4
5
class circle
{
  public:
      virtual int GetMaxRadius() { return 42; }
};

Because (I believe), maxRadius is supposed to be dynamic, since it doesn't declare a limit, it tells the highest value of the radii of that type.
Last edited on
Yes, I would like the ability to change it and all objects of these classes to access this figure. I thought this would be good use of static?
I tend to be cautious about using a static for something like this. But it really depends on your situation. I naturally tend to avoid committing to a global solution if there might be a possibility that a less than global solution could be required later on.

Will all of one class always have the same max radius? Is it possible you might want to have one group of the class with one max radius and a different group with another max radius?

There are other ways for multiple objects of a given class to share characteristics. You could pass each of them a pointer to some kind of shared 'limits' class object. That way you could independently manage multiple groups of your objects, each group having different shared attributes.

However, if your needs are simple, then simple solutions are the way to go. No point in over-engineering!
What exactly is your question?
Is it the syntax? Here:
1
2
3
4
5
6
7
8
9
struct S{
    static int i;
};
int S::i;

int main(){
    S::i = 7;
    return 0;
}
Topic archived. No new replies allowed.