How to create an initialise pointer to pointer to function variable;

Discussion in 'C++' started by iceColdFire, May 23, 2005.

  1. iceColdFire

    iceColdFire Guest

    HI,

    I have a function as

    void f(int p)
    {
    return p++;
    }

    now I have created a function pointer as
    void(**pf)(int);

    and initialization as
    *pf=f;

    but compiler gives error...

    Why ?

    a.a.cpp
    iceColdFire, May 23, 2005
    #1
    1. Advertising

  2. * iceColdFire:
    >
    > I have a function as
    >
    > void f(int p)
    > {
    > return p++;
    > }


    First, you cannot return non-void when the function is declared
    as returning void.

    Second, with 'void' changed to 'int', that has the same final effect as

    int f( int p )
    {
    return p;
    }

    You might want to take a look at

    <url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_02_11.html>

    for other reasons why you should avoid postfix increment and decrement.


    > now I have created a function pointer as
    > void(**pf)(int);
    >
    > and initialization as
    > *pf=f;
    >
    > but compiler gives error...


    The compiler shouldn't flag _that_ as an error, but a good compiler will
    warn you that you're derefencing an uninitialized pointer.

    Possibly what you wanted was

    int (*pf)(int) = f;

    However, as a beginner try to avoid using pointers directly. In this case
    you would probably (here I'm guessing, but probably) be much better served
    by using a virtual member function instead. Try to read up on that.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 23, 2005
    #2
    1. Advertising

  3. iceColdFire

    iceColdFire Guest

    Hi Alf::

    Well...that was great explanaiton indeed...however I need to use
    pointer to pointer to function as

    int(**pf)(int);

    Now thing is how do I initialise the variable pf with f, and is my
    variable declaration correct for (pointer to (pointer to function))

    Thanks,
    a.a.cpp
    iceColdFire, May 23, 2005
    #3
  4. * iceColdFire:
    >
    > Well...that was great explanaiton indeed...however I need to use
    > pointer to pointer to function as
    >
    > int(**pf)(int);
    >
    > Now thing is how do I initialise the variable pf with f


    int (*pf)(int) = f;
    int (**ppf)(int) = &pf;

    > and is my
    > variable declaration correct for (pointer to (pointer to function))


    I think so (modulo bad eyesight & auto filtering of on-screen text).

    But if you'd describe what you're trying to achieve instead of how you're
    intending to implement part of the solution, I'm reasonably sure that I or
    someone else can tell you how to do that much more safely and easily via
    virtual member functions -- or perhaps templates, or whatever.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 23, 2005
    #4
  5. iceColdFire

    John Carson Guest

    "iceColdFire" <> wrote in message
    news:
    > Hi Alf::
    >
    > Well...that was great explanaiton indeed...however I need to use
    > pointer to pointer to function as
    >
    > int(**pf)(int);
    >
    > Now thing is how do I initialise the variable pf with f, and is my
    > variable declaration correct for (pointer to (pointer to function))
    >
    > Thanks,
    > a.a.cpp



    An alternative to Alf's scheme that is closer to your original is as follow:

    // pointer to function
    int(*pf)(int);

    // pointer to pointer to function
    int(**ppf)(int);

    int main()
    {
    //initialise double pointer to point to single pointer
    ppf=&pf;
    // use double pointer to initialise single pointer
    *ppf = &f;
    // call function from double pointer
    int x = (**ppf)(5);
    return 0;
    }

    Note that the & in

    *ppf = &f;

    is optional as is one of the asterisks in

    int x = (**ppf)(5);

    but it is more logical to include them (if working with pointers to member
    functions, then you must include them --- you don't get a choice).


    --
    John Carson
    John Carson, May 23, 2005
    #5
  6. iceColdFire

    iceColdFire Guest

    ::Alf

    Allright,
    That was nice and neat...can we initialize ppf with f instead of
    initialising with pf...

    And here is the explanation, I intend to build an array of pointer to
    pointer to functions which shall be used in a module for runtime
    function reference...I am avoiding using templates and virtual member
    functions, as what I have as resource is just the function addresses
    and not the code...
    so I can only use pointers to build up the entire system....

    Thanks,
    a.a.cpp
    iceColdFire, May 23, 2005
    #6
  7. iceColdFire

    iceColdFire Guest

    ::John,,,thanks for the nice explanation...
    Check my last posting for what I actually intend to build..

    a.a.cpp
    iceColdFire, May 23, 2005
    #7
  8. On 2005-05-23, iceColdFire <> wrote:
    >::Alf
    >
    > Allright,
    > That was nice and neat...can we initialize ppf with f instead of
    > initialising with pf...
    >
    > And here is the explanation, I intend to build an array of pointer to
    > pointer to functions which shall be used in a module for runtime
    > function reference...


    Yes, fine -- but why do you need pointer to (pointer to functions) ? Why is
    a pointer to function not good enough ?

    Or do you mean that you want to create a dynamic array of pointer-to-functions
    and the type of that dynamic array is pointer to (pointer to function) ?

    > I am avoiding using templates and virtual member
    > functions, as what I have as resource is just the function addresses
    > and not the code...


    You can wrap a function pointer in a function object (for example). Most of the
    solutions that would be suggested do not require you to have access to the source
    code for your functions.

    Cheers,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
    Donovan Rebbechi, May 23, 2005
    #8
  9. iceColdFire

    iceColdFire Guest

    ::Donovan,
    Yup !!!.

    I intend to build a dynamic array of [......]
    Also I liked your wrapping idea ...means wrapping a function pointer
    into a function object...
    But how do you do that....do you have any related docs for the trick..

    Thanks,...
    a.a.cpp
    iceColdFire, May 23, 2005
    #9
  10. iceColdFire

    John Carson Guest

    "iceColdFire" <> wrote in message
    news:
    >>> John,,,thanks for the nice explanation...

    > Check my last posting for what I actually intend to build..


    If you want a dynamic array of function pointers, then you can build it with
    new or with vector or...

    With vector, you can just go:

    // creates a 10 element vector of function pointers
    std::vector<int(*)(int)> vec(10);

    //Then you can set

    vec[0] = &f;

    // and so on. You would call the function from
    // the first vector element as follows:

    int y = (*vec[0])(7);

    Naturally, you can expand the size of the vector using push_back or various
    other member functions.

    --
    John Carson
    John Carson, May 23, 2005
    #10
    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. MNQ
    Replies:
    4
    Views:
    533
    Tim Hubberstey
    Jun 17, 2004
  2. Nandan
    Replies:
    4
    Views:
    391
    Tor Iver Wilhelmsen
    Apr 29, 2005
  3. Stuart Norris
    Replies:
    5
    Views:
    500
    Dave Thompson
    Sep 29, 2003
  4. Replies:
    0
    Views:
    497
  5. Angus
    Replies:
    8
    Views:
    436
    Francesco S. Carta
    Sep 2, 2010
Loading...

Share This Page