Pointer Function problem
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.
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
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 :
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
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
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.
#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;
}
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
hey Laiq, I tried your implementation but this gives me an error:
void drawShape(vec2D<Type>& obj,const unsigned int Col_ID)
{
(obj.*mydrawFuncPtr)(Col_ID);
}
its just a variation of your code :
void Draw(T obj, const int Id) {
return (obj.*funcPtr)(Id);
}
and why is your code returning when its of type void.
The error :
error C2440: 'newline' : cannot convert from 'vec2D<Type> *' to 'Tetris *'
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
Here is the definition :
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
Tetris::Tetris()
{
//For refrence
//enum BlockType { LINE,SQUARE, ZSHAPE, LSHAPE, TSHAPE, CSHAPE, MAX_SHAPE };
ShapePos[LINE].setDrawFunc(&Tetris::drawLine);
ShapePos[0].drawShape(*ShapePos,2);
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
Hey thanks Laiq. ++Reps to you.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608