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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. function pointers

function pointers

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 Posters 0 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.
  • S Offline
    S Offline
    shiftwik
    wrote on last edited by
    #1

    I can't seem to get the function pointers working?

    anybody see what's wrong? I just want to call the print()method

    #include "stdio.h"
    class TMyClass
    {
    public:
    void print(){ printf("Print!\n"); };
    void (TMyClass::*pt2Member)();
    TMyClass(void);
    };
    TMyClass::TMyClass(void)
    {
    pt2Member = &TMyClass::print;
    };
    int main(int argc, char* argv[])
    {
    TMyClass instance1;
    instance1.pt2Member;
    return 0;
    }

    _ 1 Reply Last reply
    0
    • S shiftwik

      I can't seem to get the function pointers working?

      anybody see what's wrong? I just want to call the print()method

      #include "stdio.h"
      class TMyClass
      {
      public:
      void print(){ printf("Print!\n"); };
      void (TMyClass::*pt2Member)();
      TMyClass(void);
      };
      TMyClass::TMyClass(void)
      {
      pt2Member = &TMyClass::print;
      };
      int main(int argc, char* argv[])
      {
      TMyClass instance1;
      instance1.pt2Member;
      return 0;
      }

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      To call a member function using pointers you have to use the pointer to member operators (.* or ->* or ::*). But declaring the function pointer inside the class is not very helpful. Usually the function pointer is declared outside the class like this -

      void (TMyClass::*pt2Member)() = &TMyClass::print;

      The above declaration may be done globally or inside the main function. In this case, the function call would be as follows -

      TMyClass instance1;
      (instance1.*pt2Member)();

      If, however, you do wish to declare the pointer inside the class like you've done here, the calling syntax would be a little weird and would look like this -

      TMyClass instance1;
      (instance1.*instance1.pt2Member)();

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      S 1 Reply Last reply
      0
      • _ _Superman_

        To call a member function using pointers you have to use the pointer to member operators (.* or ->* or ::*). But declaring the function pointer inside the class is not very helpful. Usually the function pointer is declared outside the class like this -

        void (TMyClass::*pt2Member)() = &TMyClass::print;

        The above declaration may be done globally or inside the main function. In this case, the function call would be as follows -

        TMyClass instance1;
        (instance1.*pt2Member)();

        If, however, you do wish to declare the pointer inside the class like you've done here, the calling syntax would be a little weird and would look like this -

        TMyClass instance1;
        (instance1.*instance1.pt2Member)();

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        S Offline
        S Offline
        shiftwik
        wrote on last edited by
        #3

        you earned your title 'superman' thank you!!! works like a charm

        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