How do I initialize a stl vector of objects who themselves have non-trivial constructors?

后端 未结 5 1491
悲哀的现实
悲哀的现实 2020-12-07 17:56

suppose I have the following class:

class MyInteger {
private:
  int n_;
public:
  MyInteger(int n) : n_(n) {};
  // MORE STUFF
};

And supp

5条回答
  •  爱一瞬间的悲伤
    2020-12-07 18:25

    Besides all answers which answered the question very well, in a case that your class MyInteger is not copy-constructible, you could use this trick : instead of creating vector< MyInteger>, you could create vector< shared_ptr< MyInteger > >

提交回复
热议问题