Pointer Function problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 1,107
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 142
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Pointer Function problem

 
0
  #1
Aug 11th, 2009
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.

  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

  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 :

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Pointer Function problem

 
0
  #2
Aug 11th, 2009
I think you are trying to acheive the below one.

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,582
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 457
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Pointer Function problem

 
0
  #3
Aug 11th, 2009
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.
  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. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,107
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 142
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Pointer Function problem

 
0
  #4
Aug 11th, 2009
hey Laiq, I tried your implementation but this gives me an error:
  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 :
  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 :
  1. error C2440: 'newline' : cannot convert from 'vec2D<Type> *' to 'Tetris *'
Last edited by firstPerson; Aug 11th, 2009 at 1:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,107
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 142
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Pointer Function problem

 
0
  #5
Aug 11th, 2009
Here is the definition :
  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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,582
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 457
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Pointer Function problem

 
0
  #6
Aug 11th, 2009
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Pointer Function problem

 
1
  #7
Aug 12th, 2009
Hi FirstPerson,

I've tweaked your code to meet your needs, I think you are trying to acheive something like this.
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,107
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 142
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Pointer Function problem

 
0
  #8
Aug 12th, 2009
Hey thanks Laiq. ++Reps to you.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC