C++ 03 :
class Dog { // Aggregate class or struct
public:
int age;
string name;
};
Dog d1 = {5, "Henry"}; // Aggregate Initialization
Uniform Initialization:
Uniform Initialization expands on the Initializer List syntax, to provide a syntax that allows for fully uniform type initialization that works on any object – removing the distinction between initialization of aggregate + non-aggregate classes, arrays, STL/custom collection classes, and PODs.
// C++ 11 extended the scope of curly brace initialization
class Dog {
public:
Dog(int age, string name) {...};
};
Dog d1 = {5, "Henry"};
Uniform Initialization Search Order:
1. Initializer_list constructor
2. Regular constructor that takes the appropriate parameters.
3. Aggregate initializer.
==> Dog d1{3};
Example:
class Dog {
public:
int age; // 3rd choice
Dog(int a) { // 2nd choice
age = a;
}
Dog(const initializer_list
age = *(vec.begin());
}
};
Comments
Post a Comment