Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function pointer member variable to non-member function

Reply
Thread Tools

Function pointer member variable to non-member function

 
 
slide_o_mix
Guest
Posts: n/a
 
      10-15-2003

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!


--
Posted via http://dbforums.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
Pointer-to-member-function pointing to a member function of an inherited class akiriwas@gmail.com C++ 12 02-11-2005 05:15 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
Function pointer member variable to non-member function Alex C++ 0 10-15-2003 05:26 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments