Original Post
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)