member initialization list code example

Example 1: 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)
}

Example 2: c++ initialization list

class Something
{
private:
    int m_value1;
    double m_value2;
    char m_value3;
 
public:
    Something()
    {
        // These are all assignments, not initializations
        m_value1 = 1;
        m_value2 = 2.2;
        m_value3 = 'c';
    }
};