The original problem concerned me making an object (templated container
class) for an intermediate level programming club. The object was to be
simple to use (excludes most stl calls) and semi-intelligent toward it's
items. Users could construct this object for any class they wrote, but that
class had to implement certain functions. I remember they had to implement,
bool Test(int iTestId). The users could then Add() members to the
container, and execute functions on all items who answered true to Test().
It's been some months now. I never did find an implementation that was
powerful and simple. I still, however, consider it a worthy challenge and
thus give it thought from time to time.
-Kanon
Quote:
> >Given a class with several functions....
> >class Foo
> >{
> >public:
> > void SetX(int x);
> > void SetY(int y);
> >};
> >How could one make a container class.....
> >template <class item>
> >class MyContainer
> >{
> > // Implementation unknown
> >};
> >such that you could create an instance like so...
> >MyContainer <Foo> theContainer;
> >then make function call to all items in the container like so
> >theContainer.SetX(7); // Should call SetX for each item
> >theContainer.SetY(42); // Should call SetY for each item
> In order to inject Foo's interface into that of MyContainer, it would
> have to inherit from Foo, but that's no good since you don't want to
> call SetY on the container, but on each contained element.
> >Just curious - not urgent....take your time and savor.
> >Any solution is acceptable. (Example: I got the closest to a solution
using
> >macros combined with templates.)
> >Full item encapsulation is preferred.
> > (The container's internal item storage should be private or protected)
> >One line instantiation, Zero line definition is preferred.
> > (Should not have to define anything to make it work for a new item
> >type.)
> Well, within the constraints it isn't possible without making
> Container have a function with every possible function name up to a
> certain length (which would be theoretically possible given sufficient
> amount of memory). e.g.
> void a()
> {
> //for loop calling a on each element
> }
> template <class T>
> void a(T t)
> {
> //for loop calling a(t) on each element
> }
> //more a overloads
> void b()
> {
> }
> //...
> void z()
> //...
> void a1()
> //.....
> template <class T1, class T2, class T3>
> void aa1bczAbdD(T1 t1, T2 t2, T3 t3)
> In the absense of that (!), then what's wrong with standard algorithm
> techniques, using iterators?
> for_each(c.begin(), c.end(), bind(&Foo::setX, _1, 7));
> If you really had to, you could add for_each to the interface of
> MyContainer:
> c.for_each(bind(&Foo::setX, _1, 7));
> Tom