Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to do a pointer to a function into a class?

Status
Not open for further replies.

LorienCat

Programmer
Jan 29, 2002
3
ES
Hehe..... one question, how to do pointer to a function into a c++ class?

I tried:

class lala
{
int f1(int, int);
int f2(int, int);
int (lala::*pf)(int, int);
};

int lala::f1(int a, int b)
{
return a+b;
}

int lala::f2(int a, int b)
{
return a-b;
}

void main()
{
lala l;
int a = 5, b = 6, c, d;

l.pf = l.f1;

c = l.pf(a, b); //ERR: term does not evaluate to a function

l.pf = l.f2;

d = l.pf(a, b); //ERR: term does not evaluate to a function

printf("Res: %d %d\n", c, d);
}

TXS!!!!
 
I've found the solution... the solution was:
(this->*pf)(a, b);

XD

I put all code here:

#include <stdio.h>

class lala
{
public:
int f1(int, int);
int f2(int, int);
int (lala::*pf)(int, int);
int call(int, int);
};

int lala::f1(int a, int b)
{
return a+b;
}

int lala::f2(int a, int b)
{
return a-b;
}

int lala::call(int a, int b)
{
return (this->*pf)(a, b);
}

void main()
{
lala *l;

l = new lala;
int a = 5, b = 6, c, d;

l->pf = l->f1;

c = l->call(a, b);

l->pf = l->f2;

d = l->call(a, b);

printf(&quot;Res: %d %d\n&quot;, c, d);
}
 
Here is what I posted a while back... Should be able to just copy it into a program and execute it

Matt

Code:
#include <iostream.h>

class c1{
public:
	c1::c1(){}
	void sample1();
	void sample2();
	void sample3();
	void sample4();
	void sample5();
	void sample6();

};

void c1::sample1(){cout<<&quot;sample1\n&quot;;}
void c1::sample2(){cout<<&quot;sample2\n&quot;;}
void c1::sample3(){cout<<&quot;sample3\n&quot;;}
void c1::sample4(){cout<<&quot;sample4\n&quot;;}
void c1::sample5(){cout<<&quot;sample5\n&quot;;}
void c1::sample6(){cout<<&quot;sample6\n&quot;;}

class c2{
public:
	c2::c2( void(c1::*ptr)() ):method_ptr(ptr){};
	void setmethod(void(c1::*ptr)());
	void fire();
private:

	void (c1::*method_ptr)();  
};


class c3{
public:
	c3::c3():method_ptr(fxna){};
	void setmethod(int x);
	void fxna(){cout<<&quot;fxna\n&quot;;}
	void fxnb(){cout<<&quot;fxnb\n&quot;;}
	void fxnc(){cout<<&quot;fxnc\n&quot;;}
private:

	void (c3::*method_ptr)();  
};

void c2::fire(){c1 temp;(temp.*method_ptr)();}
void c2::setmethod(void(c1::*ptr)()){method_ptr=ptr;}
void c3::setmethod(int x){
	switch(x)
	{ 
	case 1:method_ptr = fxna;break;
	case 2:method_ptr = fxnb;break;
	case 3:
	default:method_ptr = fxnc;break;
	}
}


int main()
{
	c2 mc2(&c1::sample1);

	mc2.fire();
	mc2.setmethod(&c1::sample2);
	mc2.fire();
	mc2.setmethod(&c1::sample3);
	mc2.fire();
	mc2.setmethod(&c1::sample4);
	mc2.fire();
	mc2.setmethod(&c1::sample5);
	mc2.fire();
	mc2.setmethod(&c1::sample6);
	mc2.fire();

	c3 mc3;

	cout<<&quot;\n\n\n\nbeginning\n\n\n\n&quot;;
	mc3.setmethod(1);
	mc3.setmethod(2);
	mc3.setmethod(3);



	return 0;
}



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top