Function Pointers to a Member, from a Member

This is a discussion on Function Pointers to a Member, from a Member within the C++ Programming forums, part of the General Programming Boards category; I have a problem with function pointers. Essentially, i am trying to create a function pointer from a member function ...

  1. #1
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68

    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.
    Last edited by littleweseth; 11-01-2003 at 12:18 AM.
    Ph33r the sphericalCUBE

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,701
    The syntax is:

    (*this.*pFunct)(10, 20);

    or

    (this->*pFunct)(10, 20);

    I usually use a macro:

    #define THIS(member) (*this.*member)

    ie:

    THIS(pFunct)(10, 20);
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    doesn't seem to work - is it supposed to be

    int (*this->*pFunct) (int,int) ;

    as the declaration? Maybe its my compiler - im using Dev-C++ 4.9.8.4 beta.
    Ph33r the sphericalCUBE

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,701
    Your declaration was fine. What I gave you was the syntax for *calling* a member function.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    thanks - got it now *eternal smile*

    aslo kudos for putting up with my n00b stupidity.
    Ph33r the sphericalCUBE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21