【发布时间】:2013-04-18 12:46:06
【问题描述】:
我这里做了一个测试,但是输出的是一个没有结束的循环,不知道为什么。
其实我在做另一个测试,但是当我写这个的时候,我不明白循环是怎么发生的。反复输出“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- 我刚刚想通了;详情见我的回答。
-
已删除不重要内容的清理版:ideone.com/z0yc7Q
-
可能的骗子:Why does this call the default constructor? ...也许?
-
可能是这里的候选人? codegolf.stackexchange.com/questions/11441/…
标签: c++ recursion constructor constants