473,728 Members | 1,978 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 1414
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
2464
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
9936
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
4880
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
1784
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
2035
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
3646
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
1343
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
1900
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
8758
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
9422
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9277
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
9198
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
9130
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...
1
6716
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6013
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4524
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2663
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.