Function Pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Function Pointers

    I am trying to get a handle on function pointers, trying this example
    program in Visual Studio:

    #include <iostream.h>

    class TestingUp
    {
    public:
    TestingUp()
    {
    }
    TestFunc(void(* ptr)())
    {
    ptr();
    }
    };

    class TestingDown
    {
    public:
    TestingDown()
    {
    x = 8;
    y = 25;
    pfv = Display;
    test.TestFunc(p fv);
    }
    void Display()
    {
    cout << "At this time the value of x is " << x << endl;
    cout << "At this time the value of y is " << y << endl;
    }
    protected:
    int x;
    int y;
    void (*pfv)();
    TestingUp test;
    };

    int main()
    {
    TestingDown downTest;
    return 0;
    }

    Getting the cryptic error:
    error C2440: '=' : cannot convert from 'void (__thiscall
    TestingDown::*) (void)' to 'void (__cdecl *)(void)'

    How do I fix that? Websites on function pointer syntax and callback
    use are cryptic at best. Thanks for any help that can be provided.
  • Victor Bazarov

    #2
    Re: Function Pointers

    Steve wrote:[color=blue]
    > I am trying to get a handle on function pointers, trying this example
    > program in Visual Studio:[/color]

    Are you sure the example is as it came? Or did you change it slightly?
    [color=blue]
    >
    > #include <iostream.h>[/color]

    Should be

    #include <iostream>
    [color=blue]
    >
    > class TestingUp
    > {
    > public:
    > TestingUp()
    > {
    > }
    > TestFunc(void(* ptr)())
    > {
    > ptr();
    > }
    > };
    >
    > class TestingDown
    > {
    > public:
    > TestingDown()
    > {
    > x = 8;
    > y = 25;
    > pfv = Display;[/color]

    This is not valid syntax for members. You have to do

    pfv = &TestingDown::D isplay;
    [color=blue]
    > test.TestFunc(p fv);
    > }
    > void Display()
    > {
    > cout << "At this time the value of x is " << x << endl;
    > cout << "At this time the value of y is " << y << endl;
    > }
    > protected:
    > int x;
    > int y;
    > void (*pfv)();[/color]

    This is a pointer to function. If you intend to assign a pointer to
    a member function, you need to declare it a pointer to member.

    void (TestingDown::* pfv)();
    [color=blue]
    > TestingUp test;
    > };
    >
    > int main()
    > {
    > TestingDown downTest;
    > return 0;
    > }
    >
    > Getting the cryptic error:
    > error C2440: '=' : cannot convert from 'void (__thiscall
    > TestingDown::*) (void)' to 'void (__cdecl *)(void)'
    >
    > How do I fix that? Websites on function pointer syntax and callback
    > use are cryptic at best. Thanks for any help that can be provided.[/color]

    Get a good book. Before that, look in the FAQ. You can find it here:


    Victor

    Comment

    • John Harrison

      #3
      Re: Function Pointers


      "Steve" <steve.sorkin@g mail.com> wrote in message
      news:ca625fa6.0 410200719.1499f 559@posting.goo gle.com...[color=blue]
      > I am trying to get a handle on function pointers, trying this example
      > program in Visual Studio:
      >[/color]
      [snip]
      [color=blue]
      >
      > Getting the cryptic error:
      > error C2440: '=' : cannot convert from 'void (__thiscall
      > TestingDown::*) (void)' to 'void (__cdecl *)(void)'
      >
      > How do I fix that? Websites on function pointer syntax and callback
      > use are cryptic at best. Thanks for any help that can be provided.[/color]

      You should understand that pointers to member functions and pointers to
      ordinary functions are two different things with two different syntaxes. All
      your code seems to be dealing with pointers to ordinary functions but then
      you try and use it on a member function (Display), switch to an ordinary
      function and I guess it will compile.

      Books are generally better than websites for learning C++.

      john


      Comment

      Working...