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. Need help with member function pointer

Need help with member function pointer

Scheduled Pinned Locked Moved C / C++ / MFC
help
4 Posts 3 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.
  • A Offline
    A Offline
    anbluemoon
    wrote on last edited by
    #1

    I define a class like this : class Test { private: static Test* instance; private: typedef void (Test::*SayHello)(char*); SayHello sayHello; void Say(char* name) { cout<< "Hello " <<name; } static void test() { instance->*sayHello("hello"); // this where i got the error notification } public: Test() { sayHello = &Test::Say; } ~Test() { } static Test* getInstance() { if(instance == NULL) instance = new Test(); return instance; } }; When compiling i receive some error messages : error C2597: illegal reference to non-static member 'Test::sayHello' error C3867: 'Test::sayHello': function call missing argument list; use '&Test::sayHello' to create a pointer to member error C2568: '->*' : unable to resolve function overload If you have any ideas plz tell me Thanks in advance

    K N 2 Replies Last reply
    0
    • A anbluemoon

      I define a class like this : class Test { private: static Test* instance; private: typedef void (Test::*SayHello)(char*); SayHello sayHello; void Say(char* name) { cout<< "Hello " <<name; } static void test() { instance->*sayHello("hello"); // this where i got the error notification } public: Test() { sayHello = &Test::Say; } ~Test() { } static Test* getInstance() { if(instance == NULL) instance = new Test(); return instance; } }; When compiling i receive some error messages : error C2597: illegal reference to non-static member 'Test::sayHello' error C3867: 'Test::sayHello': function call missing argument list; use '&Test::sayHello' to create a pointer to member error C2568: '->*' : unable to resolve function overload If you have any ideas plz tell me Thanks in advance

      K Offline
      K Offline
      KingsGambit
      wrote on last edited by
      #2

      A static function can be called without creating an instance of the class. But the member function pointer needs the class instance to call the function. So, the compiler doesn't allow calling a non-static member function from a static function.

      A 1 Reply Last reply
      0
      • K KingsGambit

        A static function can be called without creating an instance of the class. But the member function pointer needs the class instance to call the function. So, the compiler doesn't allow calling a non-static member function from a static function.

        A Offline
        A Offline
        anbluemoon
        wrote on last edited by
        #3

        But i did have a private static instance of Test class , and i use that to call the function pointer

        1 Reply Last reply
        0
        • A anbluemoon

          I define a class like this : class Test { private: static Test* instance; private: typedef void (Test::*SayHello)(char*); SayHello sayHello; void Say(char* name) { cout<< "Hello " <<name; } static void test() { instance->*sayHello("hello"); // this where i got the error notification } public: Test() { sayHello = &Test::Say; } ~Test() { } static Test* getInstance() { if(instance == NULL) instance = new Test(); return instance; } }; When compiling i receive some error messages : error C2597: illegal reference to non-static member 'Test::sayHello' error C3867: 'Test::sayHello': function call missing argument list; use '&Test::sayHello' to create a pointer to member error C2568: '->*' : unable to resolve function overload If you have any ideas plz tell me Thanks in advance

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          You can't access instance members from a static method. Also the singleton implementation is wrong and your example doesn't make sense at all. Here is a modified version of your code which compiles fine.

          class Test
          {
          private:
          typedef void (Test::*SayHello)(char*);
          SayHello sayHello;

          // Made the constructor private
          Test()
          {
          sayHello = &Test::Say;
          }

          // Preventing compiler generated methods
          Test(const Test&);
          Test& operator=(const Test&);

          void Say(char* name)
          {
          cout<< "Hello " << name;
          }

          static void test()
          {
          Test* instance = Test::getInstance();
          instance->FireSayHello("hello");
          }

          public:

          void FireSayHello(char* message)
          {
          (*this.*sayHello)(message);
          }

          static Test* getInstance()
          {
          static Test instance;
          return &instance;
          }
          };

          Best wishes, Navaneeth

          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