Created
May 22, 2011 14:23
-
-
Save hauleth/985516 to your computer and use it in GitHub Desktop.
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
#include <list> | |
#include <iostream> | |
class Base { | |
public: | |
typedef void (*func_each)(Base*); | |
private: | |
typedef std::list<Base*> instances_list; | |
static instances_list _SM_instances; | |
public: | |
Base() { | |
_SM_instances.push_back(this); | |
} | |
virtual ~Base() {} | |
virtual int my_method() = 0; | |
static void each_instance(func_each func) { | |
for(instances_list::iterator i = _SM_instances.begin(); i != _SM_instances.end(); ++i) | |
func(*i); | |
} | |
}; | |
std::list<Base*> Base::_SM_instances; | |
class Child1 : public Base { | |
public: | |
int my_method() { | |
std::cout << "Child1\n"; | |
} | |
}; | |
class Child2 : public Base { | |
public: | |
int my_method() { | |
std::cout << "Child2\n"; | |
} | |
}; | |
class Child3 : public Base { | |
public: | |
int my_method() { | |
std::cout << "Child3\n"; | |
} | |
}; | |
class Child4 : public Base { | |
public: | |
int my_method() { | |
std::cout << "Child4\n"; | |
} | |
}; | |
void call_mymethod(Base* b) { | |
b->my_method(); | |
} | |
int main() { | |
Child1 a; | |
Child2 b; | |
Child3 c; | |
Child4 d; | |
Base::each_instance(call_mymethod); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment