Skip to main content
GameDev.net gamedev.net
🔒 Locked

Function Pointers

Started by Fredric Mar 26, 2000 at 9:28 AM 7 replies 450+ views
Original Post
Fredric
Fredric
I am having trouble understanding the following code which is from my C++ book: // using function pointers #include ''iostream.h'' void Square(int&, int&); void Cube(int&, int&); void Swap(int&, int&); void GetVals(int&, int&); void PrintVals(int, int); enum BOOL { FALSE, TRUE }; int main() { void (* pFunc) (int &, int &); // pFunc is a pointer function BOOL fQuit = FALSE; // program is indeed running int valOne =1, valTwo = 2; int choice; while(fQuit == FALSE) { cout << "(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: "; cin >> choice; switch(choice) { case 1: pFunc = GetVals; break; case 2: pFunc = Square; break; case 3: pFunc = Cube; break; case 4: pFunc = Swap; break; default: fQuit = TRUE; return (0); break; } if(fQuit) break; PrintVals(valOne, valTwo); pFunc(valOne, valTwo); PrintVals(valOne, valTwo); } return (0); } void PrintVals(int x, int y) { cout << "x: " << x << " y: " << y << endl; } void Square(int & rX, int & rY) { rX *= rY; rY *= rY; } void Cube (int & rX, int & rY) { int temp; temp = rX; rX *= rX; rX = rX * temp; temp = rY; rY *= rY; rY = rY * temp; } void Swap(int & rX, int & rY) { int temp; temp = rX; rX = rY; rY = temp; } void GetVals(int & rValOne, int & rValTwo) { cout << "New value for ValOne: "; cin >> rValOne; cout << "New value for ValTwo: "; cin >> rValTwo; } I''m confused with the prototypes where Square, Cube etc. all have int & parameters. Does this mean that the parameters are integers, but when the variables are actually declared when I type the functions, that the & is actually the reference operator? EX: Prototype: Square(int &, int &); Function: Square(int & rX, int & rY) does that and/references operatore declare rX and rY as references? My second problem is the *= operator. I''ve never used that in my code, and the author doesn''t explicitely explain it''s purpose or what it does. Could anyone help me out here? My THIRD problem is where the line of code, rX = rX * temp; is the * a multiplication sign, or a pointer sign? I don''t think it could be a pointing sign because rX is a reference (or is it?) and references should never be reassigned. That''s my last question... sorry for keeping it so long, but could anyone please help? Programming::~Fredric(const Annoy_Ance)
3D Math- The type of mathematics that'll put hair on your chest!
DerekSaw
DerekSaw
yes, rX and rY are references, and they refer to valOne and valTwo in your main()

a *= a; // same as a = a * a -> a = a multiply a

here, rX can be reassign...
rX = rX * temp; // same as valOne = valOne * temp;


Hopes that help.
"after many years of singularity, i'm still searching n the event horizon" o
bcj
bcj
the type ∫ means it is a reference to an int. if you pass just an int then the function has its own copy of the variable (called passing by value) with local scope which means it won''t affect the variable outside of the function. if the functions in your program were only ints the values of valOne and valTwo would never change.

but when it is passed a reference it has wider scope (includes the calling function) and will actually change the variable''s value in the calling function.
you can think of it as way to return more than one variable from your functions instead of just one when you do a normal return.

DerekSaw answered the other two ?''s

bcj

Fredric
Fredric
Thanks you too for both your responses, but I''m still a little queesy on the *= operator... how does it work?

say for example, x = 2

x *= x; // what would this equal, and can anyone give me a full description of how this works? Is it basically x * x? This is still confusing to me after Derek''s explanation...

Programming::~Fredric(const Annoy_Ance)
3D Math- The type of mathematics that'll put hair on your chest!
Aragorn
Aragorn
There is not only the *= but also the += , -= and /= operators. They work like this:

a += b is the same as a = a + b
a -= b is the same as a = a - b
and so on...
Example:
int a = 0;
int b = 6;
a += 6; //a is 6
a /= 3; //a is 2
a *= b; //a is 12
Hope this will help you.
May the force be with you
Aragorn
Aragorn
Sorry I forgot, ther is also the %= operator.
May the force be with you
Muzzafarath
Muzzafarath
There are also the &= ^= /= operators which perform different functions on bits.

/. Muzzafarath
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
SiCrane
SiCrane
The /= in Muzzafarath''s last post was a [pipe]=. But got destroyed in forum mangling. And there''s also <<= and >>=.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.