mardi 2 février 2016

About conversion operator and operator()

In the following code:

template <class T> class mval {
protected:
    T max;
public:
    template <class iter> mval (iter begin, iter end):max(*begin) {
        while(begin != end) {
            (*this)(*begin);
            ++begin;
        }
    }
    void operator()(const T &t) {
        if (t > max)
            max = t;
    }
    void print() {
        cout << max;
    }
};

int main() {
    int arr[3] = { 10,20,5 };
    (mval<int>(arr, arr + 3)).print();
}

Why does (*this)(*begin); leads to the operator()?

Shouldn't it go to this operator only when you have something like mval x; x(T); ? But it behaves like it's a conversion operator, how?

Aucun commentaire:

Enregistrer un commentaire