472,125 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

question/clarification: pointer to function as passed parameter


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.

Jul 23 '05 #1
3 1331
ma******@pegasus.cc.ucf.edu 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
Jul 23 '05 #2
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.

Jul 23 '05 #3
ma******@pegasus.cc.ucf.edu 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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Desmond Liu | last post: by
9 posts views Thread by copx | last post: by
4 posts views Thread by jrefactors | last post: by
4 posts views Thread by voidtwerp | last post: by
29 posts views Thread by shuisheng | last post: by
4 posts views Thread by Devon Null | last post: by
16 posts views Thread by mdh | last post: by

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.