Insert into vector having objects without copy constructor

后端 未结 4 1690
执笔经年
执笔经年 2020-12-08 10:53

I have a class whose copy constructors are explicitly deleted (because A uses pointers internally and I don\'t want to fall into shallow copy pitfalls):

clas         


        
4条回答
  •  一生所求
    2020-12-08 11:34

    Just want to add to @kayleeFrye_onDeck's answer. I have a near identical situation to theirs, and the exact syntax that works for me (based on the feedback in the comments) is as follows:

    vector< std::unique_ptr > names; // Declare vector of unique_ptrs of the class instance
    
    std::unique_ptr name_ptr = std::make_unique();
    names.push_back(std::move(name_ptr)); // Need to use std::move()
    
    // Now you can access names objects without error:
    names[0]->classMethod();
    

提交回复
热议问题