我有这个虚拟方法:
const string& my_class::to_string() const
{
string str(this->name + string(" "));
if(!this->children.empty())
{
for(const std::shared_ptr<base_class> e : this->children)
str.append(e.get()->to_string());
}
return str;
}
哪里children
是 a std::list<std::shared_ptr<base_class>>
,并my_class
继承base_class
。但是,在第一次递归调用 (of my_class::to_string
) 之后,在我返回这个 child 之后str
,我得到了一个错误的分配。
为什么?