Static variable in non-static member function?
Hello, new here and hope to get some help on this problem.
I need to have a variable in a member function of a class that maintains its value throughout the lifetime of each instance of the class. If I understand correctly, if the member function is not static, then each instance of the class will have its own copy of the function. If so then all I needed to do is to declare a static variable in that member function. But when I ran the code it did not work. The static variable seems to be shared by all instances of the class, even though it is inside a non-static member function. Maybe I just misunderstand the whole concept. Please help!
In Brief: How do I make a variable inside a member function static but not shared among all the instances of the class (each instance has its own copy)?
Thank you!
Re: Static variable in non-static member function?
Quote:
Originally Posted by
acppdummy
In Brief: How do I make a variable inside a member function static but not shared among all the instances of the class (each instance has its own copy)?
If you did that, then the variable is not static. A member variable where each instance has its own copy is by definition non-static.
What high-level problem are you trying to solve? Maybe mentioning what you're trying to solve would help giving you alternatives.
Regards,
Paul McKenzie
Re: Static variable in non-static member function?
It sounds like you want a simple, non-static class member?
Re: Static variable in non-static member function?
Quote:
Originally Posted by
Lindley
It sounds like you want a simple, non-static class member?
Why didn't I think of that? I will give it a try. Thanks!
Re: Static variable in non-static member function?
Quote:
Originally Posted by
Paul McKenzie
If you did that, then the variable is not static. A member variable where each instance has its own copy is by definition non-static.
What high-level problem are you trying to solve? Maybe mentioning what you're trying to solve would help giving you alternatives.
Regards,
Paul McKenzie
Thanks! Note I was not talking about a member variable, rather a variable inside a member function.
The high-level problem is a simple one: Read in a sequence of numbers and display the current number and the min and max value read so far. The variable I was having problem with was a bool type that tells the function whether it is being called for the very first time (i.e. reading in the very first number). If true then initialize the min and max to the current value.
Re: Static variable in non-static member function?
Quote:
Originally Posted by
Lindley
It sounds like you want a simple, non-static class member?
Quote:
Originally Posted by
acppdummy
Why didn't I think of that? I will give it a try. Thanks!
Thinking about it again, it is really preferred to have the variable encapsulated inside the member function as opposed to hanging outside the function though.
Re: Static variable in non-static member function?
Quote:
Originally Posted by
acppdummy
The high-level problem is a simple one: Read in a sequence of numbers and display the current number and the min and max value read so far. The variable I was having problem with was a bool type that tells the function whether it is being called for the very first time (i.e. reading in the very first number). If true then initialize the min and max to the current value.
If mostly find it easier to initialize the minimum to the largest possible value and the maximum to the smallest possible value. You can use std::numeric_limits to get these values.
Re: Static variable in non-static member function?
Quote:
Originally Posted by
acppdummy
The variable I was having problem with was a bool type that tells the function whether it is being called for the very first time (i.e. reading in the very first number). If true then initialize the min and max to the current value.
Consider whether it might be better to change the design so that you read in everything first, and only then compute mins and maxes using std::min_element, std::max_element, or std::minmax_element.
However, if you wish to keep your current design but want to make a variable visible only to this one function that's easy enough: create a struct or class to live inside your class which contains a single bool private member, and have it implement the method. You can optionally add the method to your outer class's public interface and just pass it along if you like.
Another possibility would be maintaining a count of the number of elements read so far. This is general enough that it might make sense to have it be visible outside the reading function, but could also be tested for whether or not it's 0 inside the reading function.
Re: Static variable in non-static member function?
Quote:
Originally Posted by
D_Drmmr
If mostly find it easier to initialize the minimum to the largest possible value and the maximum to the smallest possible value. You can use std::numeric_limits to get these values.
Thanks! At first this looked like a good solution but then I ran into a minor issue: The min and max values are displayed as fixed point numbers and when they were set to max possible value the string buffer overran and crashed the program. Good idea though.
Re: Static variable in non-static member function?
Quote:
Originally Posted by
Lindley
Consider whether it might be better to change the design so that you read in everything first, and only then compute mins and maxes using std::min_element, std::max_element, or std::minmax_element.
However, if you wish to keep your current design but want to make a variable visible only to this one function that's easy enough: create a struct or class to live inside your class which contains a single bool private member, and have it implement the method. You can optionally add the method to your outer class's public interface and just pass it along if you like.
Another possibility would be maintaining a count of the number of elements read so far. This is general enough that it might make sense to have it be visible outside the reading function, but could also be tested for whether or not it's 0 inside the reading function.
Thanks! The min and max need to be displayed "real time" so I can't wait till the end. I ended up just leave the variable outside the function as you suggested originally - it's ugly but it works. Thanks again!
Re: Static variable in non-static member function?
Quote:
Originally Posted by
acppdummy
Thanks! At first this looked like a good solution but then I ran into a minor issue: The min and max values are displayed as fixed point numbers and when they were set to max possible value the string buffer overran and crashed the program. Good idea though.
That's why you shouldn't use C functions for this unless you really have to. If you use a stringstream, there's no way to cause a buffer overrun.
Code:
#include <sstream>
#include <iostream>
template <class T>
std::string ToStrFixedPoint(T value, size_t decimals)
{
std::ostringstream oss;
oss.precision(decimals);
oss << std::fixed << value;
return oss.str();
}
int main()
{
double lf = 0.123456;
std::cout << ToStrFixedPoint(lf, 3);
}
(Note that I didn't compile this code, so it may contain errors.)
However, consider whether it makes sense to display the min/max when no value has been processed yet.
Re: Static variable in non-static member function?
Quote:
Originally Posted by
D_Drmmr
That's why you shouldn't use C functions for this unless you really have to. If you use a stringstream, there's no way to cause a buffer overrun.
Code:
#include <sstream>
#include <iostream>
template <class T>
std::string ToStrFixedPoint(T value, size_t decimals)
{
std::ostringstream oss;
oss.precision(decimals);
oss << std::fixed << value;
return oss.str();
}
int main()
{
double lf = 0.123456;
std::cout << ToStrFixedPoint(lf, 3);
}
(Note that I didn't compile this code, so it may contain errors.)
However, consider whether it makes sense to display the min/max when no value has been processed yet.
Great points! Thank you! :)