implement a general method later in c++ -
i know following code won't compile, posted anyway because exemplifies trying accomplish.
typedef struct { void actionmethod(); }object; object myobject; void myobject.actionmethod() { // something; } object anotherobject; void anotherobject.actionmethod() { // else; } main() { myobject.actionmethod(); anotherobject.actionmethod(); }
basically want kind of delegate. there easy way this?
i can't include <functional>
header , use std::function
either. how can this?
for example:
#include <iostream> using namespace std; struct anobject { void (*actionmethod)(); }; void anactionmethod() { cout << "this 1 implementation" << endl; } void anotheractionmethod() { cout << "this implementation" << endl; } int main() { anobject myobject, anotherobject; myobject.actionmethod = &anactionmethod; anotherobject.actionmethod = &anotheractionmethod; myobject.actionmethod(); anotherobject.actionmethod(); return 0; }
output:
this 1 implementation implementation
Comments
Post a Comment