Hi I'm currently learning C++ and I got to an exercise that asked to create a function that swaps two ints passed in by pointer. Here is that snippet of the program:
Everything works just fine, but the first time I compiled it I noticed I forgot the address of operators and called swap like this:
swap(num1, num2);
and it compiled and worked. I don't understand how the version without the ampersands can work when the function parameters are expecting a pointer to int but is allowing me to pass plain int values as arguments?
Wallboy maybe it was because you didnt use the asterics pointer the first time you compiled in your function. thats why it worked without the ampersands, however you will see the diferience with problems or algorithm much bigger why you have to use pointers to completely manage the value of the variable
Ok I think I might have an include including some other swap function or something. I just renamed my function and now it fails to compile with the error I would have expected. Would this be possible?
I'm using code:blocks btw, and yeah when I hightlight over my swap function definition it shows a bunch of other possible swap functions and also highlights the function name in green. When I made my own really unique function name there is no highlighting on it.
So I guess I was just calling another swap function that just happened to work lol. Thanks guys.