Static variable in non-static member function?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Static variable in non-static member function?

  1. #1
    Join Date
    Nov 2010
    Posts
    105

    Question 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!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Static variable in non-static member function?

    Quote Originally Posted by acppdummy View Post
    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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Static variable in non-static member function?

    It sounds like you want a simple, non-static class member?

  4. #4
    Join Date
    Nov 2010
    Posts
    105

    Smile Re: Static variable in non-static member function?

    Quote Originally Posted by Lindley View Post
    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!

  5. #5
    Join Date
    Nov 2010
    Posts
    105

    Re: Static variable in non-static member function?

    Quote Originally Posted by Paul McKenzie View Post
    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.

  6. #6
    Join Date
    Nov 2010
    Posts
    105

    Re: Static variable in non-static member function?

    Quote Originally Posted by Lindley View Post
    It sounds like you want a simple, non-static class member?
    Quote Originally Posted by acppdummy View Post
    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.

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Static variable in non-static member function?

    Quote Originally Posted by acppdummy View Post
    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Static variable in non-static member function?

    Quote Originally Posted by acppdummy View Post
    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.

  9. #9
    Join Date
    Nov 2010
    Posts
    105

    Re: Static variable in non-static member function?

    Quote Originally Posted by D_Drmmr View Post
    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.

  10. #10
    Join Date
    Nov 2010
    Posts
    105

    Re: Static variable in non-static member function?

    Quote Originally Posted by Lindley View Post
    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!
    Last edited by acppdummy; November 12th, 2010 at 10:54 PM.

  11. #11
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Static variable in non-static member function?

    Quote Originally Posted by acppdummy View Post
    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  12. #12
    Join Date
    Nov 2010
    Posts
    105

    Re: Static variable in non-static member function?

    Quote Originally Posted by D_Drmmr View Post
    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!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center