ststic 静态修饰
#includeusing namespace std; class AA { public: static int a; int b; public: AA() { b = 200; } void Show() { cout << a << " " << b << endl; ShowShow(); } static void ShowShow(); }; void AA::ShowShow() { cout << a << endl; //静态成员函数 不允许使用非静态成员(变量或函数) 因为静态成员函数没有this指针 } int AA::a = 100; //静态成员变量 必须要在类外初始化 类型 +类名+ :: +变量名 不赋值默认初始化为0 //可以不通过使用 类名::变量名使用 //所有对象共用一个 //受访问修饰符的约束 //非静态的能使用静态的 静态的不能使用非静态的 int main() { AA a; a.Show(); a.ShowShow(); AA::ShowShow(); return 0; }
const
#includeusing namespace std; class AA { public: const int a; int b; public: AA():a(100) { b = 200; } public: void Show()const { cout << a << b <<" " << endl; //a = 12; //b = 5; } }; //const修饰的变量必须要在初始化列表里完成初始化 //const 修饰的函数为常函数 如void Show()const不能修改成员变量 //普通成员函数this指针类型 AA* const this //const成员函数this指针类型 const AA* this int main() { AA a; cout<
常对象
#includeusing namespace std; class AA { public: const int a; int b; public: AA() :a(100) { b = 200; } public: void Show()const { cout << a << b << " " << endl; //a = 12; //b = 5; } }; int main() { AA a; a.Show(); const AA b; //b.a = 5; //b.b = 10; //用const 修饰的对象叫常对象 不能通过对象修改成员变量 常对象只能使用常函数 return 0; }



