| | |
Pointer Function problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
Inside another class I have this
In the constructor I tried to assign the function to its function
pointer like so :
But this gives me a conversion error. How could I pass the function
correctly to setDrawFunc
much. Here is the class definition for which it resides, although its
only part of it.
C++ Syntax (Toggle Plain Text)
template<typename Type> class vec2D { private: bool (*drawFunc)(unsigned int ID); unsigned int Col_id; public: //Enables each object member to have its own unique draw func. void setDrawFunc( bool(*Pf)(unsigned int) ) { drawFunc = &Pf; } ///some more stuff } };
Inside another class I have this
C++ Syntax (Toggle Plain Text)
class AnotherClass { enum BlockType { LINE,SQUARE, ZSHAPE, LSHAPE, TSHAPE, CSHAPE, MAX_SHAPE }; vec2D<float> ShapePos[MAX_SHAPE]; bool myCheckValidID(unsigned int Col_Id); bool drawLine(unsigned int Color_ID); bool drawSquare(unsigned int Color_ID); bool drawZShape(unsigned int Color_ID); bool drawLShape(unsigned int Color_ID); bool drawTShape(unsigned int Color_ID); public : AnotherClass(); };
In the constructor I tried to assign the function to its function
pointer like so :
C++ Syntax (Toggle Plain Text)
AnotherClass::AnotherClass(){ //For refrence // enum BlockType { LINE,SQUARE, ZSHAPE, LSHAPE, TSHAPE, CSHAPE, MAX_SHAPE }; ShapePos[LINE].setDrawFunc(&Tetris::drawLine); }
But this gives me a conversion error. How could I pass the function
correctly to setDrawFunc
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
I think you are trying to acheive the below one.
basically you are declarnig pointer to function and storing pointer to member function.
cpp Syntax (Toggle Plain Text)
template<class T> class Foo { typedef void (T::*MFPtr)(int id); MFPtr funcPtr; public: void SetDrawFunction(MFPtr memFunctionPtr) { this->funcPtr = memFunctionPtr; } void Draw(T obj, const int Id) { return (obj.*funcPtr)(Id); } }; class Bar { public: void DoIt(int id) { cout <<"Ahaan I am Called from Foo"<<endl; cout << "Value of my Id is : "<<id <<endl; } }; int main () { Foo<Bar> f; f.SetDrawFunction(&Bar::DoIt); Bar b; f.Draw(b, 4); return 0; }
basically you are declarnig pointer to function and storing pointer to member function.
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)
#include <iostream> using namespace std; class Tetris{ public: bool static drawLine(unsigned int p){ cout << "\nTest " << p; return true; } }; template<typename Type> class vec2D{ private: bool (*drawFunc)(unsigned int ID); unsigned int Col_id; public: //Enables each object member to have its own unique draw func. void setDrawFunc( bool( *Pf)(unsigned int) ) { drawFunc = Pf; (*Pf)(22); //invoke } }; class AnotherClass { enum BlockType { LINE,SQUARE, ZSHAPE, LSHAPE, TSHAPE, CSHAPE, MAX_SHAPE }; vec2D<float> ShapePos[MAX_SHAPE]; bool drawLine(unsigned int Color_ID); public : AnotherClass() { ShapePos[LINE].setDrawFunc(&Tetris::drawLine); } }; int main(){ AnotherClass p; return 0; }
Failure is not fatal, but failure to change might be. - John Wooden
hey Laiq, I tried your implementation but this gives me an error:
its just a variation of your code :
and why is your code returning when its of type void.
The error :
C++ Syntax (Toggle Plain Text)
void drawShape(vec2D<Type>& obj,const unsigned int Col_ID) { (obj.*mydrawFuncPtr)(Col_ID); }
its just a variation of your code :
C++ Syntax (Toggle Plain Text)
void Draw(T obj, const int Id) { return (obj.*funcPtr)(Id); }
The error :
C++ Syntax (Toggle Plain Text)
error C2440: 'newline' : cannot convert from 'vec2D<Type> *' to 'Tetris *'
Last edited by firstPerson; Aug 11th, 2009 at 1:43 pm.
Here is the definition :
And this is where its called
C++ Syntax (Toggle Plain Text)
template<typename Type> class vec2D { typedef bool (Tetris::*MFPtr)(unsigned int ID); MFPtr mydrawFuncPtr; private: unsigned int Col_id; public: Type x; Type y; vec2D(Type x0,Type y0) : x(x0), y(y0) { } vec2D() : x(0),y(0){ mydrawFuncPtr = 0; } void reset() { x = 0; y = 0; } //Enables each object member to have its own unique draw func. void setDrawFunc(MFPtr drawFunc ) { mydrawFuncPtr = drawFunc;} //get-set methods unsigned int Color() { return Col_id;} void Color(int i) { if(i < 0) Col_id = 0; else Col_id = i; } void drawShape(vec2D<Type>& obj,unsigned int colID) { (obj.*mydrawFuncPtr)(colID); } };
And this is where its called
C++ Syntax (Toggle Plain Text)
Tetris::Tetris() { //For refrence //enum BlockType { LINE,SQUARE, ZSHAPE, LSHAPE, TSHAPE, CSHAPE, MAX_SHAPE }; ShapePos[LINE].setDrawFunc(&Tetris::drawLine); ShapePos[0].drawShape(*ShapePos,2); }
Here is a useful link - Functors to encapsulate C and C++ Function Pointers
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
Hi FirstPerson,
I've tweaked your code to meet your needs, I think you are trying to acheive something like this.
Hope the Above Code help
I've tweaked your code to meet your needs, I think you are trying to acheive something like this.
C++ Syntax (Toggle Plain Text)
class Tetris { public: bool DrawLine(unsigned int iColId) { cout << "Draw Line of Tetris"<<endl; return true; } }; template<typename Type> class vec2D { typedef bool (Tetris::*MFPtr)(unsigned int ID); MFPtr mydrawFuncPtr; private: unsigned int Col_id; public: Type x; Type y; vec2D(Type x0,Type y0) : x(x0), y(y0) { } vec2D() : x(0),y(0){ mydrawFuncPtr = 0; } void reset() { x = 0; y = 0; } //Enables each object member to have its own unique draw func. void setDrawFunc(MFPtr drawFunc ) { mydrawFuncPtr = drawFunc;} //get-set methods unsigned int Color() { return Col_id;} void Color(int i) { if(i < 0) Col_id = 0; else Col_id = i; } // WRONG. //void drawShape(vec2D<Type>& obj,unsigned int colID) //CORRECTED: void drawShape(Tetris& obj,unsigned int colID) { (obj.*mydrawFuncPtr)(colID); } }; int main () { vec2D<int> vec2DTetris[10]; // use this array. vec2DTetris[0].setDrawFunc(&Tetris::DrawLine); Tetris t; vec2DTetris[0].drawShape(t, 4); return 0; }
Hope the Above Code help
![]() |
Similar Threads
- c language problm, how to pass pointer to a function (C)
- Incompatible pointer type problem (C)
- Problem with pointer-to-function (C++)
- pointer passing problem (C)
- what is pointer to function ??? (C)
- pointer to function (C)
Other Threads in the C++ Forum
- Previous Thread: Make a for loop, loop until eof
- Next Thread: typedef enum
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






