cout and cin are not functions, so what are they?

后端 未结 4 1117
予麋鹿
予麋鹿 2021-01-30 18:28

Generally in C programming language We consider printf and scanf to be functions. when it comes to cout and cin, in C++ what are they?I mean they cant be functions as they are n

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-30 19:27

    std::cout and std::cin are global objects of classes std::ostream and std::istream respectively, which they've overloaded operator << and >>. You should read about operator overloading.

       cout    <<      expr  ;
      ~~~~~~  ~~~~   ~~~~~~~~
      object   op.   argument 
    

    It's like a function call; the function is an overloaded operator and a shortcut for this:

    cout.operator<<(expr);
    

    or this:

    operator<<(cout, expr);
    

    depending on the results of overload resolution

提交回复
热议问题