![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Function pointer to a member function
I'm writing Chess in what little free time I have these days, and I'm currently working on getting the pieces to calculate their possible moves. My specific problem is with pawns, which can only move in a single direction, determined by where they started (i.e. by color). Anyways, I'm trying to create a forward pointer that will get the square "in front" of where the pawn is now.
I have the line in pawn.cpp: Square (*fwd)() = (getColor() == White) ? &Square::up : &Square::down; public: /* ... */
Square* up();
Square* down();Quote:
BTW, I'm compiling using g++ and running Linux, not that it should matter for this... |
|
|
|
|
|
|
#2 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
What do you mean by forward pointer?
Square (*fwd)() = (getColor() == White) ? &Square::up : &Square::down; I'm not sure as I haven't done function pointers in a while but here goes anyway: Square* (Square::*fwd)() = (getColor() == White) ? &Square::up : &Square::down; I'd be hesitant on the function pointer in the first place. It's almost always possible to get the same functionality in a different manner. Oh and don't forget the following either: Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates Last edited by nnxion; May 12th, 2006 at 6:56 AM. Reason: missed colons |
|
|
|
|
|
|
#3 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Missing an asterisk.
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Thanks for the replies, that seems to have fixed it.
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|