473,659 Members | 3,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question/clarification: pointer to function as passed parameter


Consider the 'C' source.

void myDoorBellISR(s tarLinkDevice *slDevice, U32 doorBellVal)
{
doorBellDetecte d = doorBellVal;
}

void slRcv()
{
starLinkOpenStr uct myOpenStruct;
// later
myOpenStruct.st arLinkId = FFT_NODE;
myOpenStruct.fl ags = 0;
myOpenStruct.do orBellCallback = myDoorBellISR;
myOpenStruct.ro ot = PSEUDO_ROOT_NOD E;
};

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
starLinkOpenStr uct myOpenStruct;
myOpenStruct.st arLinkId = FFT_NODE;
myOpenStruct.fl ags = 0;
myOpenStruct.do orBellCallback = myDoorBellISR;
myOpenStruct.ro ot = PSEUDO_ROOT_NOD E;

recv* ptr = new recv (myOpenStruct);
}

// recv looks like
class recv
{
public:
recv (starLinkOpenSt uct& open_struct)
{
// stuff
}
// more stuff
};
Trouble is doorBellCallBac k 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(s tarLinkDevice *slDevice,
U32 doorBellVal)
{
doorBellDetecte d = doorBellVal;
}

class recv
{
public:
recv (starLinkOpenSt uct& open_struct)
{
// stuff
}
// more stuff
};
Here again this forces the user to know the name of the
doorBellCallbac k function. Not good. Help!!
Thanks in advance.

Jul 23 '05 #1
3 1409
ma******@pegasu s.cc.ucf.edu wrote:
Consider the 'C' source.

void myDoorBellISR(s tarLinkDevice *slDevice, U32 doorBellVal)
{
slDevice doesn't seem to be used here.
doorBellDetecte d = doorBellVal;
}

void slRcv()
{
starLinkOpenStr uct myOpenStruct;
// later
myOpenStruct.st arLinkId = FFT_NODE;
myOpenStruct.fl ags = 0;
myOpenStruct.do orBellCallback = myDoorBellISR;
myOpenStruct.ro ot = PSEUDO_ROOT_NOD E;
};
Considering this 'C' source requires a bunch of assumptions. For example,
'starLinkDevice ' is not defined. 'starLinkOpeStr uct' 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
starLinkOpenStr uct myOpenStruct;
myOpenStruct.st arLinkId = FFT_NODE;
myOpenStruct.fl ags = 0;
myOpenStruct.do orBellCallback = myDoorBellISR;
myOpenStruct.ro ot = PSEUDO_ROOT_NOD E;

recv* ptr = new recv (myOpenStruct);
}

// recv looks like
class recv
{
public:
recv (starLinkOpenSt uct& open_struct)
{
// stuff
}
// more stuff
};
Given the same assumption as with the "'C' source", looks OK.
Trouble is doorBellCallBac k 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 'starLinkOpenSt ruct'.
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(s tarLinkDevice *slDevice,
U32 doorBellVal)
{
doorBellDetecte d = doorBellVal;
}

class recv
{
public:
recv (starLinkOpenSt uct& 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
doorBellCallbac k function.
WHERE? I don't see any 'doorBellCallba ck' 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 starLinkOpenStr uct definition. So now.

// stalink.h
typedef unsigned int U32

// later
typedef struct _starLinkOpenSt ruct
{
U32 idx;
U32 starLinkId;
U32 flags;
U32 root;
void (*doorBellCallb ack)(struct _starLinkDevice *slDev, U32
doorBellVal);
} starLinkOpenStr uct;

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 _starLinkOpenSt ruct
{
U32 idx;
U32 starLinkId;
U32 flags;
U32 root;
void (*doorBellCallb ack)(U32 doorBellVal);
} starLinkOpenStr uct;
#endif
// receiver.h
#ifndef RECV_H
#define RECV_H

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

class receiver
{
public:
receiver (starLinkOpenSt ruct& slink_open_stru ct)
{
std::cout << " receiver called " << std::endl;
}
~receiver() {}
};
#endif

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

int main()
{
starLinkOpenStr uct myOpenStruct;
myOpenStruct.st arLinkId = 100;
myOpenStruct.fl ags = 0;
myOpenStruct.do orBellCallback = myDoorBellISR;
myOpenStruct.ro ot = 10;
receiver* recv = new receiver(myOpen Struct);
delete recv;
}

So I wrestled with the line.
myOpenStruct.do orBellCallback = myDoorBellISR;

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

Jul 23 '05 #3
ma******@pegasu s.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 starLinkOpenStr uct definition. So now.

// stalink.h
typedef unsigned int U32

// later
typedef struct _starLinkOpenSt ruct
{
U32 idx;
U32 starLinkId;
U32 flags;
U32 root;
void (*doorBellCallb ack)(struct _starLinkDevice *slDev, U32
doorBellVal);
} starLinkOpenStr uct;

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 _starLinkOpenSt ruct
{
U32 idx;
U32 starLinkId;
U32 flags;
U32 root;
void (*doorBellCallb ack)(U32 doorBellVal);
} starLinkOpenStr uct;
#endif
// receiver.h
#ifndef RECV_H
#define RECV_H

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

class receiver
{
public:
receiver (starLinkOpenSt ruct& slink_open_stru ct)
{
std::cout << " receiver called " << std::endl;
}
~receiver() {}
};
#endif

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

int main()
{
starLinkOpenStr uct myOpenStruct;
myOpenStruct.st arLinkId = 100;
myOpenStruct.fl ags = 0;
myOpenStruct.do orBellCallback = myDoorBellISR;
myOpenStruct.ro ot = 10;


I would probably have written

starLinkOpenStr uct 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(myOpen Struct);
delete recv;
}

So I wrestled with the line.
myOpenStruct.do orBellCallback = 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
doorBellCallbac k member function is inane.


It's not a member function. It's a member [of 'starLinkOpenSt ruct'] 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. 'doorBellCallba ck' 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
2446
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 a local object when you pass an object by reference. But why use a reference? Is there any inherent advantage of using a reference over using a pointer? My workplace discourages the use of pointers when it comes to parameter passing. They...
110
9899
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 must be an object instead of
9
4877
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 * if I pass it to a function? TIA, copx
4
1781
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. thanks!! ============================================ void reverse(char s); void count(int x);
4
2029
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 copy of its functions held in memory (not sure what part of memory this would be refered to as). Then whenever an object has a function called on it this function would be copied onto the stack - am I right?
29
3643
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 string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
2
1341
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 (incomplete/illustrative) code: class A { // (copy) ctors goe here... };
4
1895
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 created? if ( answer == false ) { Would the idea of an abstract class simply be used to enforce integrity of the classes by disallowing the abstract class to be instantiated, or are there other purposes for it? }
16
309
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 a few pages prior to this, the example that K&R showed used this definition for it's version of strcpy void strcpy( char *s, char *t){ while ( *s++ = *t++);
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.