Why the increment do not wrapps?

Hello everybody.
I would like to test the short int variable wrapping. In the main() function it works, but when i wrote another function which increment a variable and should return overloaded i mean (wrapped) short number. But it return the same limited number as i initialized.

Code ex:
----------------------------------------------------
/* short int overload */
#include <iostream>

unsigned short int ovLod(unsigned short int plius)
{
std::cout << "Took "<< plius << std::endl;

return(plius++);
}
int main()
{
unsigned short int imPeln = 65534;

std::cout << "Company profit: " << imPeln << "\n";

ovLod(imPeln);

std::cout << "Company profit from ovLod() grow up: " << imPeln << std::endl;

imPeln++;

std::cout << "Company profit from main() grow up: " << imPeln;

return 0;
}


Output:
Company profit: 65534
Took 65534
Company profit from ovLod() grow up: 65534
Company profit from main() grow up: 65534

Why not wrapping to 0. Why does not work?



Thanks.
Last edited on
Because plius is only in function scope and your returning it to nothing. You function needs to either pass the parameter in by reference or have your function call assign the return value to imPeln: imPeln = ovLod( imPeln );

Also, not sure at what point you are expecting it to roll over to 0, but an unsigned short has a range of 0 to 65535, so it's not going to roll over until you increment it twice.
On top of that, you're using the post-increment operator inside ovLod, which takes the old value of plius and increments it after that. So the old value is returned and the new one is lost.
With your help, the problem solved. And I understood where the problem was.

Thank you!
Topic archived. No new replies allowed.