class Demo { public: Demo(){} Demo(string name, int age){ m_strName = name; m_iAge = age; } //构造函数可以重载 private: string m_strName; int m_iAge; }; class Demo { public: Demo(){} Demo(string name = "James", int age = 24){ m_strName = name; m_iAge = age; } //构造函数形参列表可以赋初值,但这种情况不行,全部赋初值会与前面的无参构造函数冲突 private: string m_strName; int m_iAge; }; class Demo { public: Demo(){} Demo(string name, int age):m_strName(name),m_iAge(age){} //初始化列表,可用于初始化const修饰的 //成员变量,初始化列表先于构造函数执行,效率高 private: const string m_strName; const int m_iAge; };
时间: 2024-09-26 21:36:11