1

Lets say my class lets say I have

static classA myObject;

void classA::update(int elapsed)
{
  static int sumElapsed = 0;
  sumElapsed+= elapsed;

}

It seems that my questions is kind of hard to understand. But if we say that myObject is a singleton of classA. Is there a difference between the local static int sumElapsed and a private member int sumElapsed of the classA, other than the scope in which they can get accessed.

DogDog
  • 4,820
  • 12
  • 44
  • 66

5 Answers5

3

Sure. For example in the singleton pattern. There a reference (or pointer) to the static variable is also returned from a static method.


For an example see here: c++ Meyers singleton undefined reference

By the way, if you are interested: Are Singletons really that bad?

Community
  • 1
  • 1
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
0

If you don't want sumElapsed to be overwritten then yes, but it does seem like it would make more sense to have sumElapsed ecapsulated within classA as a static variable.

PAULDAWG
  • 780
  • 5
  • 21
  • Doesn't having the suMelapsed encapsulated within classA as a none-static variable would have the same effect since classA is static? – DogDog Feb 06 '12 at 21:31
  • No because I think that is a issue with scope. Static declares that the variable remains in memory. The variable would be overwritten every time since it is being set to 0 in the update function. Should only need to be initialized once. – PAULDAWG Feb 06 '12 at 21:40
  • 1
    @Apoc But you don't have `sumElapsed` encapsulated. You declare it within `update`. If you also have a class member `classA::sumElapsed`, then the local variable will be hiding it--bad practice. I think I see your confusion--the solution is to declare `sumElapsed` within `classA`, statically, and just use it within the static method as you would any member. The compiler will force the static declaration of `sumElapsed` in this case. – Matt Phillips Feb 06 '12 at 21:44
  • 1
    That's what I suggested to do, Matt. Thanks for the support. :) – PAULDAWG Feb 06 '12 at 21:57
  • Ha I was switching back and forth between windows, somehow thought that Apoc's comment was a response to my own answer. Sorry, not trying to horn in on your answer! – Matt Phillips Feb 06 '12 at 22:09
0

Effectively, when you need some sort of a static member in a class template there aren't really good alternatives in the first place. Also, if the members aren't trivial you probably want to put them into a function in the first place to have some level of control over the order of initialization.

In general, be aware that whether you put a static member into a class or into a function, it is still effectively implementing the Singleton anti-pattern!

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

Of course. If sumElapsed isn't static, it will get overwritten every time the function is called; as it is, it won't be. The fact that classA::update is itself static is irrelevant; just consider if it was a global function (e.g. in C).

Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
0

You should use a static class to hold methods that are not associated with a particular object. That being said, use a static class to hold only static members that cannot be instantiated by objects, and shouldn't be overwritten.

Ben Sewards
  • 2,571
  • 2
  • 25
  • 43