operator()

C

Chameleon

How I must implement function 'func' to work the code below?
I guess that I need templates but I haven't a link to start.
I saw something like this in boost::thread but I didn't understand.
--------------------------------------------------------------
class A
{
public:
void operator()() { printf("enter A\n"); }
};

void afunc() { printf("enter afunc\n"); }

void func(void (*a)()) { a(); }

int main() {
A a;
func(afunc);
func(&a);
return 0;
}
 
I

Ian Collins

Chameleon said:
How I must implement function 'func' to work the code below?
I guess that I need templates but I haven't a link to start.
I saw something like this in boost::thread but I didn't understand.
--------------------------------------------------------------
class A
{
public:
void operator()() { printf("enter A\n"); }
};

void afunc() { printf("enter afunc\n"); }

void func(void (*a)()) { a(); }

int main() {
A a;
func(afunc);
func(&a);
return 0;
}

template <typename F> void func( F a ) { a(); }

int main() {
A a;
func(afunc);
func(a);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top