How to declare and use a function pointer?

S

Sam Waller

/*********************

**********************/


I'm trying to declare a function pointer in class A and set it in class B, but I get
syntax errors. How can I get this to compile and run?

thanks,
Sam



class A {
public:
A(void) {
}
virtual ~A() {
}
void (*func)(int i, bool b); // trying to declare a function pointer
};

/*********************

**********************/

class B {

public:
B(void) {
a = new A();

a->func = &func(int i, bool b); // <<<<<<<<<< syntax error <<<<<<<<<<<<<<

// Microsoft visual studio error messages about the above line:
// error C2144: syntax error : missing ')' before type 'int'
// error C2660: 'func' : function does not take 0 parameters
// error C2059: syntax error : ')'

}
virtual ~B() {
delete a;
}
A *a;
void func(int i, bool b) {
return;
}

};

/***************************************************************************
I need to have this so that different 'B'-type classes can
change the behavior of A::func
***************************************************************************/

int main(int argc, char *argv[]) {
B *b = new B();

b->a->func(99, true); // should call B::func()

delete b;

return 0;
}
 
J

joosteto

Sam said:
/*********************

**********************/


I'm trying to declare a function pointer in class A and set it in class B, but I get
syntax errors. How can I get this to compile and run?

thanks,
Sam



class A {
public:
A(void) {
}
virtual ~A() {
}
void (*func)(int i, bool b); // trying to declare a function pointer
};

/*********************

**********************/

class B {

public:
B(void) {
a = new A();

a->func = &func(int i, bool b); // <<<<<<<<<< syntax error <<<<<<<<<<<<<<

You can't make a->func point to a member of B, unless you make it a
static function.
See also:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html
 
S

Stuart Golodetz

Sam Waller said:
/*********************

**********************/


I'm trying to declare a function pointer in class A and set it in class B,
but I get
syntax errors. How can I get this to compile and run?

thanks,
Sam



class A {
public:
A(void) {
}
virtual ~A() {
}
void (*func)(int i, bool b); // trying to declare
a function pointer
};

/*********************

**********************/

class B {

public:
B(void) {
a = new A();

a->func = &func(int i, bool b); // <<<<<<<<<< syntax error
<<<<<<<<<<<<<<

// Microsoft visual studio error messages about the above line:
// error C2144: syntax error : missing ')' before type 'int'
// error C2660: 'func' : function does not take 0 parameters
// error C2059: syntax error : ')'

}
virtual ~B() {
delete a;
}
A *a;
void func(int i, bool b) {
return;
}

};

/***************************************************************************
I need to have this so that different 'B'-type classes can
change the behavior of A::func
***************************************************************************/

int main(int argc, char *argv[]) {
B *b = new B();

b->a->func(99, true); // should call B::func()

delete b;

return 0;
}

Well if you absolutely must do this for whatever capricious reason(!), then:

#include <iostream>

template <typename T>
class A
{
public:
void (T::*func)(int i, bool b);
};

class B
{
public:
B()
{
a = new A<B>;
a->func = &B::func;
}

~B()
{
delete a;
}

A<B> *a;

void func(int i, bool b)
{
std::cout << "func(" << i << ", " << b << ")\n";
}
};

int main()
{
B b;
(b.*(b.a->func))(99, true);
return 0;
}

But it's really, really dubious. What are you actually trying to achieve
here? If you really want to have different func behaviour in various
classes, what's wrong with something like

class A
{
public:
virtual void func(int i, bool b) = 0;
};

class B : public A
{
public:
void func(int i, bool b)
{
// Whatever
}
};

?

I'm not really clear what you're after here.

Hope this helps a bit! :)
Stu
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top