我正在尝试在 A 类构造函数中添加选项以从文件加载对象。但是我不确定加载失败时该怎么办(文件加载失败,文件格式错误..)。代码使用 A 无论 loadObjects 是否为真,这会导致分段错误。也许在构造函数中加载不是最好的方法......
template <typename T>
class A
{
    public:
        A(const std::vector<Obj<T>*>& o) : objs(o) {}
        A(const std::string& file)
        {
            // loadObject adds new objects in objs
            // objs.push_back(new Obj<T>);
            if ( loadObjects(file, objs) ) 
            {
                // good, can use object A
            }
            else
            {
                // Segmentation fault when using undefined A, 
                // What can I do to stop execution here.
            }
        }
        virtual ~A()
        {
            for (size_t i=0; i<objs.size(); ++i)
                delete objs[i];
            objs.clear();
        }
    private:
        std::vector<Obj<T>*> objs;
};