次のコードを試しました:
#include <iostream>
using std::cout;
using std::ostream;
class X
{
public:
friend ostream& operator<<(ostream &os, const X& obj)
{
cout << "hehe"; // comment this and infinite loop is gone
return (os << obj);
}
};
int main()
{
X x;
cout << x;
return 0;
}
これをコンパイルして実行すると、期待どおりです。無限ループ。cout
フレンド関数内のステートメントを削除すると、再帰は発生しません。なぜそうなのですか?