question/clarification: pointer to function as passed parameter

Discussion in 'C++' started by ma740988@pegasus.cc.ucf.edu, Feb 2, 2005.

  1. Guest

    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.
     
    , Feb 2, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Feb 2, 2005
    #2
    1. Advertising

  3. Guest

    Re: question/clarification: pointer to function as passed parameter


    >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.
     
    , Feb 2, 2005
    #3
  4. Re: question/clarification: pointer to function as passed parameter

    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
     
    Victor Bazarov, Feb 2, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    9
    Views:
    513
    Joseph Byrns
    Apr 15, 2005
  2. Anand
    Replies:
    2
    Views:
    903
    Anand
    Sep 11, 2003
  3. Mike
    Replies:
    17
    Views:
    574
    Cameron Laird
    Apr 5, 2007
  4. aneuryzma
    Replies:
    3
    Views:
    714
    Jim Langston
    Jun 16, 2008
  5. soren625
    Replies:
    10
    Views:
    240
    Kevin Collins
    Dec 28, 2005
Loading...

Share This Page