c++ initializer list constructor code example
Example 1: member initializer list in c++
#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);
S() : n(7) {}
};
S::S(int x) : n{x} {}
int main() {
S s;
S s2(10);
}
Example 3: initializer list c++
class Example {
public:
int m_A, m_B, m_C;
Example(int a, int b, int c);
};
Example::Example(int a, int b, int c):
m_A(a),
m_B(b),
m_C(c)
{ }
Example 4: c++ initialization list
class Something
{
private:
int m_value1;
double m_value2;
char m_value3;
public:
Something()
{
m_value1 = 1;
m_value2 = 2.2;
m_value3 = 'c';
}
};