What's the difference between cout<<cout and cout<<&cout in c++?

后端 未结 5 1756
余生分开走
余生分开走 2020-12-06 02:59

This might be a beginner question and understanding how cout works is probably key here. If somebody could link to a good explanation, it would be great. cout<

5条回答
  •  一向
    一向 (楼主)
    2020-12-06 03:26

    cout << cout is equivalent to cout << cout.operator void *(). This is the idiom used before C++11 to determine if an iostream is in a failure state, and is implemented in std::ios_base; it usually returns the address of static_cast(&cout).

    cout << &cout prints out the address of cout.

    Since std::ios_base is a virtual base class of cout, it may not necessarily be contiguous with cout. That is why it prints a different address.

提交回复
热议问题