Need help with member function pointer
-
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 -
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 advanceA 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 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.
But i did have a private static instance of Test class , and i use that to call the function pointer
-
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 advanceYou 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