Function pointer member variable to non-member function

S

slide_o_mix

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? Thanks!
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top