我刚刚发现了 auto_ptr 的概念并且很喜欢它!由于 Qt 经常需要 QList 或 QVector<(some QObject or QWidget) *>,所以有什么具体的理由应该避免使用 auto_ptr。如果我是对的,它允许您替换它:
std::vector<MyClass*> vec;
/* add several elements to the vector and do stuff with them */
for(size_t i=0; i<vec.length(); ++i)
{
delete vec[i];
}
vec.clear();
更短的东西(即没有清理)
std::vector<auto_ptr<MyClass>> vec;
/* add several elements to the vector and do stuff with them */
// no need for the delete loop
... Qt 仍然可以使用 auto_ptr 来发挥其共享内存的魔力吗?父子自动内存管理是否仍然透明运行?谢谢