What would be the point of defining constructor methods within the class below? The output of the called functor within the transformation in the main function is the same irrespective of the user-defined constructors. A course on templates and the STL utilizes this code as an example of transformations but the constructors are included, which I think are unnecessary. The objective of the functor is to capitalize the first character in each string passed but it would not function properly based on the implementation herein if the constructor methods are actually utilized/called. What is the functionality of constructor methods when the functor is called directly from the class without creating an object prior?
#include <cctype>
class title_case {
char _last;
char _sep = 0;
public:
// title_case() : _last(0) {}
// title_case(const char c) : _last(1), _sep(c) {}
const char operator() (const char c);
};
const char title_case::operator() (const char c) {
// if(_sep) _last = (!_last || _last == _sep) ? toupper(c) : c;
_last = (!_last || isblank(_last)) ? toupper(c) : c;
return _last;
}
int main()
{
string s1 = "this is a string";
cout << s1 << endl;
string s2(s1.size(), '.');
transform(s1.begin(), s1.end(), s2.begin(), title_case());
cout << s2 << endl;
return 0;
}
_last = (!_last || isblank(_last)) ? toupper(c) : c;
would be UB if _last
was not initialized in constructor.