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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I can't solve class calling each others function.

Status
Not open for further replies.

Timil

Programmer
May 16, 2001
17
FR
I've to class:

class A
{
public:
void start();
void stop();
void calling_function();
}


class B
{
public:
void foo(void (*start)(), void (*stop)());
}


This may seem stupid, but those are C program part transformed in classes.

My question is:
Before, only class B exist, and this work nice, the void (*start)() and void (*stop)() declaration work well when I was doing:
calling_function()
{
B.foo(start, stop) // at this time stop and start weren't class methode, but "wandering" function.
}


But now, this create an exception when I call
A::Calling_function()
{
B.foo(start, stop) // at this time stop and start weren't class methode, but "wandering" function.
}


Error:
error C2664: 'foo' : cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'
None of the functions with this name in scope match the target type


Many thanks to those who understand what I mean :p
 
You need to use method pointers in B, not function pointers
copy and paste this code and see how it works ....

Matt



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




 
By the way, C++ language is more labile. See the polymorphism:

class B;
class A
{
public:
virtual void start(){cout<<&quot;start A&quot;<<endl;}
virtual void stop(){cout<<&quot;stop A&quot;<<endl;}
void calling_function(B&);
};

class C:public A
{
public:
virtual void start(){cout<<&quot;start B&quot;<<endl;}
virtual void stop(){cout<<&quot;stop B&quot;<<endl;}
};
class B
{
public:
void foo(A* x){x->start();x->stop();};
};
void a::calling_function(B& x)
{
x.foo(this);
}
int main()
{
B caller;
A* a = new A;
A* b = new B;
a->calling_function(caller);
b->calling_function(caller);
delete a;
delete b;
... Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top