1

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?

Bruice
  • 63
  • 6
  • Is this a duplicate: [Why is list initialization (using curly braces) better than the alternatives?](https://stackoverflow.com/q/18222926/10871073)? – Adrian Mole Jul 09 '21 at 12:55
  • For pointers - no, for general user-defined types - yes. – Evg Jul 09 '21 at 13:00
  • @AdrianMole it's related, but there they talk about local variables and not class fields initialization. Though it may be same but I'm concerned if there are some nuances particularly for in-class initialization – Bruice Jul 09 '21 at 13:01
  • @Bruice Indeed. That's why I didn't close-vote this question: *subtle nuances*. – Adrian Mole Jul 09 '21 at 13:02

0 Answers0