Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question/clarification: pointer to function as passed parameter

Reply
Thread Tools

question/clarification: pointer to function as passed parameter

 
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      02-02-2005

Consider the 'C' source.

void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal)
{
doorBellDetected = doorBellVal;
}

void slRcv()
{
starLinkOpenStruct myOpenStruct;
// later
myOpenStruct.starLinkId = FFT_NODE;
myOpenStruct.flags = 0;
myOpenStruct.doorBellCallback = myDoorBellISR;
myOpenStruct.root = PSEUDO_ROOT_NODE;
};

When view from C++ perspective, I'd like pass in myOpenStruct to the
constructor of a class called recv. So now:

int main()
{
// have user setup and pass in the open struct
starLinkOpenStruct myOpenStruct;
myOpenStruct.starLinkId = FFT_NODE;
myOpenStruct.flags = 0;
myOpenStruct.doorBellCallback = myDoorBellISR;
myOpenStruct.root = PSEUDO_ROOT_NODE;

recv* ptr = new recv (myOpenStruct);
}

// recv looks like
class recv
{
public:
recv (starLinkOpenStuct& open_struct)
{
// stuff
}
// more stuff
};


Trouble is doorBellCallBack poses a potential problem, hence I'm trying
to figure out an ideal approach when one of the passed parmams is a
pointer to a function?

Of course another potential issue is the fact that this approach
requires the user to know what the name of the function (in this case
myDoorBellISR) is.

///////
My initial thought

static void myDoorBellISR(starLinkDevice *slDevice,
U32 doorBellVal)
{
doorBellDetected = doorBellVal;
}

class recv
{
public:
recv (starLinkOpenStuct& open_struct)
{
// stuff
}
// more stuff
};


Here again this forces the user to know the name of the
doorBellCallback function. Not good. Help!!


Thanks in advance.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-02-2005
wrote:
> Consider the 'C' source.
>
> void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal)
> {


slDevice doesn't seem to be used here.

> doorBellDetected = doorBellVal;
> }
>
> void slRcv()
> {
> starLinkOpenStruct myOpenStruct;
> // later
> myOpenStruct.starLinkId = FFT_NODE;
> myOpenStruct.flags = 0;
> myOpenStruct.doorBellCallback = myDoorBellISR;
> myOpenStruct.root = PSEUDO_ROOT_NODE;
> };


Considering this 'C' source requires a bunch of assumptions. For example,
'starLinkDevice' is not defined. 'starLinkOpeStruct' is not defined. U32
is not defined. Plenty of other things are undefined as well.

> When view from C++ perspective, I'd like pass in myOpenStruct to the
> constructor of a class called recv. So now:
>
> int main()
> {
> // have user setup and pass in the open struct
> starLinkOpenStruct myOpenStruct;
> myOpenStruct.starLinkId = FFT_NODE;
> myOpenStruct.flags = 0;
> myOpenStruct.doorBellCallback = myDoorBellISR;
> myOpenStruct.root = PSEUDO_ROOT_NODE;
>
> recv* ptr = new recv (myOpenStruct);
> }
>
> // recv looks like
> class recv
> {
> public:
> recv (starLinkOpenStuct& open_struct)
> {
> // stuff
> }
> // more stuff
> };


Given the same assumption as with the "'C' source", looks OK.

> Trouble is doorBellCallBack poses a potential problem,


Really? What problem is that?

> hence I'm trying
> to figure out an ideal approach when one of the passed parmams is a
> pointer to a function?


None of the passed parmams is a pointer to function in your code. You
have one argument -- a reference to 'starLinkOpenStruct'.

> Of course another potential issue is the fact that this approach
> requires the user to know what the name of the function (in this case
> myDoorBellISR) is.


Huh?

> ///////
> My initial thought
>
> static void myDoorBellISR(starLinkDevice *slDevice,
> U32 doorBellVal)
> {
> doorBellDetected = doorBellVal;
> }
>
> class recv
> {
> public:
> recv (starLinkOpenStuct& open_struct)
> {
> // stuff
> }
> // more stuff
> };


Your initial thought is fine. Nothing here involves any pointers to
function AFAICS.

> Here again this forces the user to know the name of the
> doorBellCallback function.


WHERE? I don't see any 'doorBellCallback' in that "my initial thought"
piece of code.

> Not good. Help!!


Help you do what? Not good what?

V
 
Reply With Quote
 
 
 
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      02-02-2005

>Your initial thought is fine. Nothing here involves any poi*nters to

function AFAICS

Thank you sir.

Victor, I'm batting 1/3 with you. I envisioned it would probably
take me two to three times to get it right since I might not have
understood something about your reponse or poor post on my part to
begin with

For clarification, here's the starLinkOpenStruct definition. So now.

// stalink.h
typedef unsigned int U32

