C++inline | inline member functions

C++inline member function

The member functions of C++ classes can be designated as built-in functions. The scale of member functions defined in the class body is generally very small, and the time spent in the process of system calling functions is relatively large.

The time cost of C++ calling a function is much greater than the execution time of all statements in the small-scale function body. In order to reduce the time cost, if the member functions defined in the class body do not include control structures such as loops, C++ will automatically use them as built-in Function processing.

When the program calls these member functions, it does not actually execute the calling process of the function, but embeds the function code at the calling point of the program, which can greatly reduce the time overhead of calling member functions.

C++ requires the keyword inline to be declared for general built-in functions, but for the member functions defined in the class, inline can be omitted because these member functions have been implicitly designated as built-in functions.

class Student //声明学生类 
{
    
    
  private: //声明以下为私有的 
    int number; //学号 
    char name[10];//姓名 
    char sex; //性别 
  public: //声明以下为共有的 
    inline void print_info() //在类外定义打印函数 
    {
    
    
      cout<<number<<endl;
      cout<<name<<endl;
      cout<<sex<<endl;
    }
};

In C++, if the member function is not defined in the class body but defined outside the class body, the system will not default to the built-in function. The process of calling these member functions is the same as the process of calling general functions. If you want to specify these member functions For built-in functions, inline should be used for explicit declaration.

Above, if you read and think it is helpful to you, please give Xiaolin a compliment, so that Xiaolin will also have the motivation to update, thank you fathers and villagers~

C++inline member functions
More cases can go to the public account: C language entry to proficient

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/112799059