Last active
December 19, 2015 13:59
-
-
Save hirohitokato/5966016 to your computer and use it in GitHub Desktop.
error: use of undeclared identifier 'proc' ... why?? -> solved. Required to specify the rvalue of '->*'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref: http://okwave.jp/qa/q3884248.html | |
#include <iostream> | |
#include <stdio.h> | |
#include <functional> | |
class SuperClass { | |
public: | |
virtual ~SuperClass() {} | |
typedef void (SuperClass::*method_type)(); | |
method_type proc; | |
std::function<void(void)> func; | |
protected: | |
SuperClass() { } | |
void SetSubClassFunction(method_type theProc) { | |
proc = theProc; | |
func = std::bind(theProc, this); | |
} | |
public: | |
void CallSubClassFunction() { | |
(this->*proc)(); // OK | |
} | |
virtual void Set() = 0; | |
}; | |
class SubClass : public SuperClass { | |
public: | |
void Set() { | |
SetSubClassFunction(reinterpret_cast<method_type>(&SubClass::TestFunction)); | |
} | |
void TestFunction() { | |
printf("SubClass::TestFunction()\n"); | |
} | |
}; | |
int main(int, char **) { | |
SuperClass *obj = new SubClass(); | |
obj->Set(); | |
obj->CallSubClassFunction(); // ←OK | |
// (obj->*proc)(); // ←NG(compilation error) | |
(obj->*(obj->proc))(); // ←OK! | |
SuperClass::method_type p = reinterpret_cast<SuperClass::method_type>(&SubClass::TestFunction); | |
(obj->*p)(); // ←OK!! | |
(obj->func)(); // ←OK!! | |
delete obj; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment