Reputation: 6192
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?
Upvotes: 2
Views: 150
Reputation: 133004
How is mval x; x(T);
any different than (*this)(*begin);
? In both cases I see an object of type mval
followed by parentheses with one argument inside. What did you expect to happen? (*this)
is not a type, it's an lvalue of type mval
, so I don't see how it "behaves like it's a conversion operator" either.
Upvotes: 4