473,387 Members | 1,569 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,387 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 1398
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Desmond Liu | last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use of references when passing parameters. I understand the reasoning behind using references--you avoid the cost of creating...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: copx | last post by:
I've noticed that it's valid to pass a string literal (like "test") to a function that expects a const char *. Does this mean that in C a string literal is automatically converted to a const char *...
4
by: jrefactors | last post by:
In the following program, are parameters s in function reverse() and x in function count() both pass by value? How come value k is not changed, but value str has changed? Please advise....
4
by: voidtwerp | last post by:
Hi, I hope this is not too OT but I would like clarification on how classes are held in memory. each object obviously has an individual copy of its data. I guess a class would only have one...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
2
by: Jeroen | last post by:
Hi all, I wrote a little bit of code to see the behaviour of (copy) constructors of base and derived classes. But I have a question about it. Let me explain by the following...
4
by: Devon Null | last post by:
I have been exploring the concept of abstract classes and I was curious - If I do not define a base class as abstract, will it be instantiated (hope that is the right word) when a derived class is...
16
by: mdh | last post by:
I have a question about the library function strcpy. In K&R on page 109, strcpy is used to copy a line ( line ) to a pointer (char *), which is first allocated space by K&Rs "alloc" function. In...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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,...

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.