1. c ++ supports three member functions: static, non-static, and virtual functions.
2. Various Member call Methods
Non-static member functions. In C ++, all non-static member functions are converted into a global member function and implicitly passed to a Class Object Pointer, in addition, the efficiency of selecting member functions is the same as that of global functions, so there is no burden on them.
Virtual member function calls will be converted into a pointer pointing to vptr, and vptr pointing to virtual function table
For example
PTR-> normalize (), normalize () is a virtual member function that will internally convert to (* PTR-> vptr [1]) (PTR ),
Static member function. If normalize () is a static member function, then:
OBJ. normalize () and PTR-> normalize () will be converted to normal function bar, because static is stored outside the class object, normal_point3dsfv ()
The static modifier makes it impossible to use the object pointer:
(1) The non-static member in its class cannot be directly accessed. to access non-static data members within the scope of static, you must use the this pointer.
#include <iostream>
using namespace std;
class A
{
public:
A(int a)
{
this->a = a;
}
static int f()
{
//error: invalid use of member 'A::a' in static member function
//return a;
}
private:
int a;
};
int main()
{
A a(4);
int i = a.f();
return 0;
}
In fact, the use of member functions does not work either:
#include <iostream>
using namespace std;
class A
{
public:
A(int a)
{
this->a = a;
}
static int f()
{
//error: invalid use of member 'A::a' in static member function
//return a;
int i = this.g();
return i;
}
int g()
{
return a;
}
private:
int a;
};
int main()
{
A a(4);
int i = a.f();
return 0;
}
(2) cannot be declared as const, volatile, or virtual (this pointer is required)
(3) You can use class to directly call an object without calling the object.
Static member function, which can be a callback function
3. Virtual member functions: single inheritance, multi-inheritance, and virtual inheritance