To assign parameter values ​​to the same properties, I can use an initialization list:

class A { A(int a, int b, int c) : a(a), b(b), c(c) {}; int a; int b; int c; } 

But if such parameters are, for example, six, then scribbling the initialization list is somehow ugly. Is there a more elegant way of assigning the values ​​of the constructor parameters to the object properties of the same name?

  • What is a more elegant way to assign values ​​?? - ampawd
  • If they are all of int type, then it is possible to transfer by array - Stanislav Grotto
  • If the object must be initialized, then we cannot do without a constructor. - αλεχολυτ

1 answer 1

This method is only for structures in which:

  1. No private or protected non static members
  2. No custom constructors
  3. There are no virtual , private or protected base classes.
  4. There are no virtual functions
  5. There are no default initializers

If all this is not in your class, structure or association, you can write this:

 struct Foo{ int i; double d; char c; }; Foo foo = {42, 3.14, 'c'}; 

This is called aggregate initialization.

Otherwise, you will have to write the initialization list in the constructor.

  • There is no such word :) Initializers are allowed in c ++ 14 - αλεχολυτ