954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Create static variable in non static member function

How it is useful to create a static variable in a non static member function? The compiler allows that, and i wonder in what situations it is useful to create static variables in the function bodies as they are going to be disposed at the end of the function call.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Why do you say static variables will be disposed of at the end of the function call?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 
Why do you say static variables will be disposed of at the end of the function call?


I said that part inadvertently, i should question it with my inner voice, but anyway what i wonder is how can i make use of static variable in a non static member function?

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

I'm guessing it would just act like a static variable. I wouldn't make use of it at all -- put any static constants you have in your class definition and don't use static variables.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

The ability to have static variables in the middle of functions is a crufty feature left over from the old days of C.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

I don't agree. I think that they have a place in modern code.
I have a few situtation that I think merit them. (as will all good things, go easy on them, even in my biggest code bases, there may only be 10 of them).

Let us consider a function in a geometric matrix class, that say gets a point P and finds the appropiate cell which P exists in, and returns that integer.

That function requires a bit of optimization (particularly for non-orthoganal/non-space-filling cells) and it is found that about 50% of the time the cell is the same as the last search. (P didn't move much). So add a static variable to the method, and record the cell found.

Why not add it as a class static variable? Well that adds a level of scope pollution. Why have a variable in the class, and have to have initializers etc, have someone inadvertently change it. It is a similar argument to declaring variables local to the method, in the class.
you simple would not do it, so I don't see how it is different for a static variable.

I fully agree that if the optimization starts getting a bit more sophisticated, the static variable might need to go to the class level, or be registered to an instance of the class etc, or become a complex monster :)

I also like them when some resource is required on something since the static variable in only initialized on first call to the function, but class static variables are initialized before a call to main.

p.s. N.B there are not static constants, there are constants or static. (if it is constant you don't need static)

StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
 

A better solution would just to make that variable a member of the class. Yours would require extra code to check that the same class is receiving the call as before. And it wouldn't be thread safe.

But okay, unnoticeable static variables are pretty reasonable. C++ makes doing things a more safe way (regarding risk of bugs) so awfully verbose that I can understand.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

If a non-static variable is required, it is either local to the method or local to the class. Fine, no problem 99% of variables are just that.

But when a static variable is required, then that is completely different. This way ALL calls to the method regardless of instance have the same static variable. That is typical for specialization of event type in a monte-carlo, base-class has getCell and specialization class knows about the event that can take place, the new energy/velocity of the particle.

StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
 
But when a static variable is required, then that is completely different. This way ALL calls to the method regardless of instance have the same static variable.

Yes, we know what a static variable is.That is typical for specialization of event type in a monte-carlo, base-class has getCell and specialization class knows about the event that can take place, the new energy/velocity of the particle.

I have no clue what you're talking about.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You