Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. static min max values from object
Qt 6.12 LTS is out! See what's new in the release blog

static min max values from object

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on last edited by
    #1

    Hello,

    i would like to achieve that my class has a static NumMin and numMax,

    so A::NumMin and A::numMax keep track of the smallest and biggest values of all objects of the same class.
    it should work sofar i can, but it doesn't

    class A
    {
      public:
        static int counter; // object counter
        static int numMin;
        static int numMax;
    
        int max = numeric_limits<int>::min() ;
        int min = numeric_limits<int>::max() ;
    
        int ones = 0;
        A()
        {
          counter++;
    
        }
        virtual ~A()
        {
          counter--;
        }
    
       void somefunction(int temp){
       
            setLargestNumber(temp);
        }
    
        void setLargestNumber(int temp){
            //std::cout << "temp: " << temp << std::endl; // always holds the correct value to check
            if( temp>max ) max = temp;
            if( temp<min ) min = temp;
            //std::cout << "min: " << min << "Max:" << max << std::endl;
            numMin = min; 
            numMax = max;
        }
    };
    
    int A::counter = 0;
    int A::numMin = 0;
    int A::numMax = 0;
    

    this is the orginal code:

    #include <iostream>
    #include <limits>
    using namespace std;
    
    int main()
    {
        int max = numeric_limits<int>::min() ;
        int min = numeric_limits<int>::max() ;
        int num ;
    
        std::cout << "min: " << min << "Max:" << max << std::endl;
    
        while( cout<<"Enter the number <any non-numeric character to end input> " &&
               cin >> num )
        {
            if( num>max ) max = num;
            if( num<min ) min = num;
        }
    
        cout << "largest number is: " << max << '\n'
             << "smallest number is: " << min << '\n' ;
    }
    

    the original code taken from http://www.cplusplus.com/forum/beginner/115708/works perfectly fine @ http://cpp.sh/

    JonBJ 1 Reply Last reply
    0
    • N Natural_Bugger

      Hello,

      i would like to achieve that my class has a static NumMin and numMax,

      so A::NumMin and A::numMax keep track of the smallest and biggest values of all objects of the same class.
      it should work sofar i can, but it doesn't

      class A
      {
        public:
          static int counter; // object counter
          static int numMin;
          static int numMax;
      
          int max = numeric_limits<int>::min() ;
          int min = numeric_limits<int>::max() ;
      
          int ones = 0;
          A()
          {
            counter++;
      
          }
          virtual ~A()
          {
            counter--;
          }
      
         void somefunction(int temp){
         
              setLargestNumber(temp);
          }
      
          void setLargestNumber(int temp){
              //std::cout << "temp: " << temp << std::endl; // always holds the correct value to check
              if( temp>max ) max = temp;
              if( temp<min ) min = temp;
              //std::cout << "min: " << min << "Max:" << max << std::endl;
              numMin = min; 
              numMax = max;
          }
      };
      
      int A::counter = 0;
      int A::numMin = 0;
      int A::numMax = 0;
      

      this is the orginal code:

      #include <iostream>
      #include <limits>
      using namespace std;
      
      int main()
      {
          int max = numeric_limits<int>::min() ;
          int min = numeric_limits<int>::max() ;
          int num ;
      
          std::cout << "min: " << min << "Max:" << max << std::endl;
      
          while( cout<<"Enter the number <any non-numeric character to end input> " &&
                 cin >> num )
          {
              if( num>max ) max = num;
              if( num<min ) min = num;
          }
      
          cout << "largest number is: " << max << '\n'
               << "smallest number is: " << min << '\n' ;
      }
      

      the original code taken from http://www.cplusplus.com/forum/beginner/115708/works perfectly fine @ http://cpp.sh/

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Natural_Bugger said in static min max values from object:

      it should work sofar i can, but it doesn't

      What doesn't work? How & when do you create new A instances and use them?

      If you call this more than once, be aware that second time round numMin & numMax --- because they are static --- will start off with their values from last time round where you went numMin = min; numMax = max;. However, your code sets those two variables in those two assignment statements, but (unlike the code you copied from) you do not actually use those two values anywhere. I don't know if either of these observations is relevant to your issue.

      N 2 Replies Last reply
      1
      • JonBJ JonB

        @Natural_Bugger said in static min max values from object:

        it should work sofar i can, but it doesn't

        What doesn't work? How & when do you create new A instances and use them?

        If you call this more than once, be aware that second time round numMin & numMax --- because they are static --- will start off with their values from last time round where you went numMin = min; numMax = max;. However, your code sets those two variables in those two assignment statements, but (unlike the code you copied from) you do not actually use those two values anywhere. I don't know if either of these observations is relevant to your issue.

        N Offline
        N Offline
        Natural_Bugger
        wrote on last edited by Natural_Bugger
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • JonBJ JonB

          @Natural_Bugger said in static min max values from object:

          it should work sofar i can, but it doesn't

          What doesn't work? How & when do you create new A instances and use them?

          If you call this more than once, be aware that second time round numMin & numMax --- because they are static --- will start off with their values from last time round where you went numMin = min; numMax = max;. However, your code sets those two variables in those two assignment statements, but (unlike the code you copied from) you do not actually use those two values anywhere. I don't know if either of these observations is relevant to your issue.

          N Offline
          N Offline
          Natural_Bugger
          wrote on last edited by
          #4

          @JonB
          thnx, i got it working now, i had to do some minor improvements and pass the tests.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt
          • Unsolved