943,648 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 508
  • C++ RSS
Aug 11th, 2009
0

Pointer Function problem

Expand Post »
Can someone help me out. I haven't dealt with pointer function
much. Here is the class definition for which it resides, although its
only part of it.

C++ Syntax (Toggle Plain Text)
  1. template<typename Type>
  2. class vec2D
  3. {
  4. private:
  5. bool (*drawFunc)(unsigned int ID);
  6. unsigned int Col_id;
  7.  
  8. public:
  9. //Enables each object member to have its own unique draw func.
  10. void setDrawFunc( bool(*Pf)(unsigned int) ) { drawFunc = &Pf; }
  11.  
  12. ///some more stuff
  13. }
  14.  
  15. };

Inside another class I have this

C++ Syntax (Toggle Plain Text)
  1. class AnotherClass {
  2. enum BlockType { LINE,SQUARE, ZSHAPE, LSHAPE, TSHAPE, CSHAPE, MAX_SHAPE };
  3.  
  4. vec2D<float> ShapePos[MAX_SHAPE];
  5.  
  6. bool myCheckValidID(unsigned int Col_Id);
  7. bool drawLine(unsigned int Color_ID);
  8. bool drawSquare(unsigned int Color_ID);
  9. bool drawZShape(unsigned int Color_ID);
  10. bool drawLShape(unsigned int Color_ID);
  11. bool drawTShape(unsigned int Color_ID);
  12. public : AnotherClass();
  13. };

In the constructor I tried to assign the function to its function
pointer like so :

C++ Syntax (Toggle Plain Text)
  1. AnotherClass::AnotherClass(){
  2. //For refrence
  3. // enum BlockType { LINE,SQUARE, ZSHAPE, LSHAPE, TSHAPE, CSHAPE, MAX_SHAPE };
  4.  
  5. ShapePos[LINE].setDrawFunc(&Tetris::drawLine);
  6. }

But this gives me a conversion error. How could I pass the function
correctly to setDrawFunc
Similar Threads
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 11th, 2009
0

Re: Pointer Function problem

I think you are trying to acheive the below one.

cpp Syntax (Toggle Plain Text)
  1. template<class T>
  2. class Foo {
  3. typedef void (T::*MFPtr)(int id);
  4. MFPtr funcPtr;
  5. public:
  6. void SetDrawFunction(MFPtr memFunctionPtr) {
  7. this->funcPtr = memFunctionPtr;
  8. }
  9.  
  10. void Draw(T obj, const int Id) {
  11. return (obj.*funcPtr)(Id);
  12. }
  13. };
  14.  
  15.  
  16. class Bar {
  17.  
  18. public:
  19. void DoIt(int id) {
  20. cout <<"Ahaan I am Called from Foo"<<endl;
  21. cout << "Value of my Id is : "<<id <<endl;
  22. }
  23. };
  24.  
  25.  
  26. int main ()
  27. {
  28.  
  29. Foo<Bar> f;
  30. f.SetDrawFunction(&Bar::DoIt);
  31.  
  32. Bar b;
  33. f.Draw(b, 4);
  34.  
  35. return 0;
  36. }

basically you are declarnig pointer to function and storing pointer to member function.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 11th, 2009
0

Re: Pointer Function problem

