Function pointer member variable to non-member function

A

Alex

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

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
474,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top