mardi 27 juillet 2021

Difference between = and {} in-class initialization

When I want t put initial values inside the class definition there are two ways it can be done: with {} and =

class A
{
public:
   int a = 1;
   bool b = false;
   someClass* obj = nullptr;
};

class B
{
public:
   int a {1};
   bool b {false};
   someClass* obj {nullptr};
};

Is there any difference between those two options? Will in class A and B be something different in a way fields where initialized?

With = it will be copy initialization, so with {} should be more efficient? Or in case of in-class initialization they are equal and just provide different syntax?

Aucun commentaire:

Enregistrer un commentaire