samedi 2 mai 2015

How to automatize overriding methods?

I am wondering if there is any tricky way to override all class methods in the same manner. For instance, how to wisely implement composite pattern in large classes? When i get something like this for instance:

        class Foo{
            virtual void work() = 0;
...
        };

        class FooLeaf : public Foo{
            virtual void work() override{}
...
        };

        class FooComposite : public Foo{
            std::vector<Foo *> foos;
            virtual void work() override{
                for (auto foo : foos){
                    foo->work();
                }
            }
...
        };

It is not a problem to reimplement one method. But when the number of methods grows the code get tremendously WET. I mean, I don't feel good copy-pasting the foreach loop for, let's say, 10 methods.

Aucun commentaire:

Enregistrer un commentaire