#include <stdio.h>
#include <set>
#include <algorithm>
class ClaseInteresante
{
public:
ClaseInteresante(int unValor = 0) : valor(unValor)
{
printf("Creando: %pn", this);
mInstancias.insert(this);
};
ClaseInteresante(const ClaseInteresante& c)
{
printf("Creando copia: %pn", this);
valor = c.valor;
mInstancias.insert(this);
};
~ClaseInteresante()
{
printf("Destruyendo: %pn", this);
mInstancias.erase(this);
};
template<class F>
static void aplicarEnTodos(F f)
{
std::for_each(mInstancias.begin() ,mInstancias.end() ,f);
}
int getValor()
{
return valor;
};
private:
int valor;
static std::set<ClaseInteresante*> mInstancias;
};
std::set<ClaseInteresante*> ClaseInteresante::mInstancias;
struct ClaseImpresora
{
void operator()(ClaseInteresante* i)
{
if (i)
{
printf("Objeto: %p, Valor: %dn", i , i->getValor());
}
};
};
void main()
{
ClaseInteresante a(1), b(2), c(3);
ClaseInteresante d(c);
ClaseInteresante e(c);
ClaseImpresora i;
printf("nAplicando funcion a todas las instancias:n");
ClaseInteresante::aplicarEnTodos(i);
printf("n");
}