3

In writing a program, I've come up with a piece of code that I cannot manage to make code-duplication free.

#include <iostream>

using namespace std;

class Presenter{
    public:
        template <typename T>
            void addField(T& t){
                cout << "do something that could happen to modify t" << endl;
            }

        template <typename T>
            void addField(const T& t){
                cout << "do something that cannot possibly happen to modify t" << endl;
            }
};

class Presented{
    public:
        Presented() : a(0), b(0.) {}
        void bind(Presenter& p){
            p.addField(a);
            p.addField(b);
            //...
        }
        void bind(Presenter& p) const {
            p.addField(a);
            p.addField(b);
            //...
        }
    private:
        int a;
        double b;
        //...
};

int main() {
    Presenter presenter;
    Presented p1;
    const Presented p2;
    p1.bind(presenter);
    p2.bind(presenter);
}

Here is a simple dummy program which shows the issue. As you see, the code of the two bind functions is (looks) exactly the same. Of course it is not, since two different functions (addField) are used, which only happen to share the name.

Nonetheless, I have been looking for a way to remove the need to verbatim write the void bind(Presenter& p) const.

Does anyone see a method for reaching the goal? I wasn't able to come up with one.

1 Answer 1

4

Delegate to a template that can be called with a const or a non-const instance of Presented:

class Presented{
public:
    Presented() : a(0), b(0.) {}
    void bind(Presenter& p){
        bindImpl(p, *this);

    }
    void bind(Presenter& p) const {
        bindImpl(p, *this);
    }
private:
    template<typename P>
    static void bindImpl(Presenter& p, P& presented)
    {
        p.addField(presented.a);
        p.addField(presented.b);
    }

    int a;
    double b;
};
Sign up to request clarification or add additional context in comments.

1 Comment

static... I was quite there, but I didn't manage to think of making it static. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.