cpp initialize list code example
Example 1: member initializer list in c++
// Constructor Member Initializer List
#include <iostream>
class Example
{
private:
int x, y;
public:
Example() : x(0), y(0) {}
Example(int x1, int y1) : x(x1), y(y1) {}
~Example() {}
};
int main()
{
Example e;
}
Example 2: initialization list c++
struct S {
int n;
S(int); // constructor declaration
S() : n(7) {} // constructor definition.
// ": n(7)" is the initializer list
};
S::S(int x) : n{x} {} // constructor definition. ": n{x}" is the initializer list
int main() {
S s; // calls S::S()
S s2(10); // calls S::S(int)
}