// later
typedef struct _starLinkOpenStruct
{
U32 idx;
U32 starLinkId;
U32 flags;
U32 root;
void (*doorBellCallback)(struct _starLinkDevice *slDev, U32
doorBellVal);
} starLinkOpenStruct;

starLinkDevice is additonal struct which includes another struct and
that struct includes another struct, so for the purposes of discussion
and simplicity I'll modify it to look like.

// slink_mod.h
#ifndef SLINK_MOD_H
#define SLINK_MOD_H

typedef unsigned int U32;
// later
typedef struct _starLinkOpenStruct
{
U32 idx;
U32 starLinkId;
U32 flags;
U32 root;
void (*doorBellCallback)(U32 doorBellVal);
} starLinkOpenStruct;
#endif


// receiver.h
#ifndef RECV_H
#define RECV_H

# include "slink_mod.h"
static void myDoorBellISR(U32 doorBellVal)
{
doorBellVal = 5;
}

class receiver
{
public:
receiver (starLinkOpenStruct& slink_open_struct)
{
std::cout << " receiver called " << std::endl;
}
~receiver() {}
};
#endif

// test.cpp
# include <iostream>
# include "receiver.h"
# include "slink_mod.h"

int main()
{
starLinkOpenStruct myOpenStruct;
myOpenStruct.starLinkId = 100;
myOpenStruct.flags = 0;
myOpenStruct.doorBellCallback = myDoorBellISR;
myOpenStruct.root = 10;
receiver* recv = new receiver(myOpenStruct);
delete recv;
}

So I wrestled with the line.
myOpenStruct.doorBellCallback = myDoorBellISR;

For some strange reason I thought that having the user specify the
doorBellCallback member function is inane.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-02-2005
wrote:
>>Your initial thought is fine. Nothing here involves any poi*nters to

>
> function AFAICS
>
> Thank you sir.
>
> Victor, I'm batting 1/3 with you. I envisioned it would probably
> take me two to three times to get it right since I might not have
> understood something about your reponse or poor post on my part to
> begin with
>
> For clarification, here's the starLinkOpenStruct definition. So now.
>
> // stalink.h
> typedef unsigned int U32
>
> // later
> typedef struct _starLinkOpenStruct
> {
> U32 idx;
> U32 starLinkId;
> U32 flags;
> U32 root;
> void (*doorBellCallback)(struct _starLinkDevice *slDev, U32
> doorBellVal);
> } starLinkOpenStruct;
>
> starLinkDevice is additonal struct which includes another struct and
> that struct includes another struct, so for the purposes of discussion
> and simplicity I'll modify it to look like.
>
> // slink_mod.h
> #ifndef SLINK_MOD_H
> #define SLINK_MOD_H
>
> typedef unsigned int U32;
> // later
> typedef struct _starLinkOpenStruct
> {
> U32 idx;
> U32 starLinkId;
> U32 flags;
> U32 root;
> void (*doorBellCallback)(U32 doorBellVal);
> } starLinkOpenStruct;
> #endif
>
>
> // receiver.h
> #ifndef RECV_H
> #define RECV_H
>
> # include "slink_mod.h"
> static void myDoorBellISR(U32 doorBellVal)
> {
> doorBellVal = 5;
> }
>
> class receiver
> {
> public:
> receiver (starLinkOpenStruct& slink_open_struct)
> {
> std::cout << " receiver called " << std::endl;
> }
> ~receiver() {}
> };
> #endif
>
> // test.cpp
> # include <iostream>
> # include "receiver.h"
> # include "slink_mod.h"
>
> int main()
> {
> starLinkOpenStruct myOpenStruct;
> myOpenStruct.starLinkId = 100;
> myOpenStruct.flags = 0;
> myOpenStruct.doorBellCallback = myDoorBellISR;
> myOpenStruct.root = 10;


I would probably have written

starLinkOpenStruct myOpenStruct = { 0, 100, 0, 10, myDoorBellISR };

OTOH, your way helps to understand what members the numbers correspond
to. BTW, the 'idx' member is left uninitialised.

> receiver* recv = new receiver(myOpenStruct);
> delete recv;
> }
>
> So I wrestled with the line.
> myOpenStruct.doorBellCallback = myDoorBellISR;


"Wrestled"? In what way? Did it compile? Did it do what you expected
it to do?

> For some strange reason I thought that having the user specify the
> doorBellCallback member function is inane.


It's not a member function. It's a member [of 'starLinkOpenStruct'] that
just happens to be a pointer to a function. And why is it inane? The
user has to initialise (or assign in your case) all the members so that
the struct is useful. 'doorBellCallback' is just another member to be
initialised (assigned). Otherwise it contains garbage and cannot be used.

V
 
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
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
Function pointer array as parameter to a function aruna.mysore@gmail.com C Programming 25 05-20-2008 11:08 PM
Command line options: using an [option: parameter] without a parameter passed to it soren625 Perl Misc 10 12-28-2005 09:26 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57