vendredi 18 juin 2021

Is there difference between different ways of creating an object that contains other objects and is passed to a function to be added to a vector

If we have a clas that contain vector of objects of other class

   class BigClass
    {
    private:
    vector<ClassName> vec;
    //more code
    public:
    void AddItem(const ClassName& i) {vec.push_back(i);}
    //more code
    };

And ths class also contain objects of other classes(structs, as they are simple). Which contain yet another objects.

class ClassName
{
private:
StructName obj;
//more code
public:
ClassName(const StructName &s) : obj(s) {}
ClassName(double x,
 double y,
 int i,
 const &OtherStruct o1,
 const &OtherStruct o2) 
 : obj({x, y, i, o1, o2}) {}
};

struct StructName 
{
 double x;
 double y;
 int i;
 OtherStruct o1;
 OtherStruct o2;
};

struct OtherStruct
{
  int x;
  int y;
  int z;
};

Is there any difference/benefits/downsides between different options of calling AddItem? Imagine we already have variables double x, double y, int i, const &OtherStruct o1, const &OtherStruct o2

AddItem({x, y, i, o1, o2}); //1
AddItem(ClassName{x, y, i, o1, o2}); //2
AddItem(ClassName(x, y, i, o1, o2)); //3
AddItem(StructName{x, y, i, o1, o2}); //4
AddItem(ClassName{StructName{x, y, i, o1, o2}}); //5
AddItem(ClassName(StructName{x, y, i, o1, o2})); //6

Aucun commentaire:

Enregistrer un commentaire