特殊函数方法函数
静态方法:this
const方法:编码
静态方法与const方法关系:指针
固然,若是类的成员函数不会改变对象的状态,那么该成员函数会被声明为const,但有时候须要在const函数中修改一些与类的状态无关的数据成员,那么该数据成员就应该被mutable修饰,如计算运算次数,运行效率等等。code
2.举例对象
class test { public: static int getNumber(); string getString()const; int setNumber(int x); static int getAnotherNumber(test anothertest); private: static int n; string s; }; int test::n = 0; int test::getNumber() { //return this->n;//this只能用于非静态成员函数内部 //string s1 = s;//静态成员函数仅能访问静态数据成员 return n;//访问private成员变量 } int test::setNumber(int x) { this->n = x; return this->n; } string test::getString()const { return s; } int test::getAnotherNumber(test anothertest) { return anothertest.n; } void main() { test onetest, twotest; onetest.setNumber(8); twotest.setNumber(9); cout << onetest.getAnotherNumber(twotest)<<endl;//个对象能够经过调用静态方法访问另外一个同类型对象的private和protected静态数据成员。 const test threetest; cout<<threetest.getNumber()<<"\n"; //cout << threetest.setNumber(5);//常量对象仅能调用const方法 getchar(); }