Function pointer member variable to non-member function

Discussion in 'C++' started by Alex, Oct 15, 2003.

  1. Alex

    Alex Guest

    I have a class that has an array of function pointers as a member
    variable. I pass in function pointers in the constructor (these function
    pointers are to C functions). Later when I try and call the function, it
    says there is an error and my program crashes (assuming a memory problem).
    Here is a small example of what I am trying to do.

    ----- Common include file ------

    typedef int (*ptrFunc)(int);

    ----- Separate .h/.cpp --------

    class A {
    public:
    A( ptrFunc one, ptrFunc two );
    void UseFuncs();
    private:
    ptrFunc m_pFunctions[2];
    }

    A::A( ptrFunc one, ptrFunc two )
    {
    m_pFunctions[0] = one;
    m_pFunctions[1] = two;
    }

    void A::UseFuncs()
    {
    int value1 = m_pFunctions[0]( 5 );
    int value2 = m_pFunctions[1]( 6 );
    }

    ----- Separate .h/.cpp ------

    extern "C" int funcOne( int a );
    extern "C" int funcTwo( int a );

    class B
    {
    public:
    B();
    private:
    A* m_classA;
    }

    B::B()
    {
    m_classA = new A( &funcOne, &funcTwo );
    m_classA->UseFuncs();
    }

    int funcOne( int a )
    {
    return a * 5;
    }

    int funcTwo( int a )
    {
    return a * 4;
    }

    -------------------------------

    My question is, is there anything special I need to do to get these
    functions to work when I pass them to another class? I am using MSVC++ 6.0. Thanks!
     
    Alex, Oct 15, 2003
    #1
    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. slide_o_mix
    Replies:
    0
    Views:
    444
    slide_o_mix
    Oct 15, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,093
    Fraser Ross
    Aug 14, 2004
  3. Assertor
    Replies:
    1
    Views:
    622
    Victor Bazarov
    Mar 9, 2006
  4. ittium
    Replies:
    7
    Views:
    515
    Jorgen Grahn
    Jul 1, 2011
  5. somenath
    Replies:
    2
    Views:
    177
    somenath
    Aug 29, 2013
Loading...

Share This Page