Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pointers to functions

pointers to functions

Scheduled Pinned Locked Moved C / C++ / MFC
questionannouncementlounge
12 Posts 4 Posters 17 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Calin Negru

    How do you use a pointer to a function that is declared in the general scope within a class. Do you pass the pointer as parameter to class function, save the pointer as a class member and then use the class member in a class function when needed?

    void somefunction(int i);
    void (*pointertosomef)(int i) = somefunction;

    class SomeObject
    {
    void (*m_pointertosomef)(int i);
    public:
    void ImportPointerToF(void (*pointertosomef)(int i));
    void UsePointerToF();
    }

    void SomeObject::ImportPointerToF(void (*pointertosomef)(int i))
    {
    m_pointertosomef = pointertosomef;
    }
    void SomeObject::UsePointerToF()
    {
    m_pointertosomef(30);
    }

    I found on the internet the basic version of declaring a pointer function

    void fun(int a);

    void (*fun_ptr)(int) = fun;

    fun\_ptr(10);
    

    Everything else is my speculation

    Mircea NeacsuM Offline
    Mircea NeacsuM Offline
    Mircea Neacsu
    wrote on last edited by
    #2

    Yes, that works.

    Mircea

    1 Reply Last reply
    0
    • C Calin Negru

      How do you use a pointer to a function that is declared in the general scope within a class. Do you pass the pointer as parameter to class function, save the pointer as a class member and then use the class member in a class function when needed?

      void somefunction(int i);
      void (*pointertosomef)(int i) = somefunction;

      class SomeObject
      {
      void (*m_pointertosomef)(int i);
      public:
      void ImportPointerToF(void (*pointertosomef)(int i));
      void UsePointerToF();
      }

      void SomeObject::ImportPointerToF(void (*pointertosomef)(int i))
      {
      m_pointertosomef = pointertosomef;
      }
      void SomeObject::UsePointerToF()
      {
      m_pointertosomef(30);
      }

      I found on the internet the basic version of declaring a pointer function

      void fun(int a);

      void (*fun_ptr)(int) = fun;

      fun\_ptr(10);
      

      Everything else is my speculation

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      Does it compile, build and run without errors? The only caveat I would mention is that pointers to class methods require the method to be static.

      C 1 Reply Last reply
      0
      • L Lost User

        Does it compile, build and run without errors? The only caveat I would mention is that pointers to class methods require the method to be static.

        C Offline
        C Offline
        Calin Negru
        wrote on last edited by
        #4

        ok, thanks to both of you.

        Quote:

        Does it compile, build and run without errors

        Usually I don`t try to compile wild guesses. I did things as you would do with a pointer in my post above but often in c++ things that resemble in some places don`t have a syntax that matches everywhere.

        Mircea NeacsuM 1 Reply Last reply
        0
        • C Calin Negru

          ok, thanks to both of you.

          Quote:

          Does it compile, build and run without errors

          Usually I don`t try to compile wild guesses. I did things as you would do with a pointer in my post above but often in c++ things that resemble in some places don`t have a syntax that matches everywhere.

          Mircea NeacsuM Offline
          Mircea NeacsuM Offline
          Mircea Neacsu
          wrote on last edited by
          #5

          Calin Cali wrote:

          Usually I don`t try to compile wild guesses.

          Well, you should because: 1. Might take less time than waiting for an answer. 2. Compiler is never cranky or in a bad mood. At most it will issue an error message. 3. One wild guess leads to another and soon you end up with a nice brilliant idea :)

          Mircea

          C 1 Reply Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            Calin Cali wrote:

            Usually I don`t try to compile wild guesses.

            Well, you should because: 1. Might take less time than waiting for an answer. 2. Compiler is never cranky or in a bad mood. At most it will issue an error message. 3. One wild guess leads to another and soon you end up with a nice brilliant idea :)

            Mircea

            C Offline
            C Offline
            Calin Negru
            wrote on last edited by
            #6

            Quote:

            Well, you should because:

            For me it feels like trying to compile a quote from Ceaikovski or Twain.

            L 1 Reply Last reply
            0
            • C Calin Negru

              Quote:

              Well, you should because:

              For me it feels like trying to compile a quote from Ceaikovski or Twain.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              Mircea is quite correct, you should always try and build small samples. Apart from anything else, the error reports help you to learn. The compiler, linker and debugger excellent tools that aid in development. Had you started in the days when we had to submit a deck of punched cards and wait 24 hours for the compiler output to tell us one character was mistyped, you would appreciate how easy things are these days.

              C 1 Reply Last reply
              0
              • L Lost User

                Mircea is quite correct, you should always try and build small samples. Apart from anything else, the error reports help you to learn. The compiler, linker and debugger excellent tools that aid in development. Had you started in the days when we had to submit a deck of punched cards and wait 24 hours for the compiler output to tell us one character was mistyped, you would appreciate how easy things are these days.

                C Offline
                C Offline
                Calin Negru
                wrote on last edited by
                #8

                It`s interesting to hear where things started. I had my first computer experience in the 80`s on a computer with keyboard and a dedicated green and white screen (the screen was displaying only two colors). My first programming experience was on a computer that you had to connect to a TV set. It was a computer with 16 colors graphics.

                L 1 Reply Last reply
                0
                • C Calin Negru

                  It`s interesting to hear where things started. I had my first computer experience in the 80`s on a computer with keyboard and a dedicated green and white screen (the screen was displaying only two colors). My first programming experience was on a computer that you had to connect to a TV set. It was a computer with 16 colors graphics.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  Mine was the LEO* III in the 60s, second row first three pictures at Leo Computers Society. Leo 3 photos[^]. Most input was punched paper tape, and some punched cards, no mass storage, only magnetic tape. *The Lyons Electronic Office, initially developed between Lyons Catering and English Electric, both companies long gone.

                  C 1 Reply Last reply
                  0
                  • L Lost User

                    Mine was the LEO* III in the 60s, second row first three pictures at Leo Computers Society. Leo 3 photos[^]. Most input was punched paper tape, and some punched cards, no mass storage, only magnetic tape. *The Lyons Electronic Office, initially developed between Lyons Catering and English Electric, both companies long gone.

                    C Offline
                    C Offline
                    Calin Negru
                    wrote on last edited by
                    #10

                    Working in the field since the beginning I imagine you had a computer from every generation.

                    L 1 Reply Last reply
                    0
                    • C Calin Negru

                      How do you use a pointer to a function that is declared in the general scope within a class. Do you pass the pointer as parameter to class function, save the pointer as a class member and then use the class member in a class function when needed?

                      void somefunction(int i);
                      void (*pointertosomef)(int i) = somefunction;

                      class SomeObject
                      {
                      void (*m_pointertosomef)(int i);
                      public:
                      void ImportPointerToF(void (*pointertosomef)(int i));
                      void UsePointerToF();
                      }

                      void SomeObject::ImportPointerToF(void (*pointertosomef)(int i))
                      {
                      m_pointertosomef = pointertosomef;
                      }
                      void SomeObject::UsePointerToF()
                      {
                      m_pointertosomef(30);
                      }

                      I found on the internet the basic version of declaring a pointer function

                      void fun(int a);

                      void (*fun_ptr)(int) = fun;

                      fun\_ptr(10);
                      

                      Everything else is my speculation

                      Greg UtasG Offline
                      Greg UtasG Offline
                      Greg Utas
                      wrote on last edited by
                      #11

                      You're creating a pointer to a free function (one defined outside a class) and then storing it, and invoking it, from within an object. But it's also possible to create a pointer to class member data or a member function. See here[^]. You may need to read several articles about this to gain a good understanding, because I can't point to one that is really good on its own. Search on "C++ pointer to member" and read articles that discuss the type Class::* (a pointer to a class member) and operators .* and ->*. These make the following possible:

                      int Class::\* pm = &Class::m;         // pointer to member data "int m"
                      int (Class::\* pf)(int) = &Class::f;  // pointer to member function "int f(int)"
                      Class c, \*k;
                      c.\*pm = 1;        // c.m = 1;
                      n = c.\*pf(0);     // n = c.f(0);
                      k->.pm = 2;       // k->m = 2;
                      n = (k->\*pf)(0);  // n = k->f(0);  note that ->\* requires parentheses
                      

                      Robust Services Core | Software Techniques for Lemmings | Articles
                      The fox knows many things, but the hedgehog knows one big thing.

                      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
                      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

                      1 Reply Last reply
                      0
                      • C Calin Negru

                        Working in the field since the beginning I imagine you had a computer from every generation.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        Not quite, I started in the transistor age, so things were getting quite sophisticated.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups