mardi 5 avril 2016

Prefered method of populating a std::vector of classes with its first entry

In reviewing a large software project I came a cross two ways of doing essentially the same thing, pushing an initial entry on a std::vector

consider a class like Foo

class Foo
{
   public:

    Foo(int param){
      m_param = param;
    }

    setParam(int param){
      m_param = param;
    }
  private:
    int m_param;
}

Is there a preferred method between the following considering whatever applicable metrics.... speed, stability, etc.

Foo bar;
int val = 5;
bar.setParam(val);
std::vector<Foo> fooVec(1, bar);

Versus

int val = 5;
std::vector<Foo> fooVec;
fooVec.push_back(Foo(val));

Aucun commentaire:

Enregistrer un commentaire