Function Pointers to a Member, from a Member
I have a problem with function pointers. Essentially, i am trying to create a function pointer from a member function of a class to another member function of the same class.
Things is, my compiler keeps on telling me "must use .* or ->* to call pointer-to-member function in `pFunct".
Code:
/*\
`* Draw - Doublebox Part Drawer
* Draws a double-line box char. Takes a integer from 1-9 and uses a switch to
* determine in which direction it would be on the keypad - ie, 1 would give the lower left corner, and so on.
* Arguments : Amount of chracter to draw, Part to draw
* Returns : Null.
\*/
int Draw::DoubleBox(int ArgDrawAmount, int ArgPart)
{
int Part = ArgPart;
int DrawAmount = ArgDrawAmount;
switch (Part)
{
case 1 : Symbol(DrawAmount, 200);
break;
case 2 : Symbol(DrawAmount, 205);
break;
case 3 : Symbol(DrawAmount, 188);
break;
case 4 : Symbol(DrawAmount, 186);
break;
case 5 : Symbol(DrawAmount, 206);
break;
case 6 : Symbol(DrawAmount, 186);
break;
case 7 : Symbol(DrawAmount, 201);
break;
case 8 : Symbol(DrawAmount, 205);
break;
case 9 : Symbol(DrawAmount, 187);
break;
default: std::cout << "Error in Draw::DoubleBox\n";
break;
}
return 0;
}
int Draw::CursorLocation( int ArgX, int ArgY )
{
int X = ArgX;
int Y = ArgY;
COORD GoToCoordinate = { X , Y };
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition (hConsole, GoToCoordinate);
return 0;
}
/*\
* Draw Box
* Draws a box at an arbitrary location, of arbitrary size.
* Takes :Location Varibles and Size Varibles (in X and Y format),
* thickness variable (0 for single line, 1 for double line)
* Returns :null
\*/
int Draw::Box (int ArgLocX, int ArgLocY, int ArgSizeX, int ArgSizeY , bool ArgThickness)
{
int (Draw::*pFunct) (int,int) ; //Function pointer allows the function to choose whether to
if (ArgThickness == TRUE) //use DoubleBox or SingleBox, based on ArgThickness.
pFunct = &Draw::DoubleBox;
if (ArgThickness == FALSE)
pFunct = &Draw::SingleBox;
//checks to see if the resulting box is legal (ie fits on a 80 X 25 char screen, at least 2x2)
if (ArgLocX + ArgSizeX + 1 > 81)
return 0;
if (ArgLocY + ArgSizeY + 1 > 26)
return 0;
if (ArgSizeX < 2 || ArgSizeY < 2)
return 0;
CursorLocation(ArgLocX, ArgLocY); //next four lines define the top of the box
pFunct(1,7);
pFunct( (ArgSizeX - 2) , 8 );
pFunct(1,9);
int i; //counting var
for ( i = 1 ; i <= (ArgSizeY - 2) ; i++ )
{
CursorLocation ( ArgLocX, ( ArgLocY + i ) );
pFunct( 1 , 4 );
CursorLocation ( (ArgLocX + ArgSizeX - 1),(ArgLocY + i ) );
pFunct( 1 , 4 );
}
CursorLocation( ArgLocX, ( ArgLocY + ArgSizeY - 1) ); //next four lines define the bottom of the box
pFunct(1,1);
pFunct( (ArgSizeX - 2) , 8 );
pFunct(1,3);
return 0;
}
the singlebox function is identical to doublebox, but with a different set of ascii codes.
Draw::Symbol just uses cout << (char) *asciicode* to ..... draw a symbol.
i was thinking about using a global function (not in a class) to feed the function the appropriate pointer, but thought that it would be a bit cumbersome. Also i thought about using an IF statement, duplicating the code and replacing pFunct with the actual function name, but i really want to know :) .... sure you all understand.