📜  C++ 构造函数 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:12.347000             🧑  作者: Mango

代码示例2
struct S {
    int n;
    S(int); // constructor declaration
    S() : n(7) {} // constructor definition.
                  // ": n(7)" is the initializer list
                  // ": n(7) {}" is the function body
};
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)
}