0

The unary operator method toupper was called but what is the scope resolution operator(::) doing as the function is not in the provided code. Also, is the parameter to toupper implicit so parentheses are not needed as in "toupper()".

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    string hello = "hello";
    
    cout << hello << endl;
    cout << "Begin: " << *hello.begin() << endl;
    cout << "End: " << *(hello.end()-1) << endl;
    transform(hello.begin() , hello.end(), hello.begin() +1 , ::toupper);
    
    cout << hello;
}

Bonus question- The hello variable after transform call is "hHHHH". Why is hello variable not "hHELL"?

Paul
  • 41
  • 6
  • `::toupper` means "`toupper` from the global namespace" (as opposed to namespace `std`; there's a `toupper` there, too, and because of `using namespace std;`, you'd see an error without an explicit scope). – Igor Tandetnik Jul 19 '20 at 01:29
  • Side note: `endl` is normally unnecessary. `'\n'` or `"\n"` are just as portable and you only need `endl` if you actually want the implicit `flush`: `std::cout << std::endl` is the same as `std::cout << "\n" << std::flush`. – HTNW Jul 19 '20 at 01:30

0 Answers0