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