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

I think you are trying to acheive the below one.

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.

#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;
}

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 *'

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);
	
}

Hi FirstPerson,

I've tweaked your code to meet your needs, I think you are trying to acheive something like this.

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

This article has been dead for over six months. Start a new discussion instead.