473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function pointer member variable to non-member function

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!
Jul 19 '05 #1
0 974

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bryan Parkoff | last post by:
I create one 32 Bits variable and four pointer variables. Four pointer variables link or point to one 32 Bits variable. Each pointer variable is 8 Bits. Look at my example below. unsigned int...
10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
22
by: Alex Fraser | last post by:
From searching Google Groups, I understand that void pointer arithmetic is a constraint violation, which is understandable. However, generic functions like qsort() and bsearch() must in essence do...
34
by: Andrew | last post by:
Is there anyway to test if a pointer points to allocated memory or not? For example if I have a pointer such as char *p is there a standard way to test whether an assignment such as the following...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
4
by: Bryan Parkoff | last post by:
We discussed about the union keyword on the previous threads 2-3 days earlier. I want to add. Two byte data inside struct and one word data are inside union. You modify word data. Then two byte...
1
by: Carmen Sei | last post by:
to delcare an object with pointer - MyCar * mycar = new MyCar; mycar->Create(); ======================== to delcare an object without pointer - MyCar mycar = new MyCar; mycar.Create(); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.