How to declare and use a function pointer?

Discussion in 'C++' started by Sam Waller, Jul 14, 2006.

  1. Sam Waller

    Sam Waller Guest

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

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


    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;
    }
     
    Sam Waller, Jul 14, 2006
    #1
    1. Advertising

  2. Sam Waller

    Guest

    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-lite/pointers-to-members.html
     
    , Jul 14, 2006
    #2
    1. Advertising

  3. "Sam Waller" <> wrote in message
    news:...
    >
    > /*********************
    >
    > **********************/
    >
    >
    > 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
     
    Stuart Golodetz, Jul 14, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen stark
    Replies:
    2
    Views:
    727
    Ron Natalie
    Oct 10, 2003
  2. shuisheng
    Replies:
    4
    Views:
    393
    peter koch
    Sep 26, 2006
  3. Mike Wahler
    Replies:
    4
    Views:
    348
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Mar 25, 2007
  4. JDT
    Replies:
    1
    Views:
    367
    red floyd
    Mar 30, 2007
  5. Francois Grieu
    Replies:
    5
    Views:
    319
    Krice
    Oct 25, 2011
Loading...

Share This Page