Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to declare and use a function pointer?

Reply
Thread Tools

How to declare and use a function pointer?

 
 
Sam Waller
Guest
Posts: n/a
 
      07-14-2006

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

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


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


 
Reply With Quote
 
 
 
 
joosteto@gmail.com
Guest
Posts: n/a
 
      07-14-2006

Sam Waller wrote:
> /*********************
>
> **********************/
>
>
> 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-lit...o-members.html

 
Reply With Quote
 
 
 
 
Stuart Golodetz
Guest
Posts: n/a
 
      07-14-2006
"Sam Waller" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> /*********************
>
> **********************/
>
>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Declare a Friend function which is a template member function of adifferent class getrakesh@gmail.com C++ 2 02-26-2008 11:32 AM
How can I declare and define a friend template function in a template class? =?gb2312?B?wfXquw==?= C++ 10 08-01-2007 01:48 AM
forward declare member function so that it can be friend function yancheng.cheok@gmail.com C++ 1 11-09-2006 11:30 AM
how to declare pseudo-matrix function argument that is not modified by function? classicist C Programming 3 10-05-2005 02:45 AM



Advertisments