我在這裏做了一個測試,但輸出是一個沒有結束的循環,我不知道爲什麼。構造函數中的無限循環沒有for或while
其實,我正在做另一個測試,但是當我寫這個時,我不明白循環是怎麼發生的。它反覆輸出「ABC」。
#include <map>
#include <string>
#include <iostream>
class test
{
public:
std::map <int, int> _b;
test();
test (std::map<int, int> & im);
~test();
};
test::test()
{
std::cout<<"abc";
_b.clear();
_b[1]=1;
test(_b);
}
test::test(std::map <int, int>& im)
{
std::cout<<im[1];
}
test::~test() {};
int main()
{
test a;
}
由於'test(_b)'的遞歸;'但我不確定原因。 – Roddy 2013-04-24 20:07:08
@ Roddy-我只是想通了;詳情請參閱我的答案。 – templatetypedef 2013-04-24 20:09:14
已刪除不重要的內容的清理版本:http://ideone.com/z0yc7Q – Yakk 2013-04-24 20:15:33