当前位置:网站首页>Const's constant member function after the function; Form, characteristics and use of inline function
Const's constant member function after the function; Form, characteristics and use of inline function
2022-07-04 10:30:00 【Big bear loves to work】
1 const Decorated constant member functions and const Class object
class Person{
//...
public:
int age() const; // Constant member function
int score(); // Non member function
private:
int m_Age;
int m_Score;
//...
}
- As mentioned above age() Function is a Constant member function , Constant member functions have the following characteristics
- Express You cannot change the value of a member variable Function of
- In constant member functions Only constant member functions can be called , Because very member functions , It is possible to change the value of member variables
- const Class objects can only call constant member functions in a class
- Because since the class object is already const 了 , If you call a non member function , It is possible to change the content of the class , This is related to const Class object contradiction
bool cmp(const Person& a, Person& b) {
return a.age() > b.age(); // Compiler yes
// return a.score() > b.score(); // Compiler error , Because with const Class object called non member function
}
// among a yes const Class object , Only constant member functions can be called
- For ordinary class objects , Then whether it is a constant member function or not , You can call
2 Inline function
// The first inline function
class Person{
//...
public:
inline( Not to add ) int age() const { return m_Age; } // Constant member function and inline function
int score(); // Nonmember functions
private:
int m_Age;
int m_Score;
//...
}
// The second kind of inline function
inline void myFun()
{
//....
//....
}
- The above code shows Two expressions of inline functions
- Write the member function implemented by the function directly in the definition of the class , Whether you add keywords or not inline Are all inline functions
- Ordinary function definition , Add keywords before its functions inline, It becomes an inline function
- The function of inline function
- In order to solve the efficiency problem of function call in the program
- The essence is , In the compilation stage, the compiler replaces the call expression of the inline function in the program with the function body of the inline function , Used to avoid calling this function at run time , This saves the overhead of calling functions at runtime ( For example, context saving , Copy arguments, etc )
- Points for attention of inline functions
- Loop statements and switch statements cannot be used in inline functions
- Generally, they are very short functions ( Such as 10 Within the line )
- The definition of an inline function must appear before the first call
边栏推荐
- leetcode1-3
- Number of relationship models
- Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
- leetcode842. Split the array into Fibonacci sequences
- Pod management
- MongoDB数据日期显示相差8小时 原因和解决方案
- Rhcsa day 9
- Three schemes of ZK double machine room
- Introduction to extensible system architecture
- Native div has editing ability
猜你喜欢
[200 opencv routines] 218 Multi line italic text watermark
Some summaries of the third anniversary of joining Ping An in China
Huge number (C language)
Two way process republication + routing policy
Use the data to tell you where is the most difficult province for the college entrance examination!
Summary of several job scheduling problems
C language - stack
VLAN part of switching technology
Tables in the thesis of latex learning
Rhcsa12
随机推荐
2021-08-11 function pointer
使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
Check 15 developer tools of Alibaba
RHCE - day one
The bamboo shadow sweeps the steps, the dust does not move, and the moon passes through the marsh without trace -- in-depth understanding of the pointer
Development guidance document of CMDB
Time complexity and space complexity
Student achievement management system (C language)
/*The rewriter outputs the contents of the IA array. It is required that the type defined by typedef cannot be used in the outer loop*/
Quick sort (C language)
Exercise 7-3 store the numbers in the array in reverse order (20 points)
Vanishing numbers
Occasional pit compiled by idea
The future education examination system cannot answer questions, and there is no response after clicking on the options, and the answers will not be recorded
Rhcsa day 10 operation
【Day2】 convolutional-neural-networks
PHP code audit 3 - system reload vulnerability
Dos:disk operating system, including core startup program and command program
RHCE day 3
/*Rewrite the program, find the value of the element, and return the iterator 9.13: pointing to the found element. Make sure that the program works correctly when the element you are looking for does