C++ Basics Series (1) Object Pointer

1. Function pointers and pointer functions

1.1 Function pointer

         These two are two relatively complicated concepts in C language. Let’s talk about function pointers first.

         A function pointer is essentially a pointer whose address points to a function.

         If a function is defined in the program, the system will allocate a storage space for the function code during compilation. The first address of this storage space is called the address of the function, and the function name represents this address.

        The writing method is as follows:   

        Function return value type (* pointer variable name) (function parameter list);

        int  (*fun) (int a , int b);

1.2 Pointer functions

         A pointer function is essentially a function whose return value is a pointer

         A pointer function is afunction. Functions all have return types (if they do not return a value, they are valueless), but the return type of a pointer function is a pointer of a certain type.

        It is written as follows:

        *Type identifier function name (parameter list);

        int  *fun(int x,int y);

       The difference between a regular function and a pointer function: There is an * in front of the function name, and this function is a pointer function. The return value is a pointer of type int, which is an address.

Other ways of understanding:

Pointer function: int* fun(int x,int y);

Function pointer: int (*fun)(int x,int y);

It can be understood simply and crudely that the * in the pointer function belongs to the data type, while the asterisk in the function pointer belongs to the function name.

A very simple way to determine is to observe (*), just remember one of them:

int (*p)(int,int); There are brackets, * is combined with p, *p is a pointer, pointing to a function that returns an integer and has two integer parameters, so it is called a function pointer.

int*p(int,int); There are no brackets, * is combined with int, int* is the return type, and p is a function name. At this time, it is a pointer function, but the return value type is int*.

2. Object pointer

      Let’s take a look at object pointers in C++

2.1 Pointers to object member functions

        It should be noted that the method of defining pointer variables pointing to object member functions is different from the method of defining pointer variables pointing to ordinary functions.

        The pointer variable pointing to an ordinary function is the function pointer mentioned in the first section

   Defining a pointer variable pointing to an object member function is a bit more complicated and can be explained directly through code.

#include <iostream>
using namespace std;

class Time {
    public:
    Time(int, int, int);  //声明带参构造函数
    int hour;
    int minute;
    int sec;
    void get_time(); // 声明共有成员函数
};


Time :: Time(int h, int m, int s) {
    hour = h;
    minute = m;
    sec = s;
}

void Time::get_time() {
    cout<<hour <<":" <<minute << ":" << sec << endl;
}

int main(int argc, const char** argv){
    Time t1(10, 20, 25);  // 定义Time对象t1
    int *p = &t1.hour; //定义指向整型数据的指针变量p  并使p指向t1.hour
    cout<< *p << endl;  
    
    t1.get_time(); //调用对象 t1的成员函数get_time()

    Time *p2 = &t1;  //定义指向Time 类对象的指针变量 p2, 并使 p2指向   t1
    p2->get_time();  // 调用  p2 所指对象(即t1)的get_time() 方法

    void (Time :: *p3) (); //定义指向Time类公用成员函数的指针变量p3;
    p3 = &Time::get_time;  // 使p3指向Time类公用成员函数get_time();

    (t1.*p3)(); //调用对象 t1中   p3所指的成员函数即 t1.get_time();

     void (Time :: *p4) () = &Time::get_time; // 定义指针变量时指定其指向

    return 0;
}

Printout:

10
10:20:25
10:20:25
10:20:25

Guess you like

Origin blog.csdn.net/u012514113/article/details/133927757