1 Regular member functions
1.1 Declare:< type identifier > function name (parameter list) const;
1.2 Description:
1 Const is a part of the function type, in the implementation part also should take this keyword;
2 The Const keyword can be used to distinguish the overloaded functions;
3 A constant member function cannot update a member variable of a class, nor can it invoke a member function in a class that does not have a const modifier, and can only invoke a regular member function, but may be called by other member functions;
4) in particular : a regular object can only access the const member functions in a class (except for the implicit constructors and destructors that are automatically invoked by the system)
1.3 Routines:
Class a{
private:
int W, h;
Public:
int getValue () const;
int GetValue ();
A (int x, int y): w (x), h (y) {}
A () {}
~a () {}
};
int A::getvalue () const{return
w*h;
}
int A::getvalue () {return
w+h;
}
int main ()
{
const A A (1, 2);
A c (1,2);
cout << a.getvalue () << Endl; Invoke the const member function
cout << c.getvalue () << Endl; Call non-const member function return
0;
}
2 Static member functions
A member function that is decorated with static can only be defined once, and is shared by all objects of the same class, which is an act of classes that is independent of the object and has the following characteristics:
1 static function members can not directly access the class Non-static data members and non-static member functions, only through the object name (passed by the parameter) to access;
2 when the static member function is implemented outside the class, there is no need to add static modification, or error;
3 Outside the class, you can invoke the static member function of the class by using the object name as well as the class name.
Class b{
private:
int x;
int y;
static int count;
Public:
B (): X (0), y (0) {
count++;
}
B (int xx, int yy): X (xx), Y (yy) {
count++;
}
static int Getobjcount ();
int b::count = 0;
int B::getobjcount () {return
count;
}
int main ()
{
cout << b::getobjcount () << Endl;
B B1;
B B2 (a);
cout << b1.getobjcount () << Endl;
cout << b2.getobjcount () << Endl;
cout << b::getobjcount () << Endl;
return 0;
}