static min max values from object
-
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'tclass 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/
-
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'tclass 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/
@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 arestatic
--- will start off with their values from last time round where you wentnumMin = 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. -
@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 arestatic
--- will start off with their values from last time round where you wentnumMin = 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.This post is deleted! -
@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 arestatic
--- will start off with their values from last time round where you wentnumMin = 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.@JonB
thnx, i got it working now, i had to do some minor improvements and pass the tests.