Please elaborate your question. I don't know why you want to do this and I am not sure whether my answer will help you any. Here is a code I tested.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. class Tetris{
  4. public:
  5. bool static drawLine(unsigned int p){
  6. cout << "\nTest " << p;
  7. return true;
  8. }
  9. };
  10.  
  11. template<typename Type>
  12. class vec2D{
  13. private:
  14. bool (*drawFunc)(unsigned int ID);
  15. unsigned int Col_id;
  16.  
  17. public:
  18. //Enables each object member to have its own unique draw func.
  19. void setDrawFunc( bool( *Pf)(unsigned int) )
  20. {
  21. drawFunc = Pf;
  22. (*Pf)(22); //invoke
  23. }
  24. };
  25. class AnotherClass {
  26. enum BlockType { LINE,SQUARE, ZSHAPE, LSHAPE, TSHAPE, CSHAPE, MAX_SHAPE };
  27. vec2D<float> ShapePos[MAX_SHAPE];
  28. bool drawLine(unsigned int Color_ID);
  29. public :
  30. AnotherClass()
  31. {
  32. ShapePos[LINE].setDrawFunc(&Tetris::drawLine);
  33. }
  34. };
  35. int main(){
  36. AnotherClass p;
  37. return 0;
  38. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 11th, 2009
0

Re: Pointer Function problem

hey Laiq, I tried your implementation but this gives me an error:
C++ Syntax (Toggle Plain Text)
  1. void drawShape(vec2D<Type>& obj,const unsigned int Col_ID)
  2. {
  3. (obj.*mydrawFuncPtr)(Col_ID);
  4. }

its just a variation of your code :
C++ Syntax (Toggle Plain Text)
  1. void Draw(T obj, const int Id) {
  2. return (obj.*funcPtr)(Id);
  3. }
and why is your code returning when its of type void.

The error :
C++ Syntax (Toggle Plain Text)
  1. error C2440: 'newline' : cannot convert from 'vec2D<Type> *' to 'Tetris *'
Last edited by firstPerson; Aug 11th, 2009 at 1:43 pm.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 11th, 2009
0

Re: Pointer Function problem

Here is the definition :
C++ Syntax (Toggle Plain Text)
  1. template<typename Type>
  2. class vec2D
  3. {
  4. typedef bool (Tetris::*MFPtr)(unsigned int ID);
  5. MFPtr mydrawFuncPtr;
  6.  
  7. private:
  8. unsigned int Col_id;
  9. public:
  10. Type x;
  11. Type y;
  12.  
  13. vec2D(Type x0,Type y0) : x(x0), y(y0) { }
  14. vec2D() : x(0),y(0){ mydrawFuncPtr = 0; }
  15.  
  16. void reset() { x = 0; y = 0; }
  17.  
  18. //Enables each object member to have its own unique draw func.
  19. void setDrawFunc(MFPtr drawFunc ) { mydrawFuncPtr = drawFunc;}
  20. //get-set methods
  21. unsigned int Color() { return Col_id;}
  22. void Color(int i) { if(i < 0) Col_id = 0; else Col_id = i; }
  23.  
  24. void drawShape(vec2D<Type>& obj,unsigned int colID)
  25. {
  26. (obj.*mydrawFuncPtr)(colID);
  27. }
  28. };

And this is where its called
C++ Syntax (Toggle Plain Text)
  1. Tetris::Tetris()
  2. {
  3.  
  4. //For refrence
  5. //enum BlockType { LINE,SQUARE, ZSHAPE, LSHAPE, TSHAPE, CSHAPE, MAX_SHAPE };
  6.  
  7. ShapePos[LINE].setDrawFunc(&Tetris::drawLine);
  8. ShapePos[0].drawShape(*ShapePos,2);
  9.  
  10. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 11th, 2009
0

Re: Pointer Function problem

Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 12th, 2009
1

Re: Pointer Function problem

Hi FirstPerson,

I've tweaked your code to meet your needs, I think you are trying to acheive something like this.
C++ Syntax (Toggle Plain Text)
  1.  
  2. class Tetris {
  3. public:
  4. bool DrawLine(unsigned int iColId) {
  5. cout << "Draw Line of Tetris"<<endl;
  6. return true;
  7. }
  8. };
  9.  
  10. template<typename Type>
  11. class vec2D
  12. {
  13.  
  14. typedef bool (Tetris::*MFPtr)(unsigned int ID);
  15. MFPtr mydrawFuncPtr;
  16.  
  17. private:
  18. unsigned int Col_id;
  19. public:
  20. Type x;
  21. Type y;
  22.  
  23. vec2D(Type x0,Type y0) : x(x0), y(y0) { }
  24. vec2D() : x(0),y(0){ mydrawFuncPtr = 0; }
  25.  
  26. void reset() { x = 0; y = 0; }
  27.  
  28. //Enables each object member to have its own unique draw func.
  29. void setDrawFunc(MFPtr drawFunc ) { mydrawFuncPtr = drawFunc;}
  30.  
  31.  
  32. //get-set methods
  33. unsigned int Color() { return Col_id;}
  34.  
  35. void Color(int i) { if(i < 0) Col_id = 0; else Col_id = i; }
  36.  
  37. // WRONG.
  38. //void drawShape(vec2D<Type>& obj,unsigned int colID)
  39.  
  40. //CORRECTED:
  41. void drawShape(Tetris& obj,unsigned int colID)
  42. {
  43. (obj.*mydrawFuncPtr)(colID);
  44. }
  45. };
  46.  
  47. int main ()
  48. {
  49.  
  50. vec2D<int> vec2DTetris[10]; // use this array.
  51. vec2DTetris[0].setDrawFunc(&Tetris::DrawLine);
  52.  
  53. Tetris t;
  54. vec2DTetris[0].drawShape(t, 4);
  55.  
  56. return 0;
  57. }

Hope the Above Code help
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 12th, 2009
0

Re: Pointer Function problem

Hey thanks Laiq. ++Reps to you.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Make a for loop, loop until eof
Next Thread in C++ Forum Timeline: typedef enum





